Skip to content

cSerialPort FAQ

📖 Table of Contents


Port Issues

Q: Failed to open COM10 or higher port?

A: cSerialPort internally handles the \\.\COMx prefix automatically. Just pass "COM10" directly. If it still fails, check:

  1. Whether the port is occupied by another program
  2. Whether the port actually exists (check with EnumSerialPorts)
  3. Whether the driver is properly installed
vb
' Check available ports
Dim ports As Collection
Set ports = EnumSerialPorts()
Dim p As Variant
For Each p In ports
    Debug.Print p
Next

Q: OpenPort returns False, how to troubleshoot?

A: Check LastError and LastErrorMsg:

vb
If Not sp.OpenPort("COM3") Then
    Debug.Print "Error code: " & sp.LastError
    Debug.Print "Error description: " & sp.LastErrorMsg
End If

Common errors:

  • Error code 5 (Access denied): Port occupied by another program
  • Error code 2 (File not found): Port does not exist
  • Error code 87 (Invalid parameter): Configuration parameters invalid

Q: Can I change the port name after opening?

A: No. PortName cannot be modified when IsOpen=True. Must ClosePort first then modify.


Data Send/Receive Issues

Q: Sent data but can't receive anything?

A: Troubleshooting steps:

  1. Verify physical connection: TX/RX must be cross-connected (one side's TX connects to the other side's RX)
  2. Verify configuration consistency: Both sides must have matching baud rate, data bits, parity, stop bits
  3. Verify signal lines: If using hardware flow control, CTS must be high to send
  4. Check flow control: Try setting fcNone to exclude flow control interference
vb
' Troubleshoot configuration
Debug.Print sp.GetStatusString()

' Try no flow control
sp.Config.FlowControl = fcNone
sp.ApplyConfig

Q: ReadExisting returns empty string?

A: Possible reasons:

  1. Buffer truly has no data (InBufferCount = 0)
  2. Read timeout setting too short
  3. Data has not arrived yet (asynchronous transmission has delay)
vb
' Check buffer first
If sp.InBufferCount > 0 Then
    Debug.Print sp.ReadExisting()
Else
    Debug.Print "Buffer is empty"
End If

Q: ReadLine keeps blocking and doesn't return?

A: ReadLine blocks until a terminator (default vbCrLf) is received or timeout. If the peer sends data without a newline, it will keep waiting.

Solutions:

  1. Set read timeout: sp.Config.SetReadTimeout 5000
  2. Confirm whether peer sends vbCrLf (carriage return + line feed)
  3. Use ReadExisting for non-blocking read instead

Q: Received Chinese text is garbled?

A: ReadText/WriteText uses StrConv to convert by ANSI/system codepage. If the peer uses UTF-8 encoding, use byte array mode and convert yourself:

vb
' Receive UTF-8 byte array
Dim buf() As Byte
sp.ReadData buf

' Manually convert to Unicode string (requires cJson or other UTF-8 decoding tools)
Dim s As String
s = DecodeUtf8(buf)

Configuration Issues

Q: Configuration changes don't take effect?

A: When port is open, modifying Config properties requires calling ApplyConfig to take effect:

vb
sp.OpenPort
sp.Config.BaudRate = br115200
sp.ApplyConfig          ' ← Must call

However, replacing the entire configuration object with Set sp.Config = newCfg automatically applies.

Q: DataBits setting throws error?

A: DataBits range must be 4~8, throws error if out of range:

vb
cfg.DataBits = 3    ' Error!
cfg.DataBits = 8    ' Correct

Q: InBufferSize setting throws error?

A: InBufferSize and OutBufferSize must be greater than 0:

vb
cfg.InBufferSize = 0     ' Error!
cfg.InBufferSize = 4096  ' Correct

Q: RTS/DTR behavior is abnormal after setting FlowControl?

A: Setting FlowControl automatically synchronizes DTR/RTS control modes:

FlowControlAuto-set DTR/RTS
fcRtsCtsRtsControl = Handshake
fcDtrDsrDtrControl = Handshake
fcRtsCtsAndXonXoffRtsControl = Handshake

After enabling hardware flow control, manual SetRTS/SetDTR may be overridden by flow control mechanisms.


Event & Monitoring Issues

Q: DataReceived event doesn't trigger?

A: Must call StartMonitoring to start monitoring first:

vb
sp.OpenPort
sp.StartMonitoring 50    ' ← Must call

Also check:

  1. Whether the peer actually sent data
  2. Whether PollInterval is reasonable (default 50ms)
  3. Whether the object is declared with WithEvents

Q: WaitForEvent freezes the interface?

A: WaitForEvent is a blocking method that freezes the UI thread. Do not use on UI thread. Use StartMonitoring for asynchronous monitoring instead:

vb
' ❌ Wrong: freezes interface
sp.WaitForEvent EV_RXCHAR

' ✅ Correct: asynchronous monitoring
sp.StartMonitoring 50

Q: How to monitor signal line changes simultaneously?

A: Asynchronous monitoring mode (StartMonitoring) currently does not monitor signal line changes. To monitor signal lines, you can only use WaitForEvent (which blocks the thread), or use a Timer to periodically query:

vb
Private Sub tmrPin_Timer()
    Static lastCts As Boolean
    Dim cts As Boolean
    cts = sp.CtsHolding
    If cts <> lastCts Then
        Debug.Print "CTS changed: " & cts
        lastCts = cts
    End If
End Sub

Error Handling

Q: Received CE_RXOVER (receive buffer overflow) error?

A: Receive speed exceeds processing speed, buffer is full. Solutions:

  1. Increase buffer: cfg.InBufferSize = 8192
  2. Read faster: Reduce PollInterval
  3. Read immediately in event: Call ReadExisting in DataReceived event handler
  4. Use flow control: cfg.FlowControl = fcXonXoff

Q: Received CE_RXPARITY (parity error)?

A: Communication line interference or configuration mismatch. Check:

  1. Whether both sides have matching parity settings
  2. Whether the line has interference (long distance, unshielded)
  3. Whether baud rate is too high (try lowering baud rate)

Q: Received CE_FRAME (frame error)?

A: Usually baud rate mismatch. Confirm both sides have exactly the same baud rate:

vb
Debug.Print "Current baud rate: " & sp.Config.BaudRate

Q: How to get detailed error information?

A: Use these methods:

vb
' Get communication error codes
Dim errs As Long
errs = sp.ReadCommErrors()

' Get error description
Debug.Print GetCommErrorString(errs)

' Get complete status
Debug.Print sp.GetStatusString()

Last Updated: 2026-07-05

VB6 and LOGO copyright of Microsoft Corporation