diff --git a/.gitignore b/.gitignore index 9257b4c..2b34384 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ goAdapter - +.idea plugin/ config/ selfpara/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/goAdapter.iml b/.idea/goAdapter.iml deleted file mode 100644 index c956989..0000000 --- a/.idea/goAdapter.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 28a804d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index dcb2710..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/go.mod b/go.mod index 086af51..f6d281a 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/gin-gonic/gin v1.6.3 github.com/go-ole/go-ole v1.2.4 // indirect + github.com/goburrow/serial v0.1.0 github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/jonboulle/clockwork v0.2.0 // indirect github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible @@ -25,7 +26,9 @@ require ( github.com/stretchr/testify v1.5.1 // indirect github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 github.com/tebeka/strftime v0.1.5 // indirect - github.com/thinkgos/gomodbus v1.5.2 + github.com/thinkgos/gomodbus/v2 v2.2.1 + github.com/thinkgos/mb v0.0.1 + github.com/thinkgos/timing/v4 v4.1.1 // indirect github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7 github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect diff --git a/go.sum b/go.sum index 4b1a35a..6977282 100644 --- a/go.sum +++ b/go.sum @@ -89,10 +89,15 @@ github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUY github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/tebeka/strftime v0.1.5 h1:1NQKN1NiQgkqd/2moD6ySP/5CoZQsKa1d3ZhJ44Jpmg= github.com/tebeka/strftime v0.1.5/go.mod h1:29/OidkoWHdEKZqzyDLUyC+LmgDgdHo4WAFCDT7D/Ig= -github.com/thinkgos/gomodbus v1.5.2 h1:zmFEzXILpCtBYB8FwcAuLnb8Rrr3NcdX8oukmyB9hwE= -github.com/thinkgos/gomodbus v1.5.2/go.mod h1:HX6B+W3xV/oKinltblD+aqr9LnQuh9mWT4kMul5hYvE= -github.com/thinkgos/timing v1.1.2 h1:Gkpht/YViWFl/ADWy85VAQygaKcn8K8q/WLWcE5Rfag= -github.com/thinkgos/timing v1.1.2/go.mod h1:zS1qzRn2ISTjI1xJc6AHNfWAvCtc7FylwWjt32L8qvo= +github.com/thinkgos/gomodbus/v2 v2.2.0/go.mod h1:YynqZtDx2kFRhA+Ajy9qtmWOUIs5BOi3EaX6WQwH/jo= +github.com/thinkgos/gomodbus/v2 v2.2.1 h1:eFTeFvkN8z7DuKUnExPzJs3eNE+O0kgoRRDD7cnzmSA= +github.com/thinkgos/gomodbus/v2 v2.2.1/go.mod h1:YynqZtDx2kFRhA+Ajy9qtmWOUIs5BOi3EaX6WQwH/jo= +github.com/thinkgos/mb v0.0.1 h1:cXhxZRBJ2K64Ac9CAwjX6K9qx4vwgxmWFSszSXboQ38= +github.com/thinkgos/mb v0.0.1/go.mod h1:Aew9EaLHFevy9zPT4VKpv/mf5SUf6475fuDVB8DFJhY= +github.com/thinkgos/timing/v4 v4.1.0 h1:A91sdbxRa2VGgqEnbi7BW3Atqnk9iG3b9AAzTyjEOJE= +github.com/thinkgos/timing/v4 v4.1.0/go.mod h1:UJHH/waV/JvqmHbK8phLp9w2EUgRVN6oDAlr8k+WAZY= +github.com/thinkgos/timing/v4 v4.1.1 h1:/Fte/3ChyO21ucWsrjTT/iaHz6h9elzOiw7zp/4Hh18= +github.com/thinkgos/timing/v4 v4.1.1/go.mod h1:UJHH/waV/JvqmHbK8phLp9w2EUgRVN6oDAlr8k+WAZY= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= diff --git a/httpServer/routerModbus.go b/httpServer/routerModbus.go index 9636866..854ac83 100644 --- a/httpServer/routerModbus.go +++ b/httpServer/routerModbus.go @@ -5,46 +5,47 @@ import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" - modbus "github.com/thinkgos/gomodbus" - "github.com/thinkgos/gomodbus/mb" + "github.com/goburrow/serial" + modbus "github.com/thinkgos/gomodbus/v2" + "github.com/thinkgos/mb" "net/http" "strconv" "strings" "time" ) -type MbParam struct{ - Addr byte `json:"Addr"` - RegAddr uint16 `json:"RegAddr"` - RegCnt uint16 `json:"RegCnt"` - Data string `json:"Data"` +type MbParam struct { + Addr byte `json:"Addr"` + RegAddr uint16 `json:"RegAddr"` + RegCnt uint16 `json:"RegCnt"` + Data string `json:"Data"` } -var client *mb.Client -var mbParam MbParam +var client *mb.Client +var mbParam MbParam - -func NewMbParam(addr byte,regAddr,regCnt uint16)*MbParam{ +func NewMbParam(addr byte, regAddr, regCnt uint16) *MbParam { return &MbParam{ - Addr :addr, - RegAddr:regAddr, - RegCnt :regCnt, + Addr: addr, + RegAddr: regAddr, + RegCnt: regCnt, } } -func mbParamOpenPort(port string) bool{ +func mbParamOpenPort(port string) bool { status := false //调用RTUClientProvider的构造函数,返回结构体指针 - p := modbus.NewRTUClientProvider() - p.Address = port - p.BaudRate = 115200 - p.DataBits = 8 - p.Parity = "N" - p.StopBits = 1 - p.Timeout = 100 * time.Millisecond + p := modbus.NewRTUClientProvider(modbus.WithSerialConfig(serial.Config{ + Address: port, + BaudRate: 115200, + DataBits: 8, + StopBits: 1, + Parity: "N", + Timeout: 100 * time.Millisecond, + })) - client = mb.NewClient(p) + client = mb.New(p) client.LogMode(true) err := client.Start() if err != nil { @@ -56,7 +57,7 @@ func mbParamOpenPort(port string) bool{ return status } -func mbParamReadHoldReg(slaveAddr byte,regAddr uint16,regCnt uint16)[]uint16{ +func mbParamReadHoldReg(slaveAddr byte, regAddr uint16, regCnt uint16) []uint16 { value, err := client.ReadHoldingRegisters(slaveAddr, regAddr, regCnt) if err != nil { fmt.Println("readHoldErr,", err) @@ -67,9 +68,8 @@ func mbParamReadHoldReg(slaveAddr byte,regAddr uint16,regCnt uint16)[]uint16{ return value } - -func mbParamWriteMutilReg(slaveAddr byte,regAddr uint16,regCnt uint16,data []byte) error{ - err := client.WriteMultipleRegisters(slaveAddr,regAddr,regCnt,data) +func mbParamWriteMutilReg(slaveAddr byte, regAddr uint16, regCnt uint16, data []byte) error { + err := client.WriteMultipleRegistersBytes(slaveAddr, regAddr, regCnt, data) if err != nil { fmt.Println("writeMulRegErr,", err) } @@ -77,121 +77,120 @@ func mbParamWriteMutilReg(slaveAddr byte,regAddr uint16,regCnt uint16,data []byt return err } -func apiReadHoldReg(context *gin.Context){ +func apiReadHoldReg(context *gin.Context) { - aParam := struct{ - Code string `json:"Code"` - Message string `json:"Message"` - Data string `json:"Data"` + aParam := struct { + Code string `json:"Code"` + Message string `json:"Message"` + Data string `json:"Data"` }{ - Code:"1", - Message:"", - Data:"", + Code: "1", + Message: "", + Data: "", } - bodyBuf := make([]byte,1024) - n,_ := context.Request.Body.Read(bodyBuf) + bodyBuf := make([]byte, 1024) + n, _ := context.Request.Body.Read(bodyBuf) fmt.Println(string(bodyBuf[:n])) //获取读寄存器的参数 rMbParam := &MbParam{} - err := json.Unmarshal(bodyBuf[:n],rMbParam) + err := json.Unmarshal(bodyBuf[:n], rMbParam) if err != nil { - fmt.Println("rMbParam json unMarshall err,",err) + fmt.Println("rMbParam json unMarshall err,", err) aParam.Code = "1" aParam.Message = "json unMarshall err" - sJson,_ := json.Marshal(aParam) + sJson, _ := json.Marshal(aParam) - context.String(http.StatusOK,string(sJson)) + context.String(http.StatusOK, string(sJson)) return } - mRegValue := mbParamReadHoldReg(rMbParam.Addr,rMbParam.RegAddr,rMbParam.RegCnt) - fmt.Printf("mRegValue %#v\n",mRegValue) + mRegValue := mbParamReadHoldReg(rMbParam.Addr, rMbParam.RegAddr, rMbParam.RegCnt) + fmt.Printf("mRegValue %#v\n", mRegValue) var sRegValue string = "" - for i:=0;i