serial

package module
v0.0.0-...-3cfbd2f Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2015 License: BSD-3-Clause Imports: 5 Imported by: 8

Documentation

Overview

This is a cross-platform serial library for go.

The canonical import for this library is go.bug.st/serial so the import line is the following:

import "go.bug.st/serial"

It is possibile to get the list of available serial ports with the GetPortsList function:

ports, err := serial.GetPortsList()
if err != nil {
	log.Fatal(err)
}
if len(ports) == 0 {
	log.Fatal("No serial ports found!")
}
for _, port := range ports {
	fmt.Printf("Found port: %v\n", port)
}

The serial port can be opened with the OpenPort function:

mode := &serial.Mode{
	BaudRate: 115200,
}
port, err := serial.OpenPort("/dev/ttyUSB0", mode)
if err != nil {
	log.Fatal(err)
}

The OpenPort command needs a "mode" parameter that specifies the configuration options for the serial port. If not specified the default options are 9600_N81, in the example above only the speed is changed so the port is opened using 115200_N81. The following snippets shows how to declare a configuration for 57600_E71:

mode := &serial.Mode{
	BaudRate: 57600,
	Parity: serial.PARITY_EVEN,
	DataBits: 7,
	StopBits: serial.STOPBITS_ONE,
}

The configuration can be changed at any time with the SetMode function:

err := port.SetMode(mode)
if err != nil {
	log.Fatal(err)
}

The port object implements the io.ReadWriteCloser interface, so we can use the usual Read, Write and Close functions to send and receive data from the serial port:

n, err := port.Write([]byte("10,20,30\n\r"))
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Sent %v bytes\n", n)

buff := make([]byte, 100)
for {
	n, err := port.Read(buff)
	if err != nil {
		log.Fatal(err)
		break
	}
	if n == 0 {
		fmt.Println("\nEOF")
		break
	}
	fmt.Printf("%v", string(buff[:n]))
}

This library doesn't make use of cgo and "C" package, so it's a pure go library that can be easily cross compiled.

Index

Constants

View Source
const (
	ERROR_PORT_BUSY = iota
	ERROR_PORT_NOT_FOUND
	ERROR_INVALID_SERIAL_PORT
	ERROR_PERMISSION_DENIED
	ERROR_INVALID_PORT_SPEED
	ERROR_INVALID_PORT_DATA_BITS
	ERROR_ENUMERATING_PORTS
	ERROR_OTHER
)

Variables

This section is empty.

Functions

func GetPortsList

func GetPortsList() ([]string, error)

Types

type Mode

type Mode struct {
	BaudRate int      // The serial port bitrate (aka Baudrate)
	DataBits int      // Size of the character (must be 5, 6, 7 or 8)
	Parity   Parity   // Parity (see Parity type for more info)
	StopBits StopBits // Stop bits (see StopBits type for more info)
	Vmin     uint8    // Vmin (minimum characters to receive before returning)
	Vtimeout uint8    // VTimeout (minimum time to wait before returning)
}

This structure describes a serial port configuration.

type Parity

type Parity int
const (
	PARITY_NONE  Parity = iota // No parity (default)
	PARITY_ODD                 // Odd parity
	PARITY_EVEN                // Even parity
	PARITY_MARK                // Mark parity (always 1)
	PARITY_SPACE               // Space parity (always 0)
)

type SerialPort

type SerialPort struct {
	// contains filtered or unexported fields
}

Opaque type that implements SerialPort interface for linux

func OpenPort

func OpenPort(portName string, mode *Mode) (*SerialPort, error)

Open the serial port using the specified modes

func (*SerialPort) Close

func (port *SerialPort) Close() error

Close the serial port

func (*SerialPort) Read

func (port *SerialPort) Read(p []byte) (n int, err error)

Stores data received from the serial port into the provided byte array buffer. The function returns the number of bytes read.

The Read function blocks until (at least) one byte is received from the serial port or an error occurs.

func (*SerialPort) SetDTR

func (port *SerialPort) SetDTR(level bool) error

func (*SerialPort) SetMode

func (port *SerialPort) SetMode(mode *Mode) error

Set all parameters of the serial port. See the Mode structure for more info.

func (*SerialPort) Write

func (port *SerialPort) Write(p []byte) (n int, err error)

Send the content of the data byte array to the serial port. Returns the number of bytes written.

type SerialPortError

type SerialPortError struct {
	// contains filtered or unexported fields
}

Platform independent error type for serial ports

func (SerialPortError) Code

func (e SerialPortError) Code() int

func (SerialPortError) Error

func (e SerialPortError) Error() string

type StopBits

type StopBits int
const (
	STOPBITS_ONE          StopBits = iota // 1 Stop bit
	STOPBITS_ONEPOINTFIVE                 // 1.5 Stop bits
	STOPBITS_TWO                          // 2 Stop bits
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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