From 530e31c423015109102f73f2541efd8a9031653b Mon Sep 17 00:00:00 2001 From: Godopu Date: Thu, 9 Dec 2021 11:53:30 +0900 Subject: [PATCH] commit 2021-12-09]11:53:28 --- serial/serial.go | 173 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 serial/serial.go diff --git a/serial/serial.go b/serial/serial.go new file mode 100644 index 0000000..7d779bb --- /dev/null +++ b/serial/serial.go @@ -0,0 +1,173 @@ +package serial + +import ( + "bufio" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "time" + + "github.com/jacobsa/go-serial/serial" + "github.com/rjeczalik/notify" +) + +func readJsonFromSerial(obj map[string]interface{}, decoder *json.Decoder) error { + err := decoder.Decode(&obj) + if err != nil { + return err + } + + return nil +} + +func discoverUSB() string { + fmt.Println("Waiting USB") + ch := make(chan notify.EventInfo, 1) + if err := notify.Watch("/dev", ch, notify.Create, notify.Remove); err != nil { + log.Fatalln(err) + } + defer notify.Stop(ch) + + for e := range ch { + if strings.Contains(e.Path(), "/dev/ttyACM") { + if e.Event() == notify.Create { + log.Println("Got event:", e.Path()) + return e.Path() + } + } + } + + return "" +} + +func initUSB() (string, error) { + fs, err := ioutil.ReadDir("/dev") + if err != nil { + return "", err + } + + for _, f := range fs { + if strings.Contains(f.Name(), "ttyACM") { + return filepath.Join("/dev", f.Name()), nil + } + } + + return "", errors.New("USB Not found") +} + +func Run() { + dev, err := initUSB() + if err != nil { + if err.Error() == "USB Not found" { + dev = discoverUSB() + } else { + log.Fatalln(err.Error()) + } + } + + fmt.Println("discover: ", dev) + time.Sleep(time.Second) + + log.Println("changing the mod of file") + cmd := exec.Command("chmod", "a+rw", dev) + b, _ := cmd.CombinedOutput() + fmt.Println(string(b)) + + // Set up options. + options := serial.OpenOptions{ + PortName: dev, + BaudRate: 9600, + DataBits: 8, + StopBits: 1, + MinimumReadSize: 16, + } + + // Open the port. + port, err := serial.Open(options) + if err != nil { + log.Fatalf("serial.Open: %v", err) + } + + // Make sure to close it later. + defer port.Close() + + // Write 4 bytes to the port. + // b := []byte{0x00, 0x01, 0x02, 0x03} + // n, err := port.Write(b) + // if err != nil { + // log.Fatalf("port.Write: %v", err) + // } + + // fmt.Println("Wrote", n, "bytes.") + // var tokenByte string + + reader := bufio.NewReader(port) + // decoder := json.NewDecoder(port) + encoder := json.NewEncoder(port) + command := map[string]interface{}{} + command["code"] = 1 + command["light"] = 0 + + var data string + go func() { + for { + b, _, _ := reader.ReadLine() + recvObj := map[string]interface{}{} + err := json.Unmarshal(b, &recvObj) + // err = readJsonFromSerial(recvObj, decoder) + if err != nil { + if err.Error() == "EOF" { + return + } + + fmt.Println("error: ", string(b)) + } + + data = string(b) + // fmt.Println("line : ", recvObj) + } + }() + + cmdReader := bufio.NewReader(os.Stdin) + for { + fmt.Print("> ") + cmd, _, _ := cmdReader.ReadLine() + cmdTkns := strings.Split(string(cmd), " ") + + if cmdTkns[0] == "light" { + command["code"] = 1 + if cmdTkns[1] == "on" { + command["light"] = 100 + } else { + command["light"] = 0 + } + } else if cmdTkns[0] == "fan" { + command["code"] = 2 + if cmdTkns[1] == "on" { + command["status"] = 1 + } else { + command["status"] = 0 + } + } else if cmdTkns[0] == "servo" { + command["code"] = 3 + angle, err := strconv.Atoi(cmdTkns[1]) + if err != nil { + continue + } + command["angle"] = angle + } else if cmdTkns[0] == "print" { + fmt.Println(data) + } + err := encoder.Encode(command) + if err != nil { + panic(err) + } + } +}