serial

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: AGPL-3.0 Imports: 13 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 opens local serial ports as terminal byte streams. It wraps a swappable Backend (go.bug.st/serial by default) behind its own types, so callers never see the backend's.

Index

Constants

View Source
const DefaultBaud = 115200

DefaultBaud is the baud rate of a Config that names none.

Variables

View Source
var (
	ErrNotFound      = errors.New("serial: no such port")
	ErrBusy          = errors.New("serial: port in use")
	ErrPermission    = errors.New("serial: permission denied")
	ErrInvalidConfig = errors.New("serial: invalid settings")
	ErrDisconnected  = errors.New("serial: device disconnected")
)

Errors returned by Open and by a Port's Read; each wraps the original cause.

View Source
var Bauds = []int{300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600}

Bauds are the baud rates offered as choices; any positive rate is allowed.

Functions

func Ports

func Ports() ([]string, error)

Ports lists the serial devices worth offering on this computer, sorted naturally (COM2 before COM10).

Types

type Backend

type Backend interface {
	// Open opens a normalized, validated Config.
	Open(Config) (Port, error)
	// Ports lists the serial devices present, in any order.
	Ports() ([]string, error)
}

Backend opens ports and lists devices.

var Default Backend = bugst{}

Default is the Backend Open and Ports use: go.bug.st/serial.

type Config

type Config struct {
	Device   string
	Baud     int      // 0 means DefaultBaud
	DataBits int      // 0 means 8; valid 5..8
	Parity   Parity   // "" means none
	StopBits StopBits // "" means 1
}

Config is how to open a port.

func (Config) Frame

func (c Config) Frame() string

Frame is the conventional short form of the normalized framing, such as "8N1" or "7E1.5".

func (Config) Normalized

func (c Config) Normalized() Config

Normalized fills in defaults and tidies Device (on Windows "com3" and `\\.\COM3` become "COM3").

func (Config) Validate

func (c Config) Validate() error

Validate reports whether the normalized Config can be opened; errors wrap ErrInvalidConfig.

type Parity

type Parity string

Parity is a port's parity setting.

const (
	ParityNone  Parity = "none"
	ParityOdd   Parity = "odd"
	ParityEven  Parity = "even"
	ParityMark  Parity = "mark"
	ParitySpace Parity = "space"
)

Parity settings.

type Port

type Port interface {
	io.ReadWriteCloser
	Break(d time.Duration) error
	SetDTR(on bool) error
	SetRTS(on bool) error
}

Port is an open serial port. Read blocks until data arrives; after Close it returns io.EOF, and if the device goes away it returns an error wrapping ErrDisconnected. Close is idempotent and may run concurrently with a blocked Read.

func Open

func Open(c Config) (Port, error)

Open normalizes and validates c, then opens it with Default.

type StopBits

type StopBits string

StopBits is a port's stop-bit setting.

const (
	Stop1     StopBits = "1"
	Stop1Half StopBits = "1.5"
	Stop2     StopBits = "2"
)

Stop-bit settings.

Jump to

Keyboard shortcuts

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