serial

package module
v0.0.0-...-0bb7f04 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2015 License: BSD-2-Clause Imports: 6 Imported by: 0

README

#serial GoDoc

Download:

go get github.com/npat-efault/serial

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 to supports systems that provide the POSIX Terminal Interface for configuring terminal devices.

For data-transfer operations (Read, Write) it uses package 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. Patches and pull requests for this will be greatly appreciated.


#termios GoDoc

Package termios is a simple Go wrapper to the POSIX Terminal Interface (POSIX Termios).

Package termios is more low-level and system-specific than package serial and can be used to configure most aspects of a terminal device's operation on systems that support POSIX Termios.

###Supported systems

Package termios should work on all systems that support the POSIX terminal interface, that is, on most Unix-like systems. Depending on the system, package termios can either be built to use the system's LIBC functions and macros through CGo, or as a pure-go package that issues system calls directly. In either case, the API presented by the package is identical.

By default, package termios is built to use CGo to access the system's LIBC termios functions and macros. This is the most portable option. If building termios fails on a system that has POSIX termios support, please file an issue, or submit a patch to make it work (the required changes will most likely be trivial).

Alternatively, you can build package termios as a pure-Go package that issues system-calls directly. To do this define the nocgo build-tag when building/installing the package, like this:

cd $GOPATH/github.com/npat-efault/serial/termios
go install -tags 'nocgo'

Building termios as a pure-Go package is not supported on all systems.

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

Constants

This section is empty.

Variables

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

type FlowMode int

FlowMode encodes the supported flow-control modes

const (
	FlowNone    FlowMode = iota // No flow control
	FlowRTSCTS                  // Hardware flow control
	FlowXONXOFF                 // Software flow control
	FlowOther                   // Unknown mode
)

func (FlowMode) String

func (f FlowMode) String() string

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

func Open(name string) (port *Port, err error)

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

func (p *Port) Close() error

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

func (p *Port) Conf(conf Conf) error

Conf configures the serial port using the parameters in the Conf structure

func (*Port) ConfSome

func (p *Port) ConfSome(conf Conf, flags ConfFlags) error

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

func (p *Port) Flush() error

Flush discards any unread data in the serial port's receive buffers, as well as any unsent data in the transmit buffers.

func (*Port) FlushIn

func (p *Port) FlushIn() error

FlushIn discards any unread data in the serial port's receive buffers.

func (*Port) FlushOut

func (p *Port) FlushOut() error

FlushOut discards any unsent data in the serial port's transmit buffers.

func (*Port) GetConf

func (p *Port) GetConf() (conf Conf, err error)

GetConf returns the serial port's configuration parameters as a Conf structure.

func (*Port) Read

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

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

func (p *Port) SetDeadline(t time.Time) error

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

func (p *Port) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for Read operations. See also SetDeadline.

func (*Port) SetWriteDeadline

func (p *Port) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for Write operations. See also SetDeadline.

func (*Port) Write

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

Write is compatible with the Write method of the io.Writer interface. In addition Write honors the timeout set by Port.SetDeadline and Port.SetWriteDeadline. If less than len(p) data are writen before the timeout expires Write returns with err == ErrTimeout (and n < len(p)).

Directories

Path Synopsis
Package termios is a simple Go wrapper to the POSIX Terminal Interface (POSIX Termios).
Package termios is a simple Go wrapper to the POSIX Terminal Interface (POSIX Termios).

Jump to

Keyboard shortcuts

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