serial

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package serial provides serial port abstraction for Z-Stack communication.

Index

Constants

This section is empty.

Variables

View Source
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

func ListPorts

func ListPorts() ([]string, error)

ListPorts returns a list of available serial port names.

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 NewMockPort

func NewMockPort() *MockPort

NewMockPort creates a new MockPort for testing.

func (*MockPort) ClearWrittenData

func (m *MockPort) ClearWrittenData()

ClearWrittenData clears the write buffer. Useful for resetting state between test cases.

func (*MockPort) Close

func (m *MockPort) Close() error

Close closes the mock port. Implements io.Closer interface.

func (*MockPort) GetDTR

func (m *MockPort) GetDTR() bool

GetDTR returns the current DTR signal state.

func (*MockPort) GetRTS

func (m *MockPort) GetRTS() bool

GetRTS returns the current RTS signal state.

func (*MockPort) GetReadTimeout

func (m *MockPort) GetReadTimeout() time.Duration

GetReadTimeout returns the current read timeout setting.

func (*MockPort) IsClosed

func (m *MockPort) IsClosed() bool

IsClosed returns whether the mock port is closed.

func (*MockPort) OnWrite

func (m *MockPort) OnWrite(callback func([]byte))

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

func (m *MockPort) Read(p []byte) (int, error)

Read reads data from the mock port's read buffer. Implements io.Reader interface.

func (*MockPort) ReadBufLen

func (m *MockPort) ReadBufLen() int

ReadBufLen returns the current length of the read buffer. Useful for testing buffer management.

func (*MockPort) ResetInputBuffer

func (m *MockPort) ResetInputBuffer() error

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) SetDTR

func (m *MockPort) SetDTR(dtr bool) error

SetDTR sets the Data Terminal Ready signal state.

func (*MockPort) SetRTS

func (m *MockPort) SetRTS(rts bool) error

SetRTS sets the Request To Send signal state.

func (*MockPort) SetReadTimeout

func (m *MockPort) SetReadTimeout(timeout time.Duration) error

SetReadTimeout sets the read timeout for the mock port.

func (*MockPort) SimulateResponse

func (m *MockPort) SimulateResponse(data []byte)

SimulateResponse injects bytes into the read buffer for testing. This simulates data arriving from a real serial device.

func (*MockPort) Write

func (m *MockPort) Write(p []byte) (int, error)

Write writes data to the mock port and invokes the OnWrite callback if set. Implements io.Writer interface.

func (*MockPort) WriteBufLen

func (m *MockPort) WriteBufLen() int

WriteBufLen returns the current length of the write buffer. Useful for testing write operations.

func (*MockPort) WrittenData

func (m *MockPort) WrittenData() []byte

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.

func Open

func Open(cfg Config) (Port, error)

Open opens a serial port with the given configuration. It validates the port path to prevent path traversal and ensure it's a valid serial port path.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL