Reading from a Serial Port Part II

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
 
J

John Wright

Okay I am one step closer to this. I can now read the HEX format of this
string. I get the following:

53 00 7E AA 80 80 80 80 80 80 80 00 80 80 80 80 00 80 80 80 80 82 80 00 80
80 80 80 00 80 80 80 82 80 88 A0 00 80 80 BE 80 00 80 90 80 82 80 80 84 00
80 80 80 45 0D 0A 00

I can split this data on a space, now I want to convert each value to its
binary equivalent. Ignoring 53, 00, 7E which are the opening sequence, and
ignoring oD, 0A and 00 as the ending sequence, I would like to convert as
follows (I did these calculations in my calc) and ignoring the first two
bits:

BE = 10111110 Which would indicate fields 1-5 are selected
AA= 10101010 Which would indicate fields 1,3,5 are selected
etc.

Anyone have a routine that would convert from HEX to Binary so I can get the
string value of the binary?

Thanks.

John
 
A

AlexS

You can use this sample. No other routine is required.

For example, with

string s = "AA";

if (Int32.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
out result)) {

....

}
 
J

John Wright

How does this translate to VB.NET?

John

AlexS said:
You can use this sample. No other routine is required.

For example, with

string s = "AA";

if (Int32.TryParse(s, NumberStyles.HexNumber,
CultureInfo.InvariantCulture, out result)) {

....

}
 
D

Dick Grier

Hi John,

I suggest that you read the data as binary (buffer in an array of type
Byte). Then parse the data out of that array using your own routine.
Mixing binary and ASCII (text) data is problematic -- at the end of the day,
it is best to treat it as pure binary.

However, to answer your specific question (example)...

Debug.Print(Cbyte("&H" & ASCIISplitBuffer(someindex)).ToString)

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
J

John Wright

Here are the two functions I use to read the serial port data. I read in
the data to a byte array and then convert it to hex values. I then convert
the byte array to hex values and now I want to convert from HEX to binary.
Is there a quicker way to get from the byte array to binary? The problem I
see is that I need to igore the first and last of the string and only
convert the middle portion after the S~ at the start and the B (chr10,
chr13) added at the end. Here are the two functions for reference:

John
Public Sub UPdateTextBox()

Dim bytes As Integer = serialPort.BytesToRead

Dim buffer(bytes) As Byte

serialPort.Read(buffer, 0, bytes)

With TextBox1

..AppendText(ByteArrayToHexString(buffer))

End With

End Sub

Private Function ByteArrayToHexString(ByVal data() As Byte) As String

Dim sb As New System.Text.StringBuilder(data.Length * 3)

For Each b As Byte In data

sb.Append(Convert.ToString(b, 16).PadLeft(2, "0"c).PadRight(3, " "c))

Next

Return sb.ToString.ToUpper

End Function
 
D

Dick Grier

Hi,

A Byte array already IS binary.

So, I presume from what you have written, what you really want is an ASCII
hexadecimal string that represents the binary data in a more human-readable
form?

At any rate, in answer to your question. I use something like this:

Data(I).ToString("X2") & " "

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Serial Port Help 8
Opening COM port Error 3
Serial Port Timer 4
Debugging Error 1
Serial Port Question 1
Tutorial, what have I missed ? 6
Serial Port / RS232 5
Help converting a vb6 function to vbnet. 8

Top