Serial Communication

jimctr

Well-known member
Joined
Dec 1, 2011
Messages
52
Programming Experience
Beginner
I want to take a number such as 600 (0x0258) and send these hex values to a C-based system which is assigning char's to the received bytes and rebuilding to the original number.

Currently, I am converting to bytes in VB.Net but it is not giving me the expected outcome (because of the ascii nature of the transfer most likely)

Dim btData() As Byte
Dim TempShort As UShort

TempShort = btData.Length
bytes = (BitConverter.GetBytes(TempShort))
myComPort.Write(bytes, 0, bytes.Length)

I've also tried using Hex but this doesn't get the data in the correct format. Can anyone help?

Thanks
 
The solution I thought worked does not work because I use Chr(). Chr works great as long as you do not exceed the ascii table, but what happens when you want to transmit a number like 132?


Is there no way to transmit Hex directly without going through ascii? This is where I am getting burned. If I use the following:

myComPort.Write(&H0)
myComPort.Write(&H84)

on the receive side, these values translate to '0' and '1', which are 0x30 and 0x31, respectively. Garbage. Where do these come from??
If I use the following method,

DataLength = PrependVal(Hex(132))

For index As Integer = 0 To 2 Step 2

hexText = DataLength.Substring(index, 2)
value = Convert.ToByte(hexText, 16)
numData1(index \ 2) = value

Next

and then

myComPort.Write(numData1(0)) 'Zero is the MSB
myComPort.Write(numData1(1)) 'One is the LSB

This has the same effect as #1 above

Only if I do a

myComPort.Write(Chr(numData1(0))) 'Zero is the MSB
myComPort.Write(Chr(numData1(1))) 'One is the LSB

this does work until my value gets larger than 127, at which point I run off the ascii table.

On the receiving side, my byte values I store the transmitted data in are of type char.
 
Last edited:
because of the ascii nature of the transfer most likely
SerialPort.Encoding defaults to Ascii, and can be changed to a different encoding. When sending and receiving strings and chars that encoding is used to convert to/from bytes internally, it does not apply to sending/receiving raw bytes.
but what happens when you want to transmit a number like 132
If the protocol requires chars you would convert it to a string and send that. A string is a sequence of chars. So a port.Write("123") would send the chars '1', '2' and '3' using the relevant byte encoding.
Is there no way to transmit Hex directly without going through ascii?
Hex is a (16base) string representation of a number. You can convert various data to bytes and send that, but you should be aware of what receiver expects. For example if receiver expects string/char data and you send the byte for integer number 1 (which is byte value 1), that is wildly different from the byte value of char "1" which has byte value 49 in ascii encoding.
 
Hi John:

I am not using the built-in SerialPort Object. I read in a different post someone ran into the same issue I ran into (could not transmit recognizable characters past the ascii table value of 127) so ended up using VB's built in SerialPort object to be able to transmit in hexadecimal. Since I am implementing a virtual serial port, USB derived, I'm not certain how the built-in SerialPort object will respond (possibly the same but I will need to change a great deal of my code)

SerialPort.Encoding defaults to Ascii, and can be changed to a different encoding.

Are you referring here to the built-in SerialPort object or one define outside of that schema? How to you change back and forth between transmission of hex and ascii?

Thanks
 
OK,

Now that you have given me a clue that this is an encoding issue, I was able to find a slew of posts relating to this very problem. Thanks
 
Back
Top