commit c9724728219c1850093805bef56d6bf04a9ef016 Author: Godopu Date: Thu Dec 9 11:35:44 2021 +0900 commit 2021-12-09]11:35:22 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5acbfe7 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module git.godopu.net/lab/etri-smartfarm-poc-controller-serial + +go 1.17 + +require ( + github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 + github.com/rjeczalik/notify v0.9.2 +) + +require golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b252394 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 h1:G2ztCwXov8mRvP0ZfjE6nAlaCX2XbykaeHdbT6KwDz0= +github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4/go.mod h1:2RvX5ZjVtsznNZPEt4xwJXNJrM3VTZoQf7V6gk0ysvs= +github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8= +github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM= +golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 h1:bit1t3mgdR35yN0cX0G8orgLtOuyL9Wqxa1mccLB0ig= +golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/main.go b/main.go new file mode 100644 index 0000000..4ef8c0a --- /dev/null +++ b/main.go @@ -0,0 +1,177 @@ +package main + +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 msgSender(did string) { + +} + +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 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 main() { + 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) + } + } +}