usbserial

package
v2.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: GPL-3.0 Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const PathPrefix = "usbserial:"

PathPrefix starts every port path this package serves.

Variables

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

func DeviceNode(path string) string

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

func Exists(path string) error

Exists reports whether a supported adapter is plugged in at path, whether or not it is free to open.

func IsPath

func IsPath(path string) bool

IsPath reports whether path is addressed to this package.

func MakePath

func MakePath(sysName string) string

MakePath returns the port path for the USB device with the given sysfs name.

func Topology

func Topology(path string) string

Topology returns the USB topology of a port path, such as "1-1.2", or an empty string when the path is not valid.

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

func Open(path string, mode *serial.Mode) (*Port, error)

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

func (p *Port) Break(d time.Duration) error

Break holds a break condition on the line for d. CH34x chips without break control simulate one of about 200ms.

func (*Port) Close

func (p *Port) Close() error

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

func (p *Port) Drain() error

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

func (p *Port) Path() string

Path returns the port path.

func (*Port) Read

func (p *Port) Read(b []byte) (int, error)

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

func (p *Port) ResetInputBuffer() error

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

func (p *Port) ResetOutputBuffer() error

ResetOutputBuffer does nothing: a write returns only once the adapter has taken the bytes, so the port holds none back.

func (*Port) SetDTR

func (p *Port) SetDTR(dtr bool) error

SetDTR sets the DTR output.

func (*Port) SetMode

func (p *Port) SetMode(mode *serial.Mode) error

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

func (p *Port) SetRTS(rts bool) error

SetRTS sets the RTS output.

func (*Port) SetReadTimeout

func (p *Port) SetReadTimeout(t time.Duration) error

SetReadTimeout sets how long Read waits for data. serial.NoTimeout waits until data arrives or the port is closed; zero does not wait.

func (*Port) Write

func (p *Port) Write(b []byte) (int, error)

Write sends b. It returns once the adapter has taken every byte.

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.

func List

func List() ([]PortInfo, error)

List returns the supported adapters no driver is bound to, sorted by path. It reads sysfs only and opens no device.

func (*PortInfo) VIDPID

func (i *PortInfo) VIDPID() string

VIDPID returns the vendor and product IDs as "VVVV:PPPP" in upper case hex.

Jump to

Keyboard shortcuts

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