48 lines
817 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) error {
mode := &serial.Mode{
BaudRate: 115200,
}
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)
}