473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reading from a Serial Port Part II

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.SerialPortNa mes(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
Jul 17 '07 #1
6 11861
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
"John Wright" <ri***********@hotmail.comwrote in message
news:On**************@TK2MSFTNGP03.phx.gbl...
>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.SerialPortNa mes(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

Jul 17 '07 #2
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)) {

....

}

"John Wright" <ri***********@hotmail.comwrote in message
news:eT*************@TK2MSFTNGP02.phx.gbl...
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
"John Wright" <ri***********@hotmail.comwrote in message
news:On**************@TK2MSFTNGP03.phx.gbl...
>>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.SerialPortNa mes(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


Jul 17 '07 #3
How does this translate to VB.NET?

John

"AlexS" <sa***********@SPAMrogers.comPLEASEwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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)) {

....

}

"John Wright" <ri***********@hotmail.comwrote in message
news:eT*************@TK2MSFTNGP02.phx.gbl...
>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
"John Wright" <ri***********@hotmail.comwrote in message
news:On**************@TK2MSFTNGP03.phx.gbl...
>>>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.SerialPortNa mes(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



Jul 18 '07 #4
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.
Jul 18 '07 #5
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
"Dick Grier" <dick_grierNOSPAM@.msn.comwrote in message
news:Ol*************@TK2MSFTNGP02.phx.gbl...
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.

Jul 18 '07 #6
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.
Jul 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
5
by: Franklin M. Gauer III | last post by:
Hi All, I've written an ASP.NET application (webservice) that does simple serial communications via the .NET 2.0 SerialComm object. The application runs fine on my development machine. The...
7
by: davetelling | last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things work they way I want. Using Visual C# Express, I have a form in which I added a user control to display a graph, based...
8
by: Vivek Menon | last post by:
Hi, I am using a C program to write/read from a serial port. The writing part is working perfectly fine. However, I am not able to read the values correctly and display them. To debug this issue I...
0
by: RG | last post by:
I am trying to read from my serial port a 24 bit binary number. I was able to read this number as a HEX but I was getting errors as at times using the vBCrLf indicator. I also can read it as an...
4
by: rowan | last post by:
I'm writing a driver in Python for an old fashioned piece of serial equipment. Currently I'm using the USPP serial module. From what I can see all the serial modules seem to set the timeout when...
0
omerbutt
by: omerbutt | last post by:
Sir, i am working in KONICA MINOLTA (Pakistan).i am working on a project in which we are managing the spare parts store room.The idea is to make a databse of the machine parts, and their...
2
by: colin | last post by:
Hi, Im having a tiresome amount of trouble with using a bluetooth serial link. The receiving end is a bluetooth-rs232 module conected to my embeded system. The PC has a little usb bluetooth...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.