package main
import (
"fmt"
"github.com/ohisama/serial"
"github.com/ohisama/key"
"time"
)
func main() {
device := "COM5"
baud := 9600
fmt.Println("open", device, "at", baud)
port, err := serial.Open(device, baud)
if err != nil {
fmt.Println("open failed:", err)
return
}
defer port.Close()
fmt.Println("ready")
buf := make([]byte, 100)
c := make([]byte, 1)
for {
time.Sleep(200 * time.Millisecond)
c[0] = key.Scan()
if c[0] > 0 {
n, err := port.Write(c)
if err != nil {
fmt.Println("serial write error:", err)
}
n, err = port.Read(buf)
if err != nil {
fmt.Println("serial read error:", err)
break
}
fmt.Printf(string(buf[:n]))
}
}
}