Skip to content

Commit

Permalink
feat: use serial lib
Browse files Browse the repository at this point in the history
  • Loading branch information
yyewolf committed Jan 16, 2024
1 parent 1dadd33 commit db9d17e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/creack/goselect v0.1.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -27,13 +28,15 @@ require (
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mikepb/go-serial v0.0.0-20201030162908-19fa9bf168fc // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.bug.st/serial v1.6.1 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0X
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -55,6 +57,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mikepb/go-serial v0.0.0-20201030162908-19fa9bf168fc h1:1lTV3ULswqs4Id7ufAHgKRqm+khmExlw9ivNNXUFxHw=
github.com/mikepb/go-serial v0.0.0-20201030162908-19fa9bf168fc/go.mod h1:/GJho3VaF1qyrexGicbl96+cYZXep0Xr2hxYYwY/cpE=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -87,6 +91,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
go.bug.st/serial v1.6.1 h1:VSSWmUxlj1T/YlRo2J104Zv3wJFrjHIl/T3NeruWAHY=
go.bug.st/serial v1.6.1/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
68 changes: 48 additions & 20 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package main
import (
"bytes"
"encoding/json"
"io/fs"
"log"
"net/http"
"os"
"strings"

"github.com/gin-gonic/gin"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"go.bug.st/serial"
)

type OpenRequest struct {
Expand Down Expand Up @@ -90,28 +89,57 @@ func checkRequest(c echo.Context) error {
}

func sendToACM(c echo.Context, data []byte) error {
// Write 1 to ttyACM*
files := os.DirFS("/dev")
err := fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
ports, err := serial.GetPortsList()
if err != nil {
log.Fatal(err)
}

if strings.HasPrefix(path, "ttyACM") {
f, err := os.OpenFile("/dev/"+path, os.O_RDWR, 0755)
if err != nil {
return err
}
for _, port := range ports {
mode := &serial.Mode{}

f.Write([]byte(data))
f.Close()
port, err := serial.Open(port, mode)
if err != nil {
logrus.Error(err)
continue
}

return nil
})
if err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
n, err := port.Write([]byte(data))
if err != nil {
logrus.Error(err)
continue
}
logrus.Debug("Wrote ", n, " bytes")
}

// Write 1 to ttyACM*
// files := os.DirFS("/dev")
// err := fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
// if err != nil {
// return err
// }

// if strings.HasPrefix(path, "ttyACM") {
// options := serial.RawOptions
// options.BitRate = 115200
// p, err := options.Open("/dev/tty")
// if err != nil {
// log.Panic(err)
// }

// _, err = s.Write([]byte(data))
// if err != nil {
// log.Fatal("Cannot write bytes. ", err)
// }

// f.Write([]byte())
// f.Close()
// }

// return nil
// })
// if err != nil {
// return c.JSON(400, gin.H{"error": err.Error()})
// }
return nil
}

Expand Down

0 comments on commit db9d17e

Please sign in to comment.