RS-232 String Casting Problems

son_goku

New member
Joined
Jan 24, 2008
Messages
3
Programming Experience
Beginner
Hi,
I have an external device transmitting a repeating comma deliminated hex string through the RS-232 port of my computer.

The specific data frame looks like this:
"03 FF 2C 03 FF 2C" ["2C" is the delimiting comma so "3ff,3ff,"]

Essentially, What I'm trying to do is display 1023 in a respective textbox control. since 3FF = 1023 (hex to decimal conversion)

If place a textbox control on my application with the following code:
TextBox1.Text = CType("&H3ff", Integer) I can indeed see that 3ff = 1023


If I place a textbox control on my application with the following code:
VB.NET:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
  ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim values As String
    Dim vArray As Array

    Dim sHEX As String
    Dim iHex As Integer

    values = SerialPort1.ReadExisting
    vArray = Split(values, ",")

    sHEX = ("&h" + vArray(0))
    iHex = CType(sHEX, Integer)

    TextBox1.Text = iHex

End Sub
I receive the following error:
Conversion from string "&h ?" to type 'Integer' is not valid.


If I run the code without a cast conversion I see jibberish (which makes sense) in the text controls:
specifially "?" in both text fields.
VB.NET:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
  ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim values As String
    Dim vArray As Array

    values = SerialPort1.ReadExisting
    vArray = Split(values, ",")

    TextBox1.Text = vArray(0)
    TextBox2.Text = vArray(1)

End Sub
What am I doing wrong?

Thanks for any help!
Ken Ryder
 
Last edited by a moderator:
You've probably set up your SerialPort incorrectly. I was reading data from a scale in an app a while back and it came through incorrectly with the default property values for the SerialPort. Use HyperTerminal to connect to the physical port and check the configuration, then set up your SerialPort object to match. You may also need to adjust the Encoding property and I'm not sure HT will help you there. You can just try each one until you find one that works if the default doesn't.

Also, this:
VB.NET:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
  ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim values As String
    [B]Dim vArray As [U]Array[/U][/B]

    Dim sHEX As String
    Dim iHex As Integer

    values = SerialPort1.ReadExisting
    vArray = Split(values, ",")

    [B]sHEX = ("&h" [U]+[/U] vArray(0))[/B]
    iHex = CType(sHEX, Integer)

    TextBox1.Text = iHex

End Sub
should be this:
VB.NET:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
  ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim values As String
    [B]Dim vArray As [U]String()[/U][/B]

    Dim sHEX As String
    Dim iHex As Integer

    values = SerialPort1.ReadExisting
    vArray = Split(values, ",")

    [B]sHEX = ("&h" [U]&[/U] vArray(0))[/B]
    iHex = CType(sHEX, Integer)

    TextBox1.Text = iHex

End Sub
Don't use the Array type explicitly when you know exactly what type of array it is. That is only when you want to be able to refer to different types of arrays.
 
Thanks for the reply!

I've modified my array problem and fixed the concantenation, however, I still receive the same error on this line: iHex = CType(sHEX, Integer)

"Conversion from string "&h ?" to type 'Integer' is not valid."

I've attached my serialport properties along with a snapshot of the incoming hex string using a Procomm terminal. I am pretty confident the data is indeed 9600 8-N-1.... Anymore ideas?

Thanks!
Ken Ryder
 

Attachments

  • dataread.PNG
    dataread.PNG
    36.3 KB · Views: 49
"&H" is a prefix used on integer literals to indicate hexadecimal format. That is it's ONLY use. You can't convert a string with an "&H" prefix to an integer. If you have a string containing hexadecimal digits then you would convert it to an Integer like so:
VB.NET:
myInteger = Convert.ToInt32(myString, 16)
 
What you are getting there is byte values, not a string. The Procomm image you posted display the hex values of the byte sequence, still not a string. (try a number from 0 to 255 and do Tostring("X2") and you'll get the picture)

What SerialPort class does when you read the data stream as a string is to decode it as Ascii characters, which is one character each byte. Since the Ascii character set only supports the range 0-63 it substitutes larger values with unrecognized '?' character, you get
03 (3) a non-printable end-of-text, a square sign in some cases
FF (255) out of range, a '?'
2C (44) a comma

Why you want to read first two bytes 3 and 255 as a Short (Int16) and interpret the next byte as a character "," is beyond me. But anyway you should be working with the byte data and not a decoded string. Also remember Intel processor machines use Little-Endian byte order so the Short number 1023 is represented by bytes FF and 03, and not 03 and FF. Look for example into BitConverter.GetBytes(CShort(1023)).

Another example, you have byte array (3,255) and get the short value, then switch from Big-Endian Network order to Little-Endian host order:
VB.NET:
Dim b() As Byte = {3, 255}
Dim s As Int16 = BitConverter.ToInt16(b, 0) 's=-253
s = Net.IPAddress.NetworkToHostOrder(s)) 's=1023
 
JohnH,
Your explanation (and code) was EXACTLY what I needed to get this thing up and running!!! I thank you both for your responses!!

Ken Ryder
Mankato Minnesota
 
Back
Top