Inital Working Dummy for basic Serial Communication

This commit is contained in:
2025-03-22 15:21:51 +01:00
commit 4d992ef9be
6 changed files with 289 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
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)
}
+30
View File
@@ -0,0 +1,30 @@
package serialinteraction
type SerialMockConnection struct {
portPath string
}
func (sc *SerialMockConnection) ListComports() []string {
return []string{"/dev/ttyUSB0", "/dev/ttyUSB1"}
}
func (sc *SerialMockConnection) GetPortPath() string {
return "/dev/ttyUSB0"
}
func (sc *SerialMockConnection) Connect(portPath string) error {
return nil
}
func (sc *SerialMockConnection) Disconnect() {
}
func (sc *SerialMockConnection) Write(data []byte) (int, error) {
return 10, nil
}
func (sc *SerialMockConnection) Read(data []byte) (int, error) {
msg := "Viele Lustige Daten"
data = []byte(msg)
return len(msg), nil
}