Documentation
¶
Overview ¶
Package sers offers serial port access. It is a stated goal of this package to allow for non-standard bit rates as the may be useful in a wide range of embedded projects.
Example ¶
This program opens a serial port, configurable by changing portname below and configures it for 57600 baud, 8 data bits, no parity bit, 1 stop bit, no handshaking. It then reads up to 128 bytes from the serial port, with a timeout of 1 second and prints the received bytes to stdout.
package main
import (
"encoding/hex"
"fmt"
"log"
"github.com/distributed/sers"
)
// This program opens a serial port, configurable by changing portname
// below and configures it for 57600 baud, 8 data bits, no parity bit,
// 1 stop bit, no handshaking. It then reads up to 128 bytes from the serial
// port, with a timeout of 1 second and prints the received
// bytes to stdout.
func main() {
portname := "/dev/ttyUSB0"
rb, err := readFirstBytesFromPort(portname)
if err != nil {
log.Fatal(err)
}
fmt.Printf("got %d bytes from %s:\n%s", len(rb), portname,
hex.Dump(rb))
}
func readFirstBytesFromPort(fn string) ([]byte, error) {
sp, err := sers.Open(fn)
if err != nil {
return nil, err
}
defer sp.Close()
// 57600 baud, 8 data bits, no parity bit, 1 stop bit
// no handshake. non-standard baud rates are possible.
err = sp.SetMode(57600, 8, sers.N, 1, sers.NO_HANDSHAKE)
if err != nil {
return nil, err
}
// setting:
// minread = 0: minimal buffering on read, return characters as early as possible
// timeout = 1.0: time out if after 1.0 seconds nothing is received
err = sp.SetReadParams(0, 1.0)
if err != nil {
return nil, err
}
var rb [128]byte
n, err := sp.Read(rb[:])
return rb[:n], err
}
Output:
Index ¶
Examples ¶
Constants ¶
View Source
const ( N = 0 // no parity E = 1 // even parity O = 2 // odd parity )
View Source
const ( NO_HANDSHAKE = 0 RTSCTS_HANDSHAKE = 1 )
View Source
const ( // using C.IOSSIOSPEED yields 0x80085402 // which does not work. don't ask me why // this define is wrong in cgo. IOSSIOSPEED = 0x80045402 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ParameterError ¶
func (*ParameterError) Error ¶
func (pe *ParameterError) Error() string
type SerialPort ¶
type SerialPort interface {
io.Reader
io.Writer
io.Closer
// SetMode sets the frame format and handshaking configuration.
// baudrate may be freely chosen, the driver is allowed to reject
// unachievable baud rates. databits may be any number of data bits
// supported by the driver. parity is one of (N|O|E) for none, odd
// or even parity. handshake is either NO_HANDSHAKE or
// RTSCTS_HANDSHAKE.
SetMode(baudrate, databits, parity, stopbits, handshake int) error
// SetReadParams sets the minimum number of bits to read and a read
// timeout in seconds. These parameters roughly correspond to the
// UNIX termios concepts of VMIN and VTIME.
SetReadParams(minread int, timeout float64) error
// SetBreak turns on the generation of a break condition if on == true,
// otherwise it clear the break condition.
SetBreak(on bool) error
}
Serialport represents a serial port and offers configuration of baud rate, frame format, handshaking and read paramters as well as setting and clearing break conditions.
func Open ¶
func Open(fn string) (SerialPort, error)
type StringError ¶
type StringError string
func (StringError) Error ¶
func (se StringError) Error() string
Click to show internal directories.
Click to hide internal directories.