Reading and writing hex to rs232

I’m probably a few levels below newbie…
Could anyone out there provide me with code to read and write hex to a serial port? I’m trying to figure out what the heck i’m doing wrong.
If bounty required just let me know. I really want to understand how this works.

I need to send hex string and end with “unsigned char CheckSum (unsigned char * uBuff, unsigned char uBuffLen)”

Thanks in advance

do you need to send a string of HEX characters? or just a string of BYTES (huge difference)

the string “ABCD” is 4 bytes of data
but a HEX “STRING” for the same would be “41424344” or 8 bytes of data
a lot of people misunderstand the difference between the data “content” and the “human readable”.

Usuallly a datastream would be displayed in HEX to the user because there is data that is NOT alphanumeric (0x9A for example), so it is easier to look at it this way. where in reality the DATA moving down the serial path is not that way.

Here is an example:
A total of 17 bytes Tag Data: (The following values are hexadecimal)
00 00 E3 00 60 19 D2 6D 1C E9 AA BB CC DD 01 51 FF
Of which:
00: the head sign , this is fixed
00: Device Number
E3 00 60 19 D2 6D 1C E9 AA BB CC DD: ID No.
01: antenna number, from which the antenna of this recognition Note: (integrated antenna is fixed)
51: checksum, calculates: From first byte to from the bottom the third byte, altogether 15 bytes
FF: flag, this is fixed

Ok… you have 17 “BYTES” of data (again HEXADECIMAL is for YOUR convience not the computers)

E3 is a single byte of data… to the computer its a series of “1” and “0” [11100011]
or it might be represented as a Decimal Number in which case it is 227
or an Octal Number where it would be 343.

But to the computer is the same thing…

Are you trying to figure out how to “package” the data? If so, there is nothing you need to do… send the raw data.

Or are you trying to figure out how to send data thru a serial port?

This is two different questions, one is software related, the other is hardware (and a bit of software)

Packaging more-so, I’ve done serial communication in the past and I think I have all configured properly in Xojo.
So could I just use serial.write (“0000E3006019D26D1CE9AABBCCDD0151FF”)
If so I think I tried that…

I’m now concerned it may be the old usb to serial adapter I am using that is causing me grief.

[quote=179151:@Jason Hwang]Packaging more-so, I’ve done serial communication in the past and I think I have all configured properly in Xojo.
So could I just use serial.write (“0000E3006019D26D1CE9AABBCCDD0151FF”)
If so I think I tried that…

I’m now concerned it may be the old usb to serial adapter I am using that is causing me grief.[/quote]

No…serial.write (“0000E3006019D26D1CE9AABBCCDD0151FF”)
writes out 34 bytes of data, and it does NOT represent what you said above

the first byte is “0” which is “30” in hex, the last byte it “F” which is “46” in hex

you are still getting hung up on “HEX” vs “DATA”

this is not the BEST example… but it might help

dim s as string="0000E3006019D26D1CE9AABBCCDD0151FF"
for i as integer=1 to len(s) step 2
   x=val("&H"+mid(s,i,2)) // convert from human readable HEX to actual data value
   serial.write(x) /// or maybe it should be serial.write(chrb(x))
next i

perhaps someone with more knowledge about “serial.write” could chime in

You could try

serial.write DecodeHex(0000E3006019D26D1CE9AABBCCDD0151FF)

which will turn the hex back into raw data.

[quote=179152:@Dave S]

dim s as string="0000E3006019D26D1CE9AABBCCDD0151FF"
for i as integer=1 to len(s) step 2
   x=val("&H"+mid(s,i,2)) // convert from human readable HEX to actual data value
   serial.write(x) /// or maybe it should be serial.write(chrb(x))
next i

perhaps someone with more knowledge about “serial.write” could chime in[/quote]

I use chrb when converting hex values to ASCII. However, I think Wayne’s use of the DecodeHex function is probably better and easier still.

The key to understand is that the serial port writes a string which is ASCII based (OK - well UTF8 which is a child of ASCII)…,

So the character “F” is Hex value 46 which is decimal value 70. Hex value F is the Shift-In character which is Decimal value 15. Take a look at www.asciitable.com. You need to convert the HEX column to the chr column.

DecodeHex is a good shortcut. Another alternative would be to contstruct the string using ChrB

dim s as string = chrb(0) + chrb(0) + chrb(&hE3) + chrb(0) + chrb(&h60) + …
serial1.write s

As you are sending binary data, then I believe you should make sure you have the comport set to .XON = false, so that any bytes containing control+S do not freeze communication.