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 6 11723
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
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
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
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |