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 ¶
- 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 ¶
func GetPortsList ¶
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 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 ¶
// 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) ReadyToRead ¶
func (*Port) Reconfigure ¶
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 (*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. |