Bidirectional msgpack wire protocol with unpack support
This commit is contained in:
@@ -25,22 +25,8 @@ func Open(portName string) (serial.Port, error) {
|
||||
return serial.Open(portName, &serial.Mode{BaudRate: 115200})
|
||||
}
|
||||
|
||||
func SendByte(portName string, b byte) error {
|
||||
port, err := Open(portName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening %s: %w", portName, err)
|
||||
}
|
||||
defer port.Close()
|
||||
|
||||
_, err = port.Write([]byte{b})
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing to %s: %w", portName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendByteAndRead sends a byte and reads the response with a timeout.
|
||||
func SendByteAndRead(portName string, b byte, timeout time.Duration) ([]byte, error) {
|
||||
// SendAndReceive sends data and reads the response with a timeout.
|
||||
func SendAndReceive(portName string, data []byte, timeout time.Duration) ([]byte, error) {
|
||||
port, err := Open(portName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening %s: %w", portName, err)
|
||||
@@ -49,7 +35,7 @@ func SendByteAndRead(portName string, b byte, timeout time.Duration) ([]byte, er
|
||||
|
||||
port.SetReadTimeout(timeout)
|
||||
|
||||
_, err = port.Write([]byte{b})
|
||||
_, err = port.Write(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("writing to %s: %w", portName, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user