J
John Wright
I am trying to read the data from a device on a serial port. I connect just
fine and can receive data fine in text mode but not in binary mode.
In text mode the data from the device comes in like this:
S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE
S is the start of the line, ~ indicates a good read, B is a blank reading,
and the numbers 1-6 correspond to a location on the device for a line. So
there are six positions on the line and only 1 of 6 can be selected.
However, you can switch it to binary mode to read lines of data in so that
each line can have more that one position read. bit 7 is always 1 and bit 6
is always 0. Bits 5 to 0 are 1 or 0 depending on if they are selected. So
if I select three positions (position A,B and E) then the reading for that
line should be (translated to binary) 10110010 or hex 0xB2.
When I process this through my VB.net program I get the following line (in
text)
S~???????????????????????????????????????????E
So I switch to hyper terminal and get the following line:
S~,???????????????,????????,?^ ??"???,??"???E
My code to read from this device is very simple:
Dim WithEvents serialPort As New IO.Ports.SerialPort
Public Delegate Sub myDelegate()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = ComboBox1.Text
.BaudRate = 19200
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
serialPort.Open()
Catch ex As Exception
End Try
End Sub
Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf UPdateTextBox), New Object()
{})
End Sub
Public Sub UPdateTextBox()
With TextBox1
.AppendText(serialPort.ReadExisting)---> Reads fine for text mode, but
does not work for binary mode
End With
End Sub
I can detect if the device is in binary mode or text mode. What I want to
do is switch from text mode to binary mode so I can read the binary fields
and translate them. In the first line above:
S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE
Every field between the ~ and the E are for one row of data. So instead of
getting a 5 from the first position (this indicates that position 5 was
selected) I want to get 10000010, B = 10000000.
Any help would be appreciated.
John
fine and can receive data fine in text mode but not in binary mode.
In text mode the data from the device comes in like this:
S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE
S is the start of the line, ~ indicates a good read, B is a blank reading,
and the numbers 1-6 correspond to a location on the device for a line. So
there are six positions on the line and only 1 of 6 can be selected.
However, you can switch it to binary mode to read lines of data in so that
each line can have more that one position read. bit 7 is always 1 and bit 6
is always 0. Bits 5 to 0 are 1 or 0 depending on if they are selected. So
if I select three positions (position A,B and E) then the reading for that
line should be (translated to binary) 10110010 or hex 0xB2.
When I process this through my VB.net program I get the following line (in
text)
S~???????????????????????????????????????????E
So I switch to hyper terminal and get the following line:
S~,???????????????,????????,?^ ??"???,??"???E
My code to read from this device is very simple:
Dim WithEvents serialPort As New IO.Ports.SerialPort
Public Delegate Sub myDelegate()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = ComboBox1.Text
.BaudRate = 19200
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
serialPort.Open()
Catch ex As Exception
End Try
End Sub
Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf UPdateTextBox), New Object()
{})
End Sub
Public Sub UPdateTextBox()
With TextBox1
.AppendText(serialPort.ReadExisting)---> Reads fine for text mode, but
does not work for binary mode
End With
End Sub
I can detect if the device is in binary mode or text mode. What I want to
do is switch from text mode to binary mode so I can read the binary fields
and translate them. In the first line above:
S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE
Every field between the ~ and the E are for one row of data. So instead of
getting a 5 from the first position (this indicates that position 5 was
selected) I want to get 10000010, B = 10000000.
Any help would be appreciated.
John