Read from COM port?

  • Thread starter Thread starter Olav Tollefsen
  • Start date Start date
O

Olav Tollefsen

I need to read messages from a device connected to a COM port with the
following format:

When the controller is configured to report events, it will transmit a new
message every time the event
occurs. The message follows the following format:

XX <Message> {CR} {LF} {Terminator}

.. "XX" is the Message ID Number. Each type of report message from the
controller has a unique ID
number in a 2-digit hexadecimal format. Each digit will be a hexadecimal
value (numbers 0-9 or
letters A-F). When converted to decimal format, they represent the numbers 0
through 255. By
reading these two bytes, a user program can quickly identify the type of
message from the
controller.
.. "<Message>" will contain a text description of the event.
.. {CR} signifies that a carriage return is sent.
.. {LF} signifies that a line feed is sent.
.. {Terminator} signifies that a single-byte terminator code is sent. The
byte has a value 1d, which is
the ASCII code SOH (this is NOT the number 1, which is ASCII value 49d). The
terminator is
provided to make it easier for a computer program to read and interpret the
messages from the
controller.

What framework clases are best suited for reading this kind of messages from
the device? Can you specify the terminator character which is at the end of
each message?
 
Just to give you a bit more background. I have found a SerialStream class,
so I'm able to read characters from a COM port, but I have not found a way
to let the read wait until CR/LF. I'm using BinaryStream. If I try to use
StreamReader.ReadLine, it doesn't return from the read at all.

Olav
 
Hi,

There are no Framework classes for reading serial data. There are several
available online, and you can use other add-ons for the purpose.

After that, you have to buffer up a complete packet, and parse out the
message. This is straight forward. I have lots of examples of this sort of
thing in my book. See below. I can give you VB .NET pseudo-code.

Let us suppose that we are using NETComm (download from my homepage) for the
serial interface. Then, if we enable OnComm receive processing (.Rthreshold
= 1) then,

Private GS As String = Chr(&H1D) 'Group Separator

Private Sub AxNETComm1_OnComm(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles AxNETComm1.OnComm



Static Buffer As String

Dim Message As String

'--- Branch according to the CommEvent Prop..

Select Case AxNETComm1.CommEvent

'--- Event messages

Case NETCommOCX.NETCommConstants.NETComm_EV_RECEIVE

If InStr(Buffer, vbCrLf & GS) Then

Message = Mid(Buffer, Buffer.Length - 3)

'do something with Message here

Buffer = ""

End If

If Then

End If

End Select

End Sub

You can do the same sort of thing other classes, though the details will
vary.

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
So the best method is to have a non-blocking read with an event handler that
gets called when data is available, rather than doing a loop with
"ReadLine"?

Olav
 
Hi,
So the best method is to have a non-blocking read with an event handler that
gets called when data is available, rather than doing a loop with
"ReadLine"?
<<

I often use a loop... In a separate worker thread, with event notification
to the calling thread. This provides good response, combined with a simple
structure. This is the technique in my CFSerialIO class (for the Compact
Framework).

Another way is to use the WaitCommEvent API to provide notification via a
callback. This is the "standard" method... And it is the underlying method
that is used in the MSComm32.ocx ActiveX control and my NETComm.ocx
derivative. This allows receive event notification, too.

A third way is to use a Timer to poll a non-blocking call to the selected
read method (I use a binary reader and the ReadBytes method, not ReadLine --
which is too restrictive for general purposes). I use this technique in the
class that is in my book.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Back
Top