Serial Ports in VB.NET

An example of asynchronous serial port data reception in VB.NET with StrokeReader ActiveX.

1. Install StrokeReader.

2. In Visual Studio, create a new VB application. Select File->New Project from the main menu and choose Templates->Visual Basic->Windows and Windows Forms Application in the New Project dialog as shown on the picture below.

Creating a new VB Forms application

3. In the workspace, switch to the Toolbox (if you don't see it, execute View->Toolbox). Add a new tab into the Toolbox - right-click anywhere on the panel and execute Add Tab from the context menu. Open the context menu for the tab you have just created and select Choose Items.

Creating a new tab in the Toolbox

4. Switch to the COM Components tab and select the StrokeReader Control:

A list of available ActiveX controls

5. Drag the ActiveX to the form:

A serial port ActiveX on the form

6. Execute View->Properties Window to show the serial port properties. Specify the desired port number in the Port field. To bind the ActiveX to COM3, set Port=3. The PortsAvailable property contains a list of available serial ports, USB-VCP ports and USB modems.

Setting the serial port properties

To probe the serial port, set Connected=True. If, after that, the property value stays True and the ErrorDescription property doesn't show any errors, the ActiveX is connected to the port and the port settings are accepted by the device driver.

Attention: before proceeding, set Connected to False because the debugger will launch another copy of the ActiveX while the form designer window keeps the first ActiveX instance active and the port busy. The another way is to close the form designer window before running the project.

7. Double-click the ActiveX on the form and paste the following code into the editor:

You need to update StrokeReader ActiveX to the latest version to use this example. The code is compatible with v2.6 and newer.

Imports EVT = StrokeReaderLib.Event Imports DM = StrokeReaderLib.DataMode Public Class Form1 Private Sub AxStrokeReader1_CommEvent _ (sender As Object, e As AxStrokeReaderLib._DStrokeReaderEvents_CommEventEvent) _ Handles AxStrokeReader1.CommEvent Select Case e.evt Case EVT.EVT_DATA Dim s As String If True Then s = AxStrokeReader1.Read(DM.TEXT) ' Use this code if you need a text string MsgBox(s) Else Dim x As Byte() = AxStrokeReader1.Read(DM.BINARY) ' Use this code if you need a byte array For i = 0 To x.Length - 1 s = s & Hex(x(i)) & " " Next MsgBox(s) End If Case EVT.EVT_CONNECT MsgBox("Connected") Case EVT.EVT_DISCONNECT MsgBox("Disconnected") End Select End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AxStrokeReader1.Port = 3 ' you need to choose a correct port number yourself (here or in the form designer) AxStrokeReader1.BaudRate = 115200 ' check with your hardware's user guide for correct data speed AxStrokeReader1.Connected = True ' we assume you have set Connected=False in the form designer End Sub End Class

Serial port settings (like Baud rate or number of stop bits) can be changed both in the design and in the code.

Always check with documentation provided with your hardware for the required data speed, start/stop bits configuration, CTS/RTS handshaking and data exchange protocol description.

Always keep in mind data reception timeouts - some packets may be received by parts with multiple CommEvent calls. You may need additional programming to build the entire message form these parts.