Type conversion issue

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Type conversion issue

Postby Ondra » Wed Feb 15, 2012 4:17 pm

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

User avatar
Benj
Matrix Staff
Posts: 14370
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4447 times
Been thanked: 4145 times
Contact:

Re: Type conversion issue

Postby Benj » Wed Feb 15, 2012 5:22 pm

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.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Type conversion issue

Postby Ondra » Thu Feb 16, 2012 2:20 pm

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