Programming Methods -Using the Visual Basic SerialPort Class-

Controlling a serial port in Visual Basic

To control a serial port in Visual Basic, there is a method to use the .NET Framework SerialPort class and a method to use the Win32 API. Here we will introduce a programming example that sends and receives data using the relatively simple SerialPort class.

.NET Framework SerialPort (SerialPort class)

A COM port class (SerialPort component) has been added to the .NET Framework from Ver. 2.0. Visual Basic 2005 or later can use the .NET Framework SerialPort component which allows relatively simple control of a COM port. Please note that you can no longer use the MSComm control which had been often used in previous versions such as Visual Basic 6.

.NET Framework SerialPort features

  • Settings for connecting to serial ports
  • Command transmission (control of various serial interface control signals such as RTS and CTS and status input)
  • Send data
  • Various events during serial connections and error monitoring and handling (events may be generated when there are status changes in control signals and when errors occur during communication)
  • Settings for connecting to serial ports

.NET Framework SerialPort programming points

  • Data can be written and read that includes NewLine (further expressed as "delimiter code" (terminator, separator) for convenience).By adding the delimiter code, it can be automatically judged as the separator for consecutive data. If "WriteLine" is used when sending, the specified delimiter code is automatically added to the data string.
  • Bidirectional processing is possible with interrupt (event-driven) processing.With "DataReceived" and "PinChanged" events, notification can be received at the moment events occur such as changes in control signal lines and receiving data. Interrupts occur when events occur, so the necessary processing can be promptly performed.

Paste the SerialPort component

Select the SerialPort component on the Visual Basic "Toolbox", left-click and drag and drop the component to paste it on the form. When the component is finished being pasted, the component appears pasted under the form.

Configure the properties

Click the component that has appeared to switch to the component's properties window. With the "PortName" property, set the port number that will be used for communication.The initial value is "COM1".With the "BaudRate" property, set the transmission rate.The initial value is "9600". You can also configure the other properties such as whether to use RTS.

Event settings can also be configured

Click the event button on the property window to display a list of the component's events. Select and double-click the event to use and the corresponding event routine is added. Write the code for the processing when the event has occurred in this location.

Event types

DataReceived event
When characters are received and data is stored in the receive buffer
When EOF (end-of-file) is received and this is stored in the receive buffer
ErrorReceived event
Framing error detected
Overrun error detected
Buffer overflow detected
Parity error detected
When the send buffer is full and data cannot be stored in the buffer
PinChanged event
Break signal detected
CD (Carrier Detect) status changed
CTS (Clear to Send) status changed
DSR (Data Set Ready) status changed
Ring indicator detected

Controlling a serial port with the Win32 API

For serial port programming, there is also a method that calls the Wind32 API (application programming interface). Extensive knowledge of the Win32 API and programming languages is required, but it makes more advanced and flexible programming possible.

The Win32 API is the feature (function) set that Windows provides as standard.It is provided as DLLs and any application that runs on Windows can share and use it. Windows provides its primary functionality as around 1000 types of API functions, and application software is built combining this functionality. Visual Basic and Visual C have many commands, but this does not mean that it can use all of the Windows functionality, so you should call and use the API functions as necessary. Use the following API functions to control serial ports.

Win32 API used to control serial ports

Open a port
Configure a port
SetCommState, SetCommTimeouts
Send data
Receive data

To PageTop