PDA

View Full Version : Numeric Values



Armando Herjim
- 24th June 2006, 00:31
Hello again, hope everybody´s fine!

I have some little problem here. I am receiving serial data (numbers) and I need to compare them (is it "compare them" well written?), anyways, I need to do so, but it seems that when I try to compare this data, it is taken as ascii code, so I always get the same result no matter what the data is. Is there a way to make a kind of cast to convert this ascii data into numeric data so I can do my comparison?
Thank all of you and special thank to Adam who´s I´ve been receiving too much help from.

Armando.

mister_e
- 24th June 2006, 08:51
look for DEC modifier or #

Post your code here, it will be easier to answer

paul borgmeier
- 24th June 2006, 14:07
As Steve said, post your code so we do not have to guess. If your data is in ASCII, then

Decimal 0 = 48 in ASCII
Decimal 1 = 49 in ASCII
Decimal 2 = 40 in ASCII
etc.

You can convert your received ASCII numbers to decimal by


X VAR Byte ' Received value
X = X – 48 ' convert from ASCII to decimal (or X = X - “0” does the same thing)
If X < 7 THEN Something ' your compare
or you could do your compare like this

X VAR Byte ' Received value
If X < “7” THEN Something ' “7” = decimal 55 but is 7 in ASCII
Again, post your code if you cannot make yours work.

Paul Borgmeier
Salt Lake City, Utah
USA

Armando Herjim
- 25th June 2006, 09:31
Well, thank you for your answers. The code I am using is something like this:

hserin [skip 2, str var\4 \32]

I know that I´m gonna recieve numbers, so I need to do something like this:

if var == 100 then
code here...
else
if var < 100 then
code her...
else
code here...
endif
endif

just that var contains ascii data. I had already though about some code to convert that ascii data to numbers but I´d like to know if there is something quicker because I receive data from 1 to 3 digits and I´m trying to write the less lines possible.

Thanks to all of you again.

Armando Hdez.

paul borgmeier
- 25th June 2006, 14:36
Armando,

I believe “str” it is referring to an array string not a string string. You should have something like this as the variable declare. (Let's change your variable name from VAR to RX_Var since VAR is reserved)

RX_Var var byte[4]

Then with your call, if your incoming data looks like this

49 ' ascii 1
50 ' ascii 2
51 ' etc.
52
53
54


hserin [skip 2, str RX_Var\4 \32 ]

Your data captured data will look like

RX_Var[0]=51
RX_Var[1]=52
RX_Var[2]=53
RX_Var[3]=54

If you need more help or suggestions, let us know ...

Paul Borgmeier
Salt Lake City, Utah
USA

Armando Herjim
- 27th June 2006, 05:09
Ok thank you! You´re right I have an array but then you are telling me there´s another way to get a string of characters serialy?? What do you mean with a "string string"?? Thank you very much I really appreciate it!

Armando.

paul borgmeier
- 27th June 2006, 07:10
... What do you mean with a "string string"?? ...

Poor choice of words on my part – there is no string variable in PBP. i.e., you cannot have RX_Var = “Hi There” in PBP even though it is allowed in most traditional BASIC programs if RX_Var is declared as a string variable (See Visual Basic for example). You can have

X[0]=”H”
X[1]=”i”
X[2]=” “
etc.

You have a few choices from what I can see. You can make sure you always send and receive 4 digits of a decimal number and use DEC as Steve suggested. Or you could “roll your own” with your array values: (highly unoptimized but should get the point across)

NewValue VAR WORD

RX_Var[0]=51
RX_Var[1]=52
RX_Var[2]=53
RX_Var[3]=54

IF RX_Var[0] = “ “ THEN GoSomeWhere
NewValue = RX_Var[0] - “0”

FOR X = 1 to 3
IF RX_Var[X] = “ “ THEN GoSomeWhere
NewValue = (NewValue * 10) + RX_Var[X]-”0”
NEXT X

You also can probably do it with all the options of SERIN2 but I have not used them enough to be able to suggest the format. Someone else?

Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA

paul borgmeier
- 27th June 2006, 07:17
A quick after thought - why not just send and receive your data a low byte and a high byte of a word sized variable and be done.

For example

X var word

X = 456 ' your "desired" value

send
X.lowbyte = 200
X.highbyte = 1

then X will be 456 - done.

Paul

Armando Herjim
- 28th June 2006, 01:54
Hey that seems to be an excellent idea. Thank you very much!!!