README
¶
github.com/albenik/go-serial/v2
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.
Output:
Index ¶
- Constants
- func GetPortsList() ([]string, error)
- type ModemStatusBits
- type Option
- type Parity
- type Port
- func (p *Port) Close() error
- func (p *Port) GetModemStatusBits() (*ModemStatusBits, error)
- func (p *Port) Read(b []byte) (int, error)
- func (p *Port) ReadyToRead() (uint32, error)
- func (p *Port) Reconfigure(opts ...Option) error
- func (p *Port) ResetInputBuffer() error
- func (p *Port) ResetOutputBuffer() error
- func (p *Port) SetDTR(dtr bool) error
- func (p *Port) SetFirstByteReadTimeout(t uint32) error
- func (p *Port) SetRTS(rts bool) error
- func (p *Port) SetReadTimeout(t int) error
- func (p *Port) SetReadTimeoutEx(t uint32, _ ...uint32) error
- func (p *Port) SetWriteTimeout(t int) error
- func (p *Port) String() string
- func (p *Port) Write(b []byte) (int, error)
- type PortError
- type PortErrorCode
- type StopBits
Examples ¶
Constants ¶
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 )
const FIONREAD = 0x541B
Variables ¶
This section is empty.
Functions ¶
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 WithDataBits ¶
func WithParity ¶
func WithReadTimeout ¶
func WithStopBits ¶
func WithWriteTimeout ¶
type Port ¶
type Port struct {
// contains filtered or unexported fields
}
Port is the interface for a serial Port.
func (*Port) GetModemStatusBits ¶
func (p *Port) GetModemStatusBits() (*ModemStatusBits, error)
Example ¶
Output:
func (*Port) ReadyToRead ¶
func (*Port) ResetInputBuffer ¶
func (*Port) ResetOutputBuffer ¶
func (*Port) SetFirstByteReadTimeout ¶
func (*Port) SetReadTimeout ¶
func (*Port) SetReadTimeoutEx ¶
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 ¶
type PortError ¶
type PortError struct {
// contains filtered or unexported fields
}
PortError is a platform independent error type for serial ports.
func (PortError) Cause ¶
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 ¶
EncodedErrorString returns a string explaining the error code.
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 )
Source Files
¶
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. |