Documentation ¶
Overview ¶
Package serial provides a simple, high-level, system-independent interface for accessing asynchronous serial ports. It provides functions and methods for opening serial ports, configuring their basic parameters (baudrate, character format, flow-control, etc.), for reading and writing data from and to them, and for a few other miscellaneous operations (sending a break signal, flushing the I/O buffers, and so on). Data transfer operations support deadlines (timeouts) and safe cancelation; a blocked read or write operation can be safely and reliably canceled from another goroutine by closing the port.
Supported systems ¶
Most unix-like systems are supported. Package serial uses package "termios" (github.com/npat-efault/serial/termios) to supports systems that provide the POSIX Terminal Interface for configuring terminal devices. For data-transfer operations (Read, Write) it uses package "poller" (github.com/npat-efault/poller) which provides I/O operations with timeouts and safe cancelation. Depending on the specific system, both of these packages (termios and poller) can be compiled either to use CGo or as pure-Go packages. See their documentation for details.
Addition of support for other systems is certainly possible, and mostly welcome.
Index ¶
- Variables
- type Conf
- type ConfFlags
- type FlowMode
- type ParityMode
- type Port
- func (p *Port) Close() error
- func (p *Port) Conf(conf Conf) error
- func (p *Port) ConfSome(conf Conf, flags ConfFlags) error
- func (p *Port) Flush() error
- func (p *Port) FlushIn() error
- func (p *Port) FlushOut() error
- func (p *Port) GetConf() (conf Conf, err error)
- func (p *Port) Read(b []byte) (n int, err error)
- func (p *Port) SetDeadline(t time.Time) error
- func (p *Port) SetReadDeadline(t time.Time) error
- func (p *Port) SetWriteDeadline(t time.Time) error
- func (p *Port) Write(b []byte) (n int, err error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed is returned by package functions and methods to // indicate failure because the port they tried to operate on // has been closed. ErrClosed has Closed() == true. ErrClosed = mkErr(efClosed, "port closed") // ErrTimeout is returned by Port methods Read and Write to // indicate that the operation took too long, and the set // timeout or deadline has expired. ErrTimeout has Timeout() // == true and Temporary() == true ErrTimeout = mkErr(efTimeout, "timeout/deadline expired") // ErrEOF is returned by Port method Read, in accordance with // the io.Reader interface. ErrEOF = io.EOF // ErrUnexpectedEOF is returned by Port method Write, in // accordance with the io.Writer interface. ErrUnexpectedEOF = io.ErrUnexpectedEOF )
Errors returned by functions and methods in this package. Other errors may as well be returned which are system-dependent and subject to change. You should not act based on the value of errors other than these.
Functions ¶
This section is empty.
Types ¶
type Conf ¶
type Conf struct { Baudrate int // in Bits Per Second Databits int // 5, 6, 7, or 8 Stopbits int // 1 or 2 Parity ParityMode // see ParityXXX constants Flow FlowMode // see FlowXXX constants NoReset bool // don't reset and don't hangup on close }
Conf is used to pass the serial port's configuration parameters to and from methods of this package.
type ConfFlags ¶
type ConfFlags int
ConfFlags are flags controlling which parameters to configure
const ( ConfBaudrate ConfFlags = 1 << iota ConfDatabits ConfParity ConfStopbits ConfFlow ConfNoReset ConfFormat = ConfDatabits | ConfParity | ConfStopbits ConfAll = ConfBaudrate | ConfFormat | ConfFlow | ConfNoReset )
type ParityMode ¶
type ParityMode int
ParityMode encodes the supported bit-parity modes
const ( ParityNone ParityMode = iota // No parity bit ParityEven // Even bit-parity ParityOdd // Odd bit-parity ParityMark // Parity bit to logical 1 (mark) ParitySpace // Parity bit to logical 0 (space) )
func (ParityMode) String ¶
func (p ParityMode) String() string
type Port ¶
type Port struct { Name string // Name used at Port.Open // contains filtered or unexported fields }
Port is a serial port
func Open ¶
Open opens the named serial port. Open records the port-settings (so they can be reset at Close), and sets the port to what unix calls "raw-mode" (transparent operation, without character translation or other processing). Other port settings (baudratre, character format, flow-control, etc.) are not altered.
func (*Port) Close ¶
Close closes the port. Unless the port has been configured with Conf.NoReset = true, the port is reset to its original settings (the ones it had at open), and the connection is terminated by de-asserting the modem control lines. It is safe to call Close concurently (from another goroutine) with any other Port method. Close will cancel ongoing (blocked) Read and Write operations, and make them return ErrClosed.
func (*Port) ConfSome ¶
ConfSome configures the serial port using some of the parameters in the Conf structure, based on the value of the flags argument.
func (*Port) Flush ¶
Flush discards any unread data in the serial port's receive buffers, as well as any unsent data in the transmit buffers.
func (*Port) GetConf ¶
GetConf returns the serial port's configuration parameters as a Conf structure.
func (*Port) Read ¶
Read is compatible with the Read method of the io.Reader interface. In addition Read honors the timeout set by Port.SetDeadline and Port.SetReadDeadline. If no data are read before the timeout expires Read returns with err == ErrTimeout (and n == 0).
func (*Port) SetDeadline ¶
SetDeadline sets the deadline for both Read and Write operations on the port. Deadlines are expressed as ABSOLUTE instances in time. For example, to set a deadline 5 seconds to the future do:
p.SetDeadline(time.Now().Add(5 * time.Second))
This is equivalent to:
p.SetReadDeadline(time.Now().Add(5 * time.Second)) p.SetWriteDeadline(time.Now().Add(5 * time.Second))
A zero value for t, cancels (removes) the existing deadline.
func (*Port) SetReadDeadline ¶
SetReadDeadline sets the deadline for Read operations. See also SetDeadline.
func (*Port) SetWriteDeadline ¶
SetWriteDeadline sets the deadline for Write operations. See also SetDeadline.