2024-02-05 01:50:43 +08:00
|
|
|
package serialport
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2024-02-12 23:27:08 +08:00
|
|
|
"fmt"
|
2014-09-29 11:32:31 +08:00
|
|
|
"io"
|
|
|
|
|
2020-05-04 04:40:15 +08:00
|
|
|
"go.bug.st/serial"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
|
|
|
"gobot.io/x/gobot/v2"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2024-02-12 23:27:08 +08:00
|
|
|
// configuration contains all changeable attributes of the driver.
|
|
|
|
type configuration struct {
|
|
|
|
name string
|
|
|
|
baudRate int
|
|
|
|
}
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
// Adaptor represents a Gobot Adaptor for the Serial Communication
|
2016-10-02 00:52:58 +08:00
|
|
|
type Adaptor struct {
|
2024-02-12 23:27:08 +08:00
|
|
|
port string
|
|
|
|
cfg *configuration
|
|
|
|
|
|
|
|
sp io.ReadWriteCloser
|
|
|
|
connected bool
|
|
|
|
connectFunc func(string, int) (io.ReadWriteCloser, error)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
// NewAdaptor returns a new adaptor given a port for the serial communication
|
2024-02-12 23:27:08 +08:00
|
|
|
func NewAdaptor(port string, opts ...optionApplier) *Adaptor {
|
|
|
|
cfg := configuration{
|
|
|
|
name: gobot.DefaultName("Serial"),
|
|
|
|
baudRate: 115200,
|
|
|
|
}
|
|
|
|
|
|
|
|
a := Adaptor{
|
|
|
|
cfg: &cfg,
|
2014-11-29 10:21:02 +08:00
|
|
|
port: port,
|
2024-02-12 23:27:08 +08:00
|
|
|
connectFunc: func(port string, baudRate int) (io.ReadWriteCloser, error) {
|
|
|
|
return serial.Open(port, &serial.Mode{BaudRate: baudRate})
|
2014-04-27 09:07:04 +08:00
|
|
|
},
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2024-02-12 23:27:08 +08:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o.apply(a.cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &a
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2016-10-02 00:52:58 +08:00
|
|
|
|
2024-02-12 23:27:08 +08:00
|
|
|
// WithName is used to replace the default name of the driver.
|
|
|
|
func WithName(name string) optionApplier {
|
|
|
|
return nameOption(name)
|
|
|
|
}
|
2016-12-01 23:08:56 +08:00
|
|
|
|
2024-02-12 23:27:08 +08:00
|
|
|
// WithName is used to replace the default name of the driver.
|
|
|
|
func WithBaudRate(baudRate int) optionApplier {
|
|
|
|
return baudRateOption(baudRate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the adaptors name
|
|
|
|
func (a *Adaptor) Name() string {
|
|
|
|
return a.cfg.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetName sets the adaptors name
|
|
|
|
// Deprecated: Please use option [serialport.WithName] instead.
|
|
|
|
func (a *Adaptor) SetName(n string) {
|
|
|
|
WithName(n).apply(a.cfg)
|
|
|
|
}
|
2016-12-01 23:08:56 +08:00
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
// Connect initiates a connection to the serial port.
|
2023-11-16 03:51:52 +08:00
|
|
|
func (a *Adaptor) Connect() error {
|
2024-02-12 23:27:08 +08:00
|
|
|
if a.connected {
|
|
|
|
return fmt.Errorf("serial port is already connected, try reconnect or run disconnect first")
|
|
|
|
}
|
|
|
|
|
|
|
|
sp, err := a.connectFunc(a.port, a.cfg.baudRate)
|
2023-11-16 03:51:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-20 08:17:14 +08:00
|
|
|
}
|
2016-12-01 23:08:56 +08:00
|
|
|
|
|
|
|
a.sp = sp
|
|
|
|
a.connected = true
|
2023-11-16 03:51:52 +08:00
|
|
|
return nil
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
// Finalize finalizes the adaptor by disconnect
|
|
|
|
func (a *Adaptor) Finalize() error {
|
|
|
|
return a.Disconnect()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
// Disconnect terminates the connection to the port.
|
2016-11-08 03:15:07 +08:00
|
|
|
func (a *Adaptor) Disconnect() error {
|
2014-12-23 23:00:23 +08:00
|
|
|
if a.connected {
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.sp.Close(); err != nil {
|
|
|
|
return err
|
2014-12-23 23:00:23 +08:00
|
|
|
}
|
|
|
|
a.connected = false
|
2014-11-20 08:17:14 +08:00
|
|
|
}
|
2016-11-08 03:15:07 +08:00
|
|
|
return nil
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
// Reconnect attempts to reconnect to the port. If the port is connected it will first close
|
|
|
|
// that connection and then establish a new connection.
|
|
|
|
func (a *Adaptor) Reconnect() error {
|
|
|
|
if a.connected {
|
|
|
|
if err := a.Disconnect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.Connect()
|
|
|
|
}
|
|
|
|
|
2024-02-12 23:27:08 +08:00
|
|
|
// Port returns the adaptors port
|
2024-02-05 01:50:43 +08:00
|
|
|
func (a *Adaptor) Port() string { return a.port }
|
|
|
|
|
|
|
|
// IsConnected returns the connection state
|
|
|
|
func (a *Adaptor) IsConnected() bool {
|
|
|
|
return a.connected
|
|
|
|
}
|
|
|
|
|
2024-02-12 23:27:08 +08:00
|
|
|
// SerialRead reads from the port to the given reference
|
|
|
|
func (a *Adaptor) SerialRead(pData []byte) (int, error) {
|
|
|
|
return a.sp.Read(pData)
|
2024-02-05 01:50:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SerialWrite writes to the port
|
2024-02-12 23:27:08 +08:00
|
|
|
func (a *Adaptor) SerialWrite(data []byte) (int, error) {
|
|
|
|
return a.sp.Write(data)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|