2024-02-05 01:50:43 +08:00
|
|
|
package serialport
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
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-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 {
|
2014-11-29 10:21:02 +08:00
|
|
|
name string
|
|
|
|
port string
|
|
|
|
sp io.ReadWriteCloser
|
|
|
|
connected bool
|
2014-12-23 23:00:23 +08:00
|
|
|
connect func(string) (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
|
2016-10-02 00:52:58 +08:00
|
|
|
func NewAdaptor(port string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2024-02-05 01:50:43 +08:00
|
|
|
name: gobot.DefaultName("Serial"),
|
2014-11-29 10:21:02 +08:00
|
|
|
port: port,
|
2014-12-23 23:00:23 +08:00
|
|
|
connect: func(port string) (io.ReadWriteCloser, error) {
|
2017-05-13 17:41:39 +08:00
|
|
|
return serial.Open(port, &serial.Mode{BaudRate: 115200})
|
2014-04-27 09:07:04 +08:00
|
|
|
},
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 00:52:58 +08:00
|
|
|
|
2016-12-01 23:08:56 +08:00
|
|
|
// Name returns the Adaptor's name
|
|
|
|
func (a *Adaptor) Name() string { return a.name }
|
|
|
|
|
|
|
|
// SetName sets the Adaptor's name
|
2016-10-02 00:52:58 +08:00
|
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
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 {
|
|
|
|
sp, err := a.connect(a.Port())
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Port returns the Adaptor's port
|
|
|
|
func (a *Adaptor) Port() string { return a.port }
|
|
|
|
|
|
|
|
// IsConnected returns the connection state
|
|
|
|
func (a *Adaptor) IsConnected() bool {
|
|
|
|
return a.connected
|
|
|
|
}
|
|
|
|
|
|
|
|
// SerialRead reads from the port
|
|
|
|
func (a *Adaptor) SerialRead(p []byte) (int, error) {
|
|
|
|
return a.sp.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SerialWrite writes to the port
|
|
|
|
func (a *Adaptor) SerialWrite(p []byte) (int, error) {
|
|
|
|
return a.sp.Write(p)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|