Re: Reading from a Serial Port Part II

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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" <riley_wrightx@xxxxxxxxxxx> wrote in message
news:eTpgHnLyHHA.140@xxxxxxxxxxxxxxxxxxxxxxx
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" <riley_wrightx@xxxxxxxxxxx> wrote in message
news:OnixC$KyHHA.5484@xxxxxxxxxxxxxxxxxxxxxxx
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





.



Relevant Pages

  • Re: Reading from a Serial Port Part II
    ... Anyone have a routine that would convert from HEX to Binary so I can get the ... string value of the binary? ... just fine and can receive data fine in text mode but not in binary mode. ... Private Sub Form1_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Reading from a Serial Port Part II
    ... How does this translate to VB.NET? ... the string value of the binary? ... just fine and can receive data fine in text mode but not in binary mode. ... Private Sub Form1_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Reading from a Serial Port Part II
    ... fine and can receive data fine in text mode but not in binary mode. ... Dim WithEvents serialPort As New IO.Ports.SerialPort ... Private Sub Form1_Load(ByVal sender As System.Object, ... Private Sub Button1_Click(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)
  • Re: FTP CD command
    ... My code connects to a ftp site and the enumerates all content on that place ... Public Property UriAs String ... End Sub ... Dim listRequest As FtpWebRequest = CType, ...
    (microsoft.public.dotnet.languages.vb)