From 2ca8e94068ede4e6a105db9c99fdb8955baf23d2 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Sun, 18 Jun 2023 21:14:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9rtu=E4=B8=BAseria?= =?UTF-8?q?l=EF=BC=8Crtu=E4=BB=85=E9=99=90=E4=BA=8Emodbus=EF=BC=8C?= =?UTF-8?q?=E6=98=AF=E5=BA=94=E7=94=A8=E5=B1=82=E5=8D=8F=E8=AE=AE=E8=80=8C?= =?UTF-8?q?=E9=9D=9E=E4=BC=A0=E8=BE=93=E5=B1=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- connection/rtu.go | 39 -------------------- connection/serial.go | 43 ++++++++++++++++++++++ connection/{rtu_addr.go => serial_addr.go} | 14 +++---- go.mod | 14 ++++--- go.sum | 38 +++++++++---------- server_rtu.go => server_serial.go | 5 ++- 6 files changed, 80 insertions(+), 73 deletions(-) delete mode 100644 connection/rtu.go create mode 100644 connection/serial.go rename connection/{rtu_addr.go => serial_addr.go} (59%) rename server_rtu.go => server_serial.go (83%) diff --git a/connection/rtu.go b/connection/rtu.go deleted file mode 100644 index 65ea790..0000000 --- a/connection/rtu.go +++ /dev/null @@ -1,39 +0,0 @@ -package connection - -import ( - "github.com/goburrow/serial" - "net" - "time" -) - -// RTUConn rtu connection wrapper -type RTUConn struct { - serial.Port - config *serial.Config -} - -// NewRTUConn 新建 RTUConn -func NewRTUConn(port serial.Port, cfg *serial.Config) net.Conn { - return &RTUConn{Port: port, config: cfg} -} - -func (c *RTUConn) LocalAddr() net.Addr { - cfg := c.config - return NewRTUAddr(cfg.Address, cfg.BaudRate, cfg.DataBits, cfg.StopBits, cfg.Parity) -} - -func (c *RTUConn) RemoteAddr() net.Addr { - return c.LocalAddr() -} - -func (c *RTUConn) SetDeadline(_ time.Time) error { - return nil -} - -func (c *RTUConn) SetReadDeadline(_ time.Time) error { - return nil -} - -func (c *RTUConn) SetWriteDeadline(_ time.Time) error { - return nil -} diff --git a/connection/serial.go b/connection/serial.go new file mode 100644 index 0000000..63daf84 --- /dev/null +++ b/connection/serial.go @@ -0,0 +1,43 @@ +package connection + +import ( + "git.noahlan.cn/noahlan/ntool/nlog" + "github.com/goburrow/serial" + "net" + "time" +) + +// SerialConn serial connection wrapper +type SerialConn struct { + serial.Port + config *serial.Config +} + +// NewSerialConn 新建 SerialConn +func NewSerialConn(port serial.Port, cfg *serial.Config) net.Conn { + return &SerialConn{Port: port, config: cfg} +} + +func (c *SerialConn) LocalAddr() net.Addr { + cfg := c.config + return NewSerialAddr(cfg.Address, cfg.BaudRate, cfg.DataBits, cfg.StopBits, cfg.Parity) +} + +func (c *SerialConn) RemoteAddr() net.Addr { + return c.LocalAddr() +} + +func (c *SerialConn) SetDeadline(_ time.Time) error { + nlog.Error("SetDeadline in serial connection was not supported.") + return nil +} + +func (c *SerialConn) SetReadDeadline(_ time.Time) error { + nlog.Error("SetReadDeadline in serial connection was not supported.") + return nil +} + +func (c *SerialConn) SetWriteDeadline(_ time.Time) error { + nlog.Error("SetWriteDeadline in serial connection was not supported.") + return nil +} diff --git a/connection/rtu_addr.go b/connection/serial_addr.go similarity index 59% rename from connection/rtu_addr.go rename to connection/serial_addr.go index eabaa6b..9443af3 100644 --- a/connection/rtu_addr.go +++ b/connection/serial_addr.go @@ -5,9 +5,9 @@ import ( "net" ) -const RTUNetwork = "rtu" +const SerialNetwork = "serial" -type RTUAddr struct { +type SerialAddr struct { Address string BaudRate int DataBits int @@ -15,8 +15,8 @@ type RTUAddr struct { Parity string } -func NewRTUAddr(addr string, rate int, dataBits int, stopBits int, parity string) net.Addr { - return &RTUAddr{ +func NewSerialAddr(addr string, rate int, dataBits int, stopBits int, parity string) net.Addr { + return &SerialAddr{ Address: addr, BaudRate: rate, DataBits: dataBits, @@ -25,11 +25,11 @@ func NewRTUAddr(addr string, rate int, dataBits int, stopBits int, parity string } } -func (r *RTUAddr) Network() string { - return RTUNetwork +func (r *SerialAddr) Network() string { + return SerialNetwork } // String form of address (for example, "/dev/ttyS0|19200|8|1|N", "com1|19200|8|1|N") -func (r *RTUAddr) String() string { +func (r *SerialAddr) String() string { return fmt.Sprintf("%s|%d|%d|%d", r.Address, r.BaudRate, r.DataBits, r.StopBits) } diff --git a/go.mod b/go.mod index b3e566b..a33dfb3 100644 --- a/go.mod +++ b/go.mod @@ -4,21 +4,25 @@ go 1.20 require ( github.com/gorilla/websocket v1.5.0 - github.com/panjf2000/ants/v2 v2.7.5 // indirect + github.com/panjf2000/ants/v2 v2.7.5 ) -require google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 +require ( + git.noahlan.cn/noahlan/ntool v1.0.1 + github.com/goburrow/serial v0.1.0 + google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 +) require ( - git.noahlan.cn/noahlan/ntool v1.0.1 // indirect - github.com/fatih/color v1.15.0 // indirect - github.com/goburrow/serial v0.1.0 // indirect github.com/gofrs/uuid/v5 v5.0.0 // indirect + github.com/gookit/color v1.5.3 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect go.opentelemetry.io/otel v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.16.0 // indirect golang.org/x/crypto v0.10.0 // indirect golang.org/x/sys v0.9.0 // indirect + golang.org/x/term v0.9.0 // indirect golang.org/x/text v0.10.0 // indirect ) diff --git a/go.sum b/go.sum index c101ffe..3ce1fc9 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,9 @@ git.noahlan.cn/noahlan/ntool v1.0.1 h1:c2sw65BmyqQHKn3+s2Bjdkz9AtSKI4e1WAkRi/CG1oc= git.noahlan.cn/noahlan/ntool v1.0.1/go.mod h1:nLp9j4lz203kNpVFrNbT25A2QHbx235gXv+Ut1bX8E8= -git.noahlan.cn/noahlan/ntools-go/core v1.1.3 h1:n4z0KaXmX/fmobavxCMc2vGJDoStbhNbm8AZugPEPGg= -git.noahlan.cn/noahlan/ntools-go/core v1.1.3/go.mod h1:pmwee9V76Cyp6nVr3dPj5TpePLvRpc8C0ZgAzFIFAKU= 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= -github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= -github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= -github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA= github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA= @@ -16,16 +12,16 @@ github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE= +github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= -github.com/mattn/go-isatty v0.0.17/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/panjf2000/ants/v2 v2.7.3 h1:rHQ0hH0DQvuNUqqlWIMJtkMcDuL1uQAfpX2mIhQ5/s0= -github.com/panjf2000/ants/v2 v2.7.3/go.mod h1:KIBmYG9QQX5U2qzFP/yQJaq/nSb6rahS9iEHkrCMgM8= +github.com/panjf2000/ants/v2 v2.7.5 h1:/vhh0Hza9G1vP1PdCj9hl6MUzCRbmtcTJL0OsnmytuU= github.com/panjf2000/ants/v2 v2.7.5/go.mod h1:KIBmYG9QQX5U2qzFP/yQJaq/nSb6rahS9iEHkrCMgM8= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -35,25 +31,27 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM= -go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU= +github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +go.opentelemetry.io/otel v1.16.0 h1:Z7GVAX/UkAXPKsy94IU+i6thsQS4nb7LviLpnaNeW8s= go.opentelemetry.io/otel v1.16.0/go.mod h1:vl0h9NUa1D5s1nv3A5vZOYWn8av4K8Ml6JDeHrT/bx4= -go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY= -go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M= -go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8= +go.opentelemetry.io/otel/metric v1.16.0 h1:RbrpwVG1Hfv85LgnZ7+txXioPDoh6EdbZHo26Q3hqOo= +go.opentelemetry.io/otel/sdk v1.16.0 h1:Z1Ok1YsijYL0CSJpHt4cS3wDDh7p572grzNrBMiMWgE= +go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZEu5MQs= go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0= -golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= -golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= -golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28= +golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo= +golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= diff --git a/server_rtu.go b/server_serial.go similarity index 83% rename from server_rtu.go rename to server_serial.go index 34ef30d..a470aee 100644 --- a/server_rtu.go +++ b/server_serial.go @@ -7,7 +7,8 @@ import ( "sync" ) -func (ngin *Engine) ServerRTU(conf serial.Config) error { +// ListenSerial 监听串口消息 +func (ngin *Engine) ListenSerial(conf serial.Config) error { err := ngin.setup() if err != nil { nlog.Errorf("%s failed to setup server, err:%v", ngin.LogPrefix(), err) @@ -29,7 +30,7 @@ func (ngin *Engine) ServerRTU(conf serial.Config) error { var wg sync.WaitGroup wg.Add(1) - ngin.handle(connection.NewRTUConn(port, &conf)) + ngin.handle(connection.NewSerialConn(port, &conf)) go func() { for {