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:
- Whether the port is occupied by another program
- Whether the port actually exists (check with
EnumSerialPorts) - Whether the driver is properly installed
' Check available ports
Dim ports As Collection
Set ports = EnumSerialPorts()
Dim p As Variant
For Each p In ports
Debug.Print p
NextQ: OpenPort returns False, how to troubleshoot?
A: Check LastError and LastErrorMsg:
If Not sp.OpenPort("COM3") Then
Debug.Print "Error code: " & sp.LastError
Debug.Print "Error description: " & sp.LastErrorMsg
End IfCommon 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:
- Verify physical connection: TX/RX must be cross-connected (one side's TX connects to the other side's RX)
- Verify configuration consistency: Both sides must have matching baud rate, data bits, parity, stop bits
- Verify signal lines: If using hardware flow control, CTS must be high to send
- Check flow control: Try setting
fcNoneto exclude flow control interference
' Troubleshoot configuration
Debug.Print sp.GetStatusString()
' Try no flow control
sp.Config.FlowControl = fcNone
sp.ApplyConfigQ: ReadExisting returns empty string?
A: Possible reasons:
- Buffer truly has no data (
InBufferCount = 0) - Read timeout setting too short
- Data has not arrived yet (asynchronous transmission has delay)
' Check buffer first
If sp.InBufferCount > 0 Then
Debug.Print sp.ReadExisting()
Else
Debug.Print "Buffer is empty"
End IfQ: 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:
- Set read timeout:
sp.Config.SetReadTimeout 5000 - Confirm whether peer sends
vbCrLf(carriage return + line feed) - Use
ReadExistingfor 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:
' 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:
sp.OpenPort
sp.Config.BaudRate = br115200
sp.ApplyConfig ' ← Must callHowever, 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:
cfg.DataBits = 3 ' Error!
cfg.DataBits = 8 ' CorrectQ: InBufferSize setting throws error?
A: InBufferSize and OutBufferSize must be greater than 0:
cfg.InBufferSize = 0 ' Error!
cfg.InBufferSize = 4096 ' CorrectQ: RTS/DTR behavior is abnormal after setting FlowControl?
A: Setting FlowControl automatically synchronizes DTR/RTS control modes:
| FlowControl | Auto-set DTR/RTS |
|---|---|
fcRtsCts | RtsControl = Handshake |
fcDtrDsr | DtrControl = Handshake |
fcRtsCtsAndXonXoff | RtsControl = 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:
sp.OpenPort
sp.StartMonitoring 50 ' ← Must callAlso check:
- Whether the peer actually sent data
- Whether
PollIntervalis reasonable (default 50ms) - 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:
' ❌ Wrong: freezes interface
sp.WaitForEvent EV_RXCHAR
' ✅ Correct: asynchronous monitoring
sp.StartMonitoring 50Q: 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:
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 SubError Handling
Q: Received CE_RXOVER (receive buffer overflow) error?
A: Receive speed exceeds processing speed, buffer is full. Solutions:
- Increase buffer:
cfg.InBufferSize = 8192 - Read faster: Reduce
PollInterval - Read immediately in event: Call
ReadExistinginDataReceivedevent handler - Use flow control:
cfg.FlowControl = fcXonXoff
Q: Received CE_RXPARITY (parity error)?
A: Communication line interference or configuration mismatch. Check:
- Whether both sides have matching parity settings
- Whether the line has interference (long distance, unshielded)
- 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:
Debug.Print "Current baud rate: " & sp.Config.BaudRateQ: How to get detailed error information?
A: Use these methods:
' 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