45 lines
790 B
Go
45 lines
790 B
Go
package serialinteraction
|
|
|
|
import (
|
|
"go.bug.st/serial"
|
|
"log"
|
|
)
|
|
|
|
type SerialConnection struct {
|
|
port serial.Port
|
|
portPath string
|
|
}
|
|
|
|
func (sc *SerialConnection) ListComports() []string {
|
|
ports, err := serial.GetPortsList()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return ports
|
|
}
|
|
|
|
func (sc *SerialConnection) GetPortPath() string {
|
|
return sc.portPath
|
|
}
|
|
|
|
func (sc *SerialConnection) Connect(portPath string, mode *serial.Mode) error {
|
|
port, err := serial.Open(portPath, mode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sc.port = port
|
|
return nil
|
|
}
|
|
|
|
func (sc *SerialConnection) Disconnect() {
|
|
sc.port.Close()
|
|
}
|
|
|
|
func (sc *SerialConnection) Write(data []byte) (int, error) {
|
|
return sc.port.Write(data)
|
|
}
|
|
|
|
func (sc *SerialConnection) Read(data []byte) (int, error) {
|
|
return sc.port.Read(data)
|
|
}
|