Documentation
¶
Overview ¶
Package serial provides serial port abstraction for Z-Stack communication.
Index ¶
- Variables
- func IsCC2652Adapter(info USBPortInfo) bool
- func ListPorts() ([]string, error)
- type Config
- type MockPort
- func (m *MockPort) ClearWrittenData()
- func (m *MockPort) Close() error
- func (m *MockPort) GetDTR() bool
- func (m *MockPort) GetRTS() bool
- func (m *MockPort) GetReadTimeout() time.Duration
- func (m *MockPort) IsClosed() bool
- func (m *MockPort) OnWrite(callback func([]byte))
- func (m *MockPort) Read(p []byte) (int, error)
- func (m *MockPort) ReadBufLen() int
- func (m *MockPort) ResetInputBuffer() error
- func (m *MockPort) SetClosed()
- func (m *MockPort) SetDTR(dtr bool) error
- func (m *MockPort) SetRTS(rts bool) error
- func (m *MockPort) SetReadTimeout(timeout time.Duration) error
- func (m *MockPort) SimulateResponse(data []byte)
- func (m *MockPort) Write(p []byte) (int, error)
- func (m *MockPort) WriteBufLen() int
- func (m *MockPort) WrittenData() []byte
- type Port
- type USBPortInfo
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidPortPath = errors.New("invalid port path")
ErrInvalidPortPath indicates the port path is invalid or potentially unsafe.
Functions ¶
func IsCC2652Adapter ¶
func IsCC2652Adapter(info USBPortInfo) bool
IsCC2652Adapter checks if a USBPortInfo represents a known CC2652/SONOFF adapter. Recognized adapters:
- CH340: VID 1A86, PID 55D4
- CP2102: VID 10C4, PID EA60
Types ¶
type Config ¶
type Config struct {
Path string // Device path (e.g., /dev/ttyUSB0, COM3).
BaudRate int // Baud rate (default 115200).
ReadTimeout time.Duration // Read timeout (default 100ms).
RTSCTSFlow bool // Enable RTS/CTS hardware flow control.
}
Config holds serial port configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults for Z-Stack communication.
type MockPort ¶
type MockPort struct {
// contains filtered or unexported fields
}
MockPort is a mock implementation of Port for testing. It is thread-safe and can simulate various serial port behaviors.
func (*MockPort) ClearWrittenData ¶
func (m *MockPort) ClearWrittenData()
ClearWrittenData clears the write buffer. Useful for resetting state between test cases.
func (*MockPort) GetReadTimeout ¶
GetReadTimeout returns the current read timeout setting.
func (*MockPort) OnWrite ¶
OnWrite sets a callback function that is invoked whenever data is written. This is useful for simulating device responses in tests. The callback receives a copy of the written data.
Example:
mock.OnWrite(func(data []byte) {
if isCommandFrame(data) {
mock.SimulateResponse(createResponse(data))
}
})
func (*MockPort) Read ¶
Read reads data from the mock port's read buffer. Implements io.Reader interface.
func (*MockPort) ReadBufLen ¶
ReadBufLen returns the current length of the read buffer. Useful for testing buffer management.
func (*MockPort) ResetInputBuffer ¶
ResetInputBuffer clears the read buffer.
func (*MockPort) SetClosed ¶
func (m *MockPort) SetClosed()
SetClosed simulates a disconnection by marking the port as closed. Subsequent operations will fail with appropriate errors.
func (*MockPort) SetReadTimeout ¶
SetReadTimeout sets the read timeout for the mock port.
func (*MockPort) SimulateResponse ¶
SimulateResponse injects bytes into the read buffer for testing. This simulates data arriving from a real serial device.
func (*MockPort) Write ¶
Write writes data to the mock port and invokes the OnWrite callback if set. Implements io.Writer interface.
func (*MockPort) WriteBufLen ¶
WriteBufLen returns the current length of the write buffer. Useful for testing write operations.
func (*MockPort) WrittenData ¶
WrittenData returns all data that has been written to the mock port. This is useful for verifying what was sent in tests.
type Port ¶
type Port interface {
io.ReadWriteCloser
SetReadTimeout(timeout time.Duration) error
SetDTR(dtr bool) error
SetRTS(rts bool) error
ResetInputBuffer() error
}
Port interface for serial port operations. Extends io.ReadWriteCloser with additional serial port specific methods.
type USBPortInfo ¶
type USBPortInfo struct {
Name string // Port name (e.g., /dev/ttyUSB0).
IsUSB bool // Whether this is a USB port.
VID string // USB Vendor ID (hex string).
PID string // USB Product ID (hex string).
SerialNumber string // USB serial number.
Product string // Product description.
}
USBPortInfo contains detailed information about a USB serial port.
func FindCC2652Adapters ¶
func FindCC2652Adapters() ([]USBPortInfo, error)
FindCC2652Adapters returns all detected CC2652/SONOFF adapters.
func ListUSBPorts ¶
func ListUSBPorts() ([]USBPortInfo, error)
ListUSBPorts returns detailed information about available USB serial ports.