Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soypat/cereal
Serial port abstraction creation for bugst, sers, goburrow and tarm serial libraries.
https://github.com/soypat/cereal
go golang hal hardware-abstraction serial termios
Last synced: about 2 months ago
JSON representation
Serial port abstraction creation for bugst, sers, goburrow and tarm serial libraries.
- Host: GitHub
- URL: https://github.com/soypat/cereal
- Owner: soypat
- License: bsd-2-clause
- Created: 2023-06-18T21:13:47.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-27T14:28:23.000Z (about 1 year ago)
- Last Synced: 2024-10-12T09:11:45.465Z (3 months ago)
- Topics: go, golang, hal, hardware-abstraction, serial, termios
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-tinygo - `cereal` - All-you-need serial device tooling integrating bugst, goburrow, tarm and sers serial libraries. (Wireless Communication / WebSockets)
README
# cereal
Serial port abstraction creation for bugst, sers, goburrow and tarm serial libraries.This allows for:
- Easily diagnosing if a bug is an issue with a certain library or not.
- Modularizing a program so that the underlying serial implementation can be
easily swapped out## Example
Below is a program that writes and reads from a serial port.
The library used to access the port is selected by the program user via a flag.
```sh
program -seriallib=tarm
``````go
package mainimport (
"flag"
"log"
"time""github.com/soypat/cereal"
)func main() {
availableLibs := map[string]cereal.Opener{
cereal.Bugst{}.String(): cereal.Bugst{},
cereal.Tarm{}.String(): cereal.Tarm{},
cereal.Goburrow{}.String(): cereal.Goburrow{},
cereal.Sers{}.String(): cereal.Sers{},
}
flagSerial := flag.String("seriallib", "bugst", "Serial library to use: bugst, tarm, goburrow, sers")
flag.Parse()
serial, ok := availableLibs[*flagSerial]
if !ok {
flag.PrintDefaults()
log.Fatalf("Invalid serial library: %s\n", *flagSerial)
}port, err := serial.OpenPort("/dev/ttyUSB0", cereal.Mode{
BaudRate: 9600,
DataBits: 8,
Parity: cereal.ParityNone,
StopBits: cereal.StopBits1,
})
if err != nil {
log.Fatal(err)
}
defer port.Close()// Do something with port
readBuffer := make([]byte, 128)
for {
_, err := port.Write([]byte("Hello\n"))
if err != nil {
log.Fatal(err)
}
n, err := port.Read(readBuffer)
if err != nil {
log.Fatal(err)
}
log.Printf("Read %d bytes: %q\n", n, string(readBuffer[:n]))
time.Sleep(time.Second)
}
}
```