Page 1 of 1

Type conversion issue

Posted: Wed Feb 15, 2012 4:17 pm
by Ondra
Good day all. I need to get some in-site in to a situation I don't understand.
I have 2 devices that are communication via RS232, I'm using a get Char to capture the values as they are passed in.
Implementing an error checking algorithm. I'm building the string on the sending device enclosing the data in <> exp. <8MRD12340> the first Char is
the length of the data. On the receiving end I'm indexing the bytes into a string variable then comparing the indexed length with the first Char. I have
confirmed that all the value are being captured. I confirmed this by sending the captured values out a second RS232 port connected to my PC. The
problem is though I have confirmed the Char held in var "dataPacketLen" is correct. When I do a comparison of the value held in "dataPacketLen"
and "idx" they don't don't compare. Also I converted the value in dataPacketLen to a string then sent the value to the PC and the value was 56. If I
send the same byte without conversion it prints out as 8, which is correct. I tried converting both to string and they don't compare. I tried without
conversion and they don't compare. I'm assuming my approch is wrong and If so, how do I compare a received Char vaue with an int value?

The testing value being passed in is <8MRD12340>

dim AS byte datapacketSw = 0
dim AS int dataPacketLen
dim AS string str_data
dim AS Byte Rec
dim AS int idx = 0

While 1

ConnetionPoint[C]

Rec = RX_RS232::RX_char(20)
If Rec < 255 {
IF datapacketSw = 1 {
dataPacketLen = Byte // capture the len of sting for comparison
dataPacketSw = 0
GoTo ConnetionPoint[C]
}
IF byte = "<" { // if data packet begin, datapacketSw = 1 to capture the length Char
datapacketSW = 1
GoTo ConnetionPoint[C]
}
IF Byte = ">" AND (Idx = dataPacketLen) { // If end of string ">" compare (Idx = dataPacketLen) value
str_data[idx] = 0
idx = 0
Else
idx = 0 // reset the index disreguard the data
}

str_data[idx] = byte
idx = idx + 1
}
end while

Re: Type conversion issue

Posted: Wed Feb 15, 2012 5:22 pm
by Benj
Hello,

You are sending the data as ASCII which means that sending the character '8' will equal 56 decimal.

http://www.asciitable.com/

When you are receiving numbers you can convert from an ASCII char back to decimal by doing this.

dataPacketLen = Byte - '0'

Which subtracts the ASCII value for 0 (48 decimal) from your variable to give you a valid byte count.

Alternatively you could send the data in non ASCII format and this will then work on your receiver as is. Using RealTerm you can transmit decimal numbers as bytes so this could be an option for you while testing.

Re: Type conversion issue

Posted: Thu Feb 16, 2012 2:20 pm
by Ondra
Thank Benj.
Now this is why I like Flowcode. Great product and even better support.
Thanks for the RealTerm tip.
Another question. How do you send in non ASCII format?

Ondra