Shop OBEX P1 Docs P2 Docs Learn Events
Help with string arrays — Parallax Forums

Help with string arrays

mdsteenmdsteen Posts: 8
edited 2005-08-12 17:44 in BASIC Stamp
Hello all,

Been reading these forums for a while now, but this is my first post.

I need help. I'm developing this code for a tester for a Sensotech DS depth transducer. I have attached the code thus far. My problem is that I am having trouble converting my incoming scientific notation into PSI. To do this I have the scientific notation coming in from the depth transducer as a string array, and I am having trouble manipulating the data after I receive it. More specifically I can't seem to be able to add a variable -- labeled upper -- to the string array -- lowerStr. Is this even possible? or is there a better way to go about this.

Any help would be much appreciated. smile.gif

Thanks,

mdsteen

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2005-08-04 16:31
    Hi,

    Looks like you're on the right track. However, you need to add one step which is converting the string representation of the digit into a number.

    A couple of techniques are:
    1. use the DEC formatters to receive the data from the depth transducer directly in number format.
    2. write a subroutine that can look at the string version of the number, for example the character "3", and convert it to the digit 3. Basically, if the character is "3", then the value becomes 3, which is different from the ASCII representation of "3".

    This is part of the reason your "upper" and "lower" don't appear to work. They're working with the ASCII values of the string representations, not numbers.

    On a related topic, your SERIN might be a bit more reliable if you could use the WAIT parameter to make sure you're always grabbing the right set of 12 characters.

    Cheers,
    Tom Sisk
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-08-04 17:01
    Well, you can't add a value and a string -- I mean you can, but it doesn't make semantic sense to do so.

    So, first you have to convert your numeric String into a number of some description. If it's scientific notation, then a floating point number would be nice -- but I don't think the BS2 does floats.
  • mdsteenmdsteen Posts: 8
    edited 2005-08-04 17:57
    Thanks for the ideas guys, I will try and implament a subroutine that will change my string ASCII to Decimal numbers.
  • mdsteenmdsteen Posts: 8
    edited 2005-08-04 21:38
    Hey all,

    Does anyone have any ideas how to effectivly implament the above suggestion of converting my string into decimal numbers?

    Thanks again,

    mdsteen
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-08-04 22:09
    I know you're working with an array instead of SPRAM, but is this thread essentially what you're trying to do.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • mdsteenmdsteen Posts: 8
    edited 2005-08-04 22:26
    Chris, which thread are you reffering to?

    --mdsteen
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-08-04 22:51
    Whoops!· Forgot to hit CNTL-V...

    http://forums.parallax.com/showthread.php?p=545173



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • mdsteenmdsteen Posts: 8
    edited 2005-08-09 18:52
    First, thanks for all the help so far.

    Second will implamenting an ASCII lookup table to convert my ASCII values to decimal values be the most efficiant way to go here?

    -mdsteen
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-09 19:11
    Can you provide an example of the strings you expect? I've done string-to-numeric conversion in GPS applications, so that code -- with adjustments -- might work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • mdsteenmdsteen Posts: 8
    edited 2005-08-09 20:52
    John,

    My incoming data stream looks like this: +1.12345E+12 -- just scientific notation. I am bringing it in via a 13 byte string array (where the 13th byte is set to 0). I want to convert the ASCII values into decimal numbers so I can manipulate them later on in the code. I have attached my thus far working code to show what I am doing.

    --mdsteen
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-09 20:59
    Conversion of ASCII numbers is relatively simple, just subtract 48 ($30 HEX) from the ASCII value to obtain the number. Youll have to test for the execptional charactors like +,-,. and E. (ASCII codes 43,45,46 and 69 respectively)

    ASCII Table Chart

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 8/9/2005 9:01:56 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-09 22:35
    With values that large you may want to use a floating point coprocessor. The uU-FPU will accept values like that as strings and convert them to floats. You can do all the math in the processor that have it give you a scaled integer back.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • mdsteenmdsteen Posts: 8
    edited 2005-08-10 19:44
    Hi again,

    Would using the Lookdown and Lookup commands be able to convert my ASCII to decimal, something like this:

    xdcrSTR  VAR  Byte(13)
    
    LOOKDOWN xdcrSTR(1) [noparse][[/noparse]"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] upper
    LOOKUP upper [noparse][[/noparse]0, 1, 2, 3, 4, 5, 6, 7, 8, 9] upper
    
    
    



    -mdsteen
  • YanroyYanroy Posts: 96
    edited 2005-08-10 20:10
    That lookup/lookdown code is functionally equivalent to just subtracting $30 from the ASCII value... you'd be much better off in terms of speed and memory to just do what Paul suggested.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-10 20:58
    -mdsteen
    I gather you are keeping the numerical value in memory for manipulations of the number. Since it is in scientific notation, I assume you'll be storing each digit into a seperate memory location (like you are doing with your xdcrSTR). To save memory space, consider storing that data into an array of nibbles (NIB) since 4 bits can represent a single digit (0-9). This way the number will take up half the space of the ASCII representation, which requires 8 bits per character. You can do the conversion on the fly so you don't need to store the ASCII value.

    Heres a code sample (which assumes the data is formatted as +1.12345E+12):
    xdcrVAL  VAR  Nib(8)
    xdcrSIGN VAR  Bit(2)
    ASCII    VAR  Byte
    iter     VAR  Nib
     
     
    'Get ASCII mantissa sign (placed in variable ASCII)
    IF ASCII = "-" THEN
       xdcrSIGN(0) = 0  'mantissa is negative
    ELSE
       xdcrSIGN(0) = 1  'mantissa is positive
    ENDIF
     
    'Get ASCII 1st mantissa digit (placed in variable ASCII)
    xdcrVAL(0) = ASCII - $30
     
    'Get ASCII decimal point, then ignore it
     
    FOR iter = 1 TO 5
       'Get ASCII mantissa digit (placed in variable ASCII)
       xdcrVAL(iter) = ASCII - $30
    NEXT
     
    'Get ASCII leter E, then ignore it
     
    'Get ASCII exponent sign (placed in variable ASCII)
    IF ASCII = "-" THEN
       xdcrSIGN(1) = 0  'exponent is negative
    ELSE
       xdcrSIGN(1) = 1  'exponent is positive
    ENDIF
    
     
    FOR iter = 6 TO 7
       'Get ASCII exponent digit (placed in variable ASCII)
       xdcrVAL(iter) = ASCII - $30
    NEXT
     
     
    'Perform data manipulations as desired
    

    You can structure the program the way you want, such as having seperate variables for the mantissa and exponent, this is only meant as an example.

    You can see that instead of needing 12 bytes of space to hold the actual data, you only need 4 bytes (8 nibs) and 2 bits.

    You didnt specify exactly how you are receiving the data, this code assumes you can get successive digits seperately (this should work if you are SHIFTINing the data, if you are SERINing the data, this approach of getting each digit seperately may not work).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 8/10/2005 9:01:44 PM GMT
  • mdsteenmdsteen Posts: 8
    edited 2005-08-12 17:44
    Hi All,

    Thanks for all of the fantastic help, this was my first big stamp coding project. I got the code working just this morning. My boss likes the results. Now on to my next stamp project for him -- a Battery Tester / Load Tester. Should be fun and interesting. I'm sure I'l be back on these forums looking for assistance, but this next project should be a lot easier, since we're using A/D to bring in the data.

    Till next time,

    -mdsteen
Sign In or Register to comment.