Skip to content

cSerialPort Quick Start

📖 Table of Contents


Reference Methods

Method 1: Source Code Reference

Add the three files under src/SerialPort to the VB6 project:

modSerialPortAPI.bas   # API declarations, constants, types, helper functions
cSerialConfig.cls      # Configuration class (includes eBaudRate/eParity/eStopBits enums)
cSerialPort.cls        # Serial port main class (includes eCommEvent/eCommError/eModemStatus enums)

Method 2: DLL Reference

Reference dist\DLL\VBMAN.dll, use with namespace prefix:

vb
Dim sp As New VBMANLIB.cSerialPort

Minimal Example

Open COM3, send text and read response:

vb
Dim sp As New cSerialPort
sp.PortName = "COM3"
sp.Config.BaudRate = br9600

If sp.OpenPort() Then
    sp.WriteText "Hello"
    Debug.Print sp.ReadExisting()
    sp.ClosePort
End If

Common Scenarios

1. Event-Driven Continuous Send/Receive

Declare object with WithEvents, receive data automatically through events:

vb
Private WithEvents m_Port As cSerialPort

Private Sub Form_Load()
    Set m_Port = New cSerialPort
    m_Port.PortName = "COM4"
    m_Port.Config.BaudRate = 9600
    m_Port.Config.DataBits = 8
    m_Port.Config.Parity = 0        ' ptNone
    m_Port.Config.StopBits = 0      ' sb1

    If Not m_Port.OpenPort() Then
        Debug.Print "Open failed: " & m_Port.LastErrorMsg
        Exit Sub
    End If

    m_Port.StartMonitoring 50       ' 50ms polling
    Debug.Print "Started, waiting for data..."
End Sub

Private Sub m_Port_DataReceived(ByVal BytesCount As Long)
    Debug.Print "Received: " & m_Port.ReadExisting()
End Sub

Private Sub m_Port_ErrorOccurred(ByVal ErrorCode As Long, ByVal ErrorMsg As String)
    Debug.Print "Error: " & ErrorMsg
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If Not m_Port Is Nothing Then
        If m_Port.IsOpen Then m_Port.ClosePort
        Set m_Port = Nothing
    End If
End Sub

2. Request-Response Mode (With Timeout)

Send command and wait for response, auto-exit on timeout:

vb
Dim sp As New cSerialPort
sp.PortName = "COM3"
sp.Config.BaudRate = br9600
sp.Config.SetReadTimeout 2000       ' Read timeout 2 seconds
sp.OpenPort

sp.WriteText "hello"

Dim received As String
Dim startTime As Single
startTime = Timer
Do
    DoEvents
    If sp.InBufferCount > 0 Then
        received = received & sp.ReadExisting()
        If InStr(received, "world") > 0 Then Exit Do
    End If
Loop While Timer - startTime < 10   ' Wait up to 10 seconds

If InStr(received, "world") > 0 Then
    Debug.Print "Received response: " & received
Else
    Debug.Print "Timeout, no response received"
End If

sp.ClosePort

3. Binary Protocol Send/Receive

Send and receive byte arrays, suitable for Modbus and other binary protocols:

vb
Dim sp As New cSerialPort
sp.PortName = "COM3"
sp.Config.BaudRate = br115200
sp.OpenPort

' Send Modbus request frame
Dim frame(7) As Byte
frame(0) = &H01    ' Device address
frame(1) = &H03    ' Function code
frame(2) = &H00: frame(3) = &H00  ' Start address
frame(4) = &H00: frame(5) = &H0A  ' Register count
frame(6) = &HC5: frame(7) = &HCD  ' CRC
sp.WriteData frame

' Wait and read response
Dim startTime As Single
startTime = Timer
Do While sp.InBufferCount < 5 And Timer - startTime < 2
    DoEvents
Loop

Dim buf() As Byte
Dim n As Long
n = sp.ReadData(buf)               ' Read all available data
Debug.Print "Received " & n & " bytes"
sp.ClosePort

4. Read One Line of Text

Split by newline, read a complete line:

vb
Dim sp As New cSerialPort
sp.PortName = "COM3"
sp.Config.BaudRate = br9600
sp.Config.SetReadTimeout 5000      ' 5 second timeout
sp.OpenPort

Dim line As String
line = sp.ReadLine()                ' Read until vbCrLf
Debug.Print "Received line: " & line

sp.ClosePort

5. Enumerate Available Serial Ports

Scan all available serial ports on the system:

vb
Dim ports As Collection
Set ports = EnumSerialPorts()

Dim p As Variant
For Each p In ports
    Debug.Print p                  ' COM1, COM3, ...
Next

Debug.Print "Total " & ports.Count & " available serial ports"

Last Updated: 2026-07-05

VB6 and LOGO copyright of Microsoft Corporation