PDA

View Full Version : Help with string arrays



mdsteen
08-04-2005, 11:50 PM
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. http://forums.parallax.com/images/smilies/smile.gif

Thanks,

mdsteen

stamptrol
08-05-2005, 12:31 AM
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

allanlane5
08-05-2005, 01:01 AM
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.

mdsteen
08-05-2005, 01:57 AM
Thanks for the ideas guys, I will try and implament a subroutine that will change my string ASCII to Decimal numbers.

mdsteen
08-05-2005, 05:38 AM
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 Savage
08-05-2005, 06:09 AM
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 (mailto:csavage@parallax.com)

mdsteen
08-05-2005, 06:26 AM
Chris, which thread are you reffering to?

--mdsteen

Chris Savage
08-05-2005, 06:51 AM
Whoops! Forgot to hit CNTL-V...

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



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

mdsteen
08-10-2005, 02:52 AM
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 Williams
08-10-2005, 03:11 AM
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

mdsteen
08-10-2005, 04:52 AM
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 Baker
08-10-2005, 04:59 AM
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 (http://www.lookuptables.com/)

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

Post Edited (Paul Baker) : 8/9/2005 9:01:56 PM GMT

Jon Williams
08-10-2005, 06:35 AM
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

mdsteen
08-11-2005, 03:44 AM
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) ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] upper
LOOKUP upper [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] upper





-mdsteen

Yanroy
08-11-2005, 04:10 AM
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 Baker
08-11-2005, 04:58 AM
-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

mdsteen
08-13-2005, 01:44 AM
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