Documentation
¶
Overview ¶
Package usbserial drives CH34x and PL2303 USB serial adapters from userspace, through the kernel's usbfs, for systems whose kernel has no driver for them. It only ever takes an interface no kernel driver is bound to and never detaches one, so where the kernel's ch341 or pl2303 driver is present the adapter is left to it and appears as a normal /dev/ttyUSB port.
An adapter is addressed as "usbserial:<port>", where <port> is the USB topology name the kernel gives the device in /sys/bus/usb/devices, such as "usbserial:1-1.2". The name follows the physical port, so it survives unplugging and matches the topology Core uses to identify readers on /dev/ttyUSB ports.
Index ¶
- Constants
- Variables
- func DeviceNode(path string) string
- func Exists(path string) error
- func IsPath(path string) bool
- func MakePath(sysName string) string
- func Topology(path string) string
- type Port
- func (p *Port) Break(d time.Duration) error
- func (p *Port) Close() error
- func (p *Port) Drain() error
- func (p *Port) GetModemStatusBits() (*serial.ModemStatusBits, error)
- func (p *Port) Path() string
- func (p *Port) Read(b []byte) (int, error)
- func (p *Port) ResetInputBuffer() error
- func (p *Port) ResetOutputBuffer() error
- func (p *Port) SetDTR(dtr bool) error
- func (p *Port) SetMode(mode *serial.Mode) error
- func (p *Port) SetRTS(rts bool) error
- func (p *Port) SetReadTimeout(t time.Duration) error
- func (p *Port) Write(b []byte) (int, error)
- type PortInfo
Constants ¶
const PathPrefix = "usbserial:"
PathPrefix starts every port path this package serves.
Variables ¶
var ( // ErrUnsupported is returned on systems without usbfs. ErrUnsupported = errors.New("userspace USB serial is not supported on this system") // ErrBadPath is returned for a path that is not a valid usbserial path. ErrBadPath = errors.New("not a usbserial port path") // ErrNotFound is returned when no supported adapter is at the path. ErrNotFound = errors.New("no supported USB serial adapter at this port") // ErrDriverBound is returned when a kernel driver or another program // already has the adapter. The driver is never detached. ErrDriverBound = errors.New("USB serial adapter is bound to a driver") // ErrSettling is returned for an adapter plugged in moments ago. The // kernel may still be loading a driver for it, so it is left alone for // a few seconds first. ErrSettling = errors.New("USB serial adapter was only just plugged in") // ErrClosed is returned by operations on a closed port. ErrClosed = errors.New("usbserial port is closed") // ErrDeviceGone is returned once the adapter has been unplugged. It // wraps ENODEV so code that classifies errno values sees an unplugged // device. ErrDeviceGone = fmt.Errorf("USB serial adapter is gone: %w", syscall.ENODEV) )
var ErrUnsupportedChip = errors.New("unsupported USB serial chip variant")
ErrUnsupportedChip is returned for an adapter variant the driver cannot run, such as a multi-port PL256x.
Functions ¶
func DeviceNode ¶
DeviceNode returns the usbfs device node of the adapter at path, such as "/dev/bus/usb/001/004", or an empty string when there is none. The node is recreated with a new number whenever the adapter is plugged in again.
func Exists ¶
Exists reports whether a supported adapter is plugged in at path, whether or not it is free to open.
Types ¶
type Port ¶
type Port struct {
// contains filtered or unexported fields
}
Port is an open adapter. It implements go.bug.st/serial's Port.
Locks are taken in the order ctrlMu, writeMu, readMu.
func Open ¶
Open opens the adapter at a usbserial path and applies mode. It fails with ErrDriverBound when a kernel driver or another program has the adapter.
func (*Port) Break ¶
Break holds a break condition on the line for d. CH34x chips without break control simulate one of about 200ms.
func (*Port) Close ¶
Close lowers DTR and RTS, releases the adapter and stops any Read in progress within one read slice. Closing again does nothing.
func (*Port) Drain ¶
Drain returns at once: a write already waits for the adapter to take the bytes, and the adapters cannot report when their own buffer has emptied.
func (*Port) GetModemStatusBits ¶
func (p *Port) GetModemStatusBits() (*serial.ModemStatusBits, error)
GetModemStatusBits reads the modem input lines.
func (*Port) Read ¶
Read reads received bytes. It waits up to the read timeout for the first byte and returns 0 bytes and no error when none arrive. Each USB packet is read whole; bytes that do not fit p are returned by the next Read.
func (*Port) ResetInputBuffer ¶
ResetInputBuffer discards received bytes, both those held by the port and those waiting on the adapter. It waits for a Read in progress to return.
func (*Port) ResetOutputBuffer ¶
ResetOutputBuffer does nothing: a write returns only once the adapter has taken the bytes, so the port holds none back.
func (*Port) SetMode ¶
SetMode changes the baud rate and framing. A mode equal to the current one is not sent again, as rewriting it can make a chip drop bytes.
func (*Port) SetReadTimeout ¶
SetReadTimeout sets how long Read waits for data. serial.NoTimeout waits until data arrives or the port is closed; zero does not wait.
type PortInfo ¶
type PortInfo struct {
// Path is the port path to open, such as "usbserial:1-1.2".
Path string
// Name is the manufacturer and product strings, when the device has them.
Name string
// Chip is the adapter family, "CH34x" or "PL2303".
Chip string
// Serial is the device's serial number string, when it has one.
Serial string
VendorID uint16
ProductID uint16
}
PortInfo describes a supported adapter with no driver bound to it.