serial

package module
v2.5.3 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

README

github.com/albenik/go-serial/v2

Go

A cross-platform serial library for Go. Forked from github.com/bugst/go-serial and now developing independently.

Many ideas are bein taken from github.com/bugst/go-serial and github.com/pyserial/pyserial.

Any PR-s are welcome.

INSTALL

Not work in GOPATH mode!!!

go get -u github.com/albenik/go-serial/v2

MacOS build note

  • Since version v2.1.0 the macos build requires IOKit as dependency and is only possible on Mac with cgo enabled.
  • Apple M1 (darwin/arm64) is supported. (Thanks to martinhpedersen)

Documentation and examples

See the godoc here: https://godoc.org/github.com/albenik/go-serial

License

The software is release under a BSD 3-clause license

Documentation

Overview

Example (SendAndReceive)

This example prints the list of serial ports and use the first one to send a string "10,20,30" and prints the response on the screen.

// Retrieve the port list
ports, err := serial.GetPortsList()
if err != nil {
	log.Fatal(err)
}
if len(ports) == 0 {
	log.Fatal("No serial ports found!")
}

// Print the list of detected ports
for _, port := range ports {
	fmt.Printf("Found port: %v\n", port)
}

// Open the first serial port detected at 9600bps N81
port, err := serial.Open(ports[0],
	serial.WithBaudrate(9600),
	serial.WithDataBits(8),
	serial.WithParity(serial.NoParity),
	serial.WithStopBits(serial.OneStopBit),
	serial.WithReadTimeout(1000),
	serial.WithWriteTimeout(1000),
	serial.WithHUPCL(false),
)
if err != nil {
	log.Fatal(err)
}

// Send the string "ABCDEF" to the serial port
n, err := fmt.Fprint(port, "ABCDEF")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Sent %v bytes\n", n)

// Read and print the response
buff := make([]byte, 100)
for {
	// Reads up to 100 bytes
	n, err := port.Read(buff)
	if err != nil {
		log.Fatal(err)
	}
	if n == 0 {
		fmt.Println("\nEOF")
		break
	}
	fmt.Printf("%v", string(buff[:n]))
}
Output:

Index

Examples

Constants

View Source
const (
	// NoParity disable parity control (default).
	NoParity Parity = iota
	// OddParity enable odd-parity check.
	OddParity
	// EvenParity enable even-parity check.
	EvenParity
	// MarkParity enable mark-parity (always 1) check.
	MarkParity
	// SpaceParity enable space-parity (always 0) check.
	SpaceParity

	// OneStopBit sets 1 stop bit (default).
	OneStopBit StopBits = iota
	// OnePointFiveStopBits sets 1.5 stop bits.
	OnePointFiveStopBits
	// TwoStopBits sets 2 stop bits.
	TwoStopBits
)
View Source
const FIONREAD = 0x541B

Variables

This section is empty.

Functions

func GetPortsList

func GetPortsList() ([]string, error)
Example
ports, err := serial.GetPortsList()
if err != nil {
	log.Fatal(err)
}
if len(ports) == 0 {
	fmt.Println("No serial ports found!")
} else {
	for _, port := range ports {
		fmt.Printf("Found port: %v\n", port)
	}
}
Output:

Types

type ModemStatusBits

type ModemStatusBits struct {
	CTS bool // ClearToSend status
	DSR bool // DataSetReady status
	RI  bool // RingIndicator status
	DCD bool // DataCarrierDetect status
}

ModemStatusBits contains all the modem status bits for a serial port (CTS, DSR, etc...). It can be retrieved with the Port.GetModemStatusBits() method.

type Option

type Option func(p *Port)

func WithBaudrate

func WithBaudrate(o int) Option

func WithDataBits

func WithDataBits(o int) Option

func WithHUPCL

func WithHUPCL(o bool) Option

func WithParity

func WithParity(o Parity) Option

func WithReadTimeout

func WithReadTimeout(o int) Option

func WithStopBits

func WithStopBits(o StopBits) Option

func WithWriteTimeout

func WithWriteTimeout(o int) Option

type Parity

type Parity int

Parity describes a serial port parity setting.

type Port

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

Port is the interface for a serial Port.

func Open

func Open(name string, opts ...Option) (*Port, error)

func (*Port) Close

func (p *Port) Close() error

func (*Port) GetModemStatusBits

func (p *Port) GetModemStatusBits() (*ModemStatusBits, error)
Example
// Open the first serial port detected at 9600bps N81
port, err := serial.Open("/dev/ttyACM1",
	serial.WithBaudrate(9600),
	serial.WithDataBits(8),
	serial.WithParity(serial.NoParity),
	serial.WithStopBits(serial.OneStopBit),
	serial.WithReadTimeout(1000),
	serial.WithWriteTimeout(1000),
)
if err != nil {
	log.Fatal(err)
}
defer port.Close()

count := 0
for count < 25 {
	status, err := port.GetModemStatusBits()
	if err != nil {
		log.Println(err) // DO NOT USER log.Fatal or `port.Close()` deferred call will never happened!
		return
	}
	fmt.Printf("Status: %+v\n", status)

	time.Sleep(time.Second)
	count++
	if count == 5 {
		if err = port.SetDTR(false); err != nil {
			log.Println(err)
			return
		}
		fmt.Println("Set DTR OFF")
	}
	if count == 10 {
		if err = port.SetDTR(true); err != nil {
			log.Println(err)
			return
		}
		fmt.Println("Set DTR ON")
	}
	if count == 15 {
		if err = port.SetRTS(false); err != nil {
			log.Println(err)
			return
		}
		fmt.Println("Set RTS OFF")
	}
	if count == 20 {
		if err = port.SetRTS(true); err != nil {
			log.Println(err)
			return
		}
		fmt.Println("Set RTS ON")
	}
}
Output:

func (*Port) Read

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

func (*Port) ReadyToRead

func (p *Port) ReadyToRead() (uint32, error)

func (*Port) Reconfigure

func (p *Port) Reconfigure(opts ...Option) error
Example
port, err := serial.Open("/dev/ttyACM0")
if err != nil {
	log.Fatal(err)
}
if err := port.Reconfigure(
	serial.WithBaudrate(9600),
	serial.WithDataBits(8),
	serial.WithParity(serial.NoParity),
	serial.WithStopBits(serial.OneStopBit),
	serial.WithReadTimeout(1000),
	serial.WithWriteTimeout(1000),
); err != nil {
	log.Fatal(err)
}
fmt.Println("Port set to 9600 N81")
Output:

func (*Port) ResetInputBuffer

func (p *Port) ResetInputBuffer() error

func (*Port) ResetOutputBuffer

func (p *Port) ResetOutputBuffer() error

func (*Port) SetDTR

func (p *Port) SetDTR(dtr bool) error

func (*Port) SetFirstByteReadTimeout

func (p *Port) SetFirstByteReadTimeout(t uint32) error

func (*Port) SetRTS

func (p *Port) SetRTS(rts bool) error

func (*Port) SetReadTimeout

func (p *Port) SetReadTimeout(t int) error

func (*Port) SetReadTimeoutEx

func (p *Port) SetReadTimeoutEx(t uint32, _ ...uint32) error

SetReadTimeoutEx Sets advanced timeouts. Second argument was forget here due refactoring and keeping now for backward compatibility. TODO Remove second argument in version v3.

func (*Port) SetWriteTimeout

func (p *Port) SetWriteTimeout(t int) error

func (*Port) String

func (p *Port) String() string

func (*Port) Write

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

type PortError

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

PortError is a platform independent error type for serial ports.

func (PortError) Cause

func (e PortError) Cause() error

Cause returns the cause for the error Deprecated: Use go1.13 error iterface Unwrap() instead.

func (PortError) Code

func (e PortError) Code() PortErrorCode

Code returns an identifier for the kind of error occurred.

func (PortError) EncodedErrorString

func (e PortError) EncodedErrorString() string

EncodedErrorString returns a string explaining the error code.

func (PortError) Error

func (e PortError) Error() string

Error returns the complete error code with details on the cause of the error.

func (PortError) Unwrap

func (e PortError) Unwrap() error

type PortErrorCode

type PortErrorCode int

PortErrorCode is a code to easily identify the type of error.

const (
	PortErrorUnknown PortErrorCode = iota
	// PortBusy the serial port is already in used by another process.
	PortBusy
	// PortNotFound the requested port doesn't exist.
	PortNotFound
	// InvalidSerialPort the requested port is not a serial port.
	InvalidSerialPort
	// PermissionDenied the user doesn't have enough priviledges.
	PermissionDenied
	// InvalidSpeed the requested speed is not valid or not supported.
	InvalidSpeed
	// InvalidDataBits the number of data bits is not valid or not supported.
	InvalidDataBits
	// InvalidParity the selected parity is not valid or not supported.
	InvalidParity
	// InvalidStopBits the selected number of stop bits is not valid or not supported.
	InvalidStopBits
	// InvalidTimeoutValue Invalid timeout value passed.
	InvalidTimeoutValue
	// ErrorEnumeratingPorts an error occurred while listing serial port.
	ErrorEnumeratingPorts
	// PortClosed the port has been closed while the operation is in progress.
	PortClosed
	// FunctionNotImplemented the requested function is not implemented.
	FunctionNotImplemented
	// OsError Operating system function error.
	OsError
	// WriteFailed Port write failed.
	WriteFailed
	// ReadFailed Port read failed.
	ReadFailed
)

type StopBits

type StopBits int

StopBits describe a serial port stop bits setting.

Directories

Path Synopsis
Package enumerator is a golang cross-platform library for USB serial port discovery.
Package enumerator is a golang cross-platform library for USB serial port discovery.

Jump to

Keyboard shortcuts

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