A Go package to allow you easily to read and write from the serial port uses channel or API call. This package can help you to easily make the serial network to remotely control your device. This package depends on github.com/tarm/serial and github.com/gorilla/websocket.
package main
import (
"github.com/srleohung/serialnetwork"
"log"
)
func main() {
d := serialnetwork.NewDevice()
// Initialize and establish a serial port connection
d.Init(serialnetwork.Config{Name: "/dev/ttyUSB0", Baud: 115200, RxBuffer: 1})
// Get serial read and write channel
rx := d.GetRxChannel()
tx := d.GetTxChannel()
// Use channels to read and write
tx <- []byte("test")
log.Printf("% x", <-rx)
}
// Set the serial read format
d.SetRxFormat([]serialnetwork.RxFormat{
{StartByte: []byte{0x01}, EndByte: []byte{0x09}},
{StartByte: []byte{0x01}, LengthByteIndex: 1, LengthByteMissing: 7},
{StartByte: []byte{0x01}, LengthFixed: 9},
})
// Test sample read format
message := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}
bytes := d.TestRxFormats(message)
if len(bytes) != len(message) {
log.Print("Unknown message in Rx format.")
}
// Test serial read format
message = <-d.GetRxChannel()
bytes = d.TestRxFormats(message)
if len(bytes) != len(message) {
log.Print("Unknown message in Rx format.")
}
- StartByte
- Type: []byte
- EndByte
- Type: []byte
- LengthByteIndex
- Type: int
- LengthByteMissing
- Type: int
- LengthFixed
- Type: int
- LengthHighByteIndex
- Type: int
- LengthDecoder
- Type: string
- Option: bcd
- Block check character (BCC)
- import "github.com/srleohung/serialnetwork/encoding/bcc"
- Encode
- import "github.com/srleohung/serialnetwork/encoding/bcc"
- Binary coded decimal (BCD)
- import "github.com/srleohung/serialnetwork/encoding/bcd"
- Encode
- Decode
- import "github.com/srleohung/serialnetwork/encoding/bcd"
s.NewWebSocketServer(":9876")
d.NewWebSocketClient("localhost:9876")
- Serial device example
- Serial server example
- Return error messages and device connection status to the server.
- Use RWMutex to prevent error signals.
- Upgrade device automatically reconnects.
- The Init device on the server is not working properly.
- Add other encodings for use.
- Upgrade websocket to automatically reconnect.
This package is startup of a unmanned store projects. A host to controls many vending machine, camera, payment device. This package is for many different devices connected to one server.