From 2d51207a4ed44f3b450b37a68cc27ea0cea468fa Mon Sep 17 00:00:00 2001 From: Godopu Date: Thu, 9 Dec 2021 12:45:50 +0900 Subject: [PATCH] commit 2021-12-09]12:45:47 --- puserial/serial.go | 164 +++++++++++++++++++++++++++++++++++++++++++++ serial/serial.go | 19 ++---- 2 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 puserial/serial.go diff --git a/puserial/serial.go b/puserial/serial.go new file mode 100644 index 0000000..d959f92 --- /dev/null +++ b/puserial/serial.go @@ -0,0 +1,164 @@ +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 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) + } + } +} diff --git a/serial/serial.go b/serial/serial.go index 7d779bb..1f51ee6 100644 --- a/serial/serial.go +++ b/serial/serial.go @@ -1,4 +1,4 @@ -package serial +package puserial import ( "bufio" @@ -18,16 +18,7 @@ import ( "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 { +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 { @@ -47,7 +38,7 @@ func discoverUSB() string { return "" } -func initUSB() (string, error) { +func InitUSB() (string, error) { fs, err := ioutil.ReadDir("/dev") if err != nil { return "", err @@ -63,10 +54,10 @@ func initUSB() (string, error) { } func Run() { - dev, err := initUSB() + dev, err := InitUSB() if err != nil { if err.Error() == "USB Not found" { - dev = discoverUSB() + dev = DiscoverUSB() } else { log.Fatalln(err.Error()) }