serial

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: AGPL-3.0 Imports: 26 Imported by: 0 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 serial carries a serial port of the host to the client as a local virtual serial port.

Wire protocol, on one session stream named "serial":

  • request {op, port, baud, data, parity, stop, dtr, rts}. op "list" answers one JSON {ok, error, ports:[...]} and ends. op "open" opens the port on the host (exclusively, raw, 8N1 unless asked otherwise) and answers {ok, error, port, baud}.
  • after an ok open both sides exchange binary frames (the wire package framing, payload at most 64 KiB): 1 DATA raw bytes; 2 CONFIG JSON {baud,data,parity,stop,rtscts} client to host; 3 MODEM JSON, client to host {dtr,rts} (null = unchanged), host to client {cts,dsr,dcd,ri}; 4 BREAK client to host, 2-byte big-endian milliseconds; 5 NOTICE host to client, a text diagnostic (for example a refused CONFIG).
  • the stream ending, in either direction, closes the host port.

The client end of a virtual port is a pseudo-terminal whose slave is symlinked to a path of the user's choosing; it exists on Linux and macOS.

Index

Constants

View Source
const Service = "serial"

Service is the wire service name.

Variables

View Source
var ErrUnsupported = errors.New("serial: not supported on this platform")

ErrUnsupported is returned where a platform has no implementation.

Functions

func DefaultLocalPath

func DefaultLocalPath(target, port string) (string, error)

DefaultLocalPath is where a virtual port for target's port lives by default.

Types

type Active

type Active struct {
	PID    int    `json:"pid"`
	Target string `json:"target"`
	Local  string `json:"local"`
	Port   string `json:"port"`
	Config string `json:"config"`
}

Active is one virtual port running on this machine.

func ListActive

func ListActive() ([]Active, error)

ListActive returns the virtual ports run by remohop processes on this machine.

type AttachOptions

type AttachOptions struct {
	OpenOptions
	// LocalPath is where the virtual port appears (a symlink to the pty
	// slave). Required.
	LocalPath string
}

AttachOptions configure Attach.

type Config

type Config struct {
	Baud   int  `json:"baud"`
	Data   int  `json:"data"`
	Parity byte `json:"-"`
	Stop   int  `json:"stop"`
	RTSCTS bool `json:"rtscts,omitempty"`
}

Config is the line setting of a port: Baud, 5-8 data bits, parity 'N', 'E' or 'O', 1 or 2 stop bits, optional RTS/CTS hardware flow control.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig is 115200 8N1.

func (*Config) ParseFormat

func (c *Config) ParseFormat(s string) error

ParseFormat parses "8N1" style frame formats into c's Data/Parity/Stop.

func (Config) String

func (c Config) String() string

func (Config) Validate

func (c Config) Validate() error

Validate bounds every field; peers are untrusted.

type Modem

type Modem struct {
	CTS bool `json:"cts"`
	DSR bool `json:"dsr"`
	DCD bool `json:"dcd"`
	RI  bool `json:"ri"`
}

Modem is the state of the input modem status lines.

type OpenOptions

type OpenOptions struct {
	// DTR and RTS, when non-nil, are the initial state of those lines.
	DTR, RTS *bool
	// OnNotice receives host diagnostics (a refused setting); may be nil.
	OnNotice func(string)
	// OnModem receives host input-line changes (CTS, DSR, DCD, RI); may be nil.
	OnModem func(Modem)
}

OpenOptions are optional extras of Open.

type Port

type Port interface {
	io.ReadWriteCloser
	Apply(Config) error
	// SetModem sets DTR and/or RTS (nil = leave alone).
	SetModem(dtr, rts *bool) error
	// Modem reads the input status lines.
	Modem() (Modem, error)
	// Break holds the line in break condition for d.
	Break(d time.Duration) error
}

Port is an open serial device on the host.

type PortInfo

type PortInfo struct {
	Path        string   `json:"path"`                  // /dev/ttyUSB0, COM3
	Name        string   `json:"name"`                  // ttyUSB0, COM3
	ByID        []string `json:"by_id,omitempty"`       // stable /dev/serial/by-id names
	Driver      string   `json:"driver,omitempty"`      // kernel driver
	Description string   `json:"description,omitempty"` // manufacturer product
	VID         string   `json:"vid,omitempty"`
	PID         string   `json:"pid,omitempty"`
	Serial      string   `json:"serial,omitempty"`
}

PortInfo describes one serial port of a machine.

func List

func List(ctx context.Context, c *peer.Conn) ([]PortInfo, error)

List asks the host which serial ports it has. Strings are bounded but not sanitised for a terminal: print them through cli.Sanitize.

func ListPorts

func ListPorts() ([]PortInfo, error)

ListPorts enumerates the serial ports of this machine.

type Remote

type Remote struct {

	// Path is the device path on the host; Config what it was opened with.
	Path   string
	Config Config
	// contains filtered or unexported fields
}

Remote is an open remote serial port: an io.ReadWriteCloser of the bytes of the port plus control. It is safe for concurrent use.

func Open

func Open(ctx context.Context, c *peer.Conn, port string, cfg Config, opts OpenOptions) (*Remote, error)

Open opens the host's port (exclusively) with cfg.

func (*Remote) Break

func (r *Remote) Break(d time.Duration) error

Break asks the host to hold a break condition for d (at most 2 s).

func (*Remote) Close

func (r *Remote) Close() error

Close closes the remote port.

func (*Remote) Done

func (r *Remote) Done() <-chan struct{}

Done is closed when the port or the session ends.

func (*Remote) Err

func (r *Remote) Err() error

Err is why Done closed (nil for a clean end). Valid after Done.

func (*Remote) Read

func (r *Remote) Read(b []byte) (int, error)

Read returns bytes from the remote port; io.EOF once the port or session is gone.

func (*Remote) SetConfig

func (r *Remote) SetConfig(cfg Config) error

SetConfig changes the line settings of the remote port.

func (*Remote) SetModem

func (r *Remote) SetModem(dtr, rts *bool) error

SetModem sets DTR and/or RTS on the remote port (nil leaves a line alone).

func (*Remote) Write

func (r *Remote) Write(b []byte) (int, error)

Write sends bytes to the remote port.

type Virtual

type Virtual struct {
	Remote *Remote
	// Path is the symlink the user gave; Slave the real pty device behind it.
	Path, Slave string
	// contains filtered or unexported fields
}

Virtual is a local virtual serial port wired to a remote one.

func Attach

func Attach(ctx context.Context, c *peer.Conn, port string, cfg Config, o AttachOptions) (*Virtual, error)

Attach opens the remote port and creates the local virtual serial port.

func (*Virtual) Close

func (v *Virtual) Close() error

Close removes the virtual port and closes the remote one.

func (*Virtual) Done

func (v *Virtual) Done() <-chan struct{}

Done is closed once the virtual port is gone.

func (*Virtual) Err

func (v *Virtual) Err() error

Err is why the port ended (nil: closed locally or by the host cleanly).

Jump to

Keyboard shortcuts

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