Type conversion issue
Posted: 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
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