upgrade modbus to v2

remove ignore .idea
This commit is contained in:
mo 2020-09-25 09:28:14 +08:00
parent 9d0f6c719c
commit 4c3beac280
9 changed files with 92 additions and 121 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
goAdapter
.idea
plugin/
config/
selfpara/

8
.idea/.gitignore vendored
View File

@ -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/

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/goAdapter.iml" filepath="$PROJECT_DIR$/.idea/goAdapter.iml" />
</modules>
</component>
</project>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

5
go.mod
View File

@ -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

13
go.sum
View File

@ -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=

View File

@ -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<len(mRegValue);i++{
sRegValue += strconv.FormatUint(uint64(mRegValue[i]),10)
for i := 0; i < len(mRegValue); i++ {
sRegValue += strconv.FormatUint(uint64(mRegValue[i]), 10)
if i != (len(mRegValue)-1) {
if i != (len(mRegValue) - 1) {
sRegValue = sRegValue + ","
}else{
} else {
sRegValue = sRegValue + ""
}
}
fmt.Println(sRegValue)
aParam.Code = "0"
aParam.Message = ""
aParam.Data = sRegValue
sJson,_ := json.Marshal(aParam)
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
context.String(http.StatusOK, string(sJson))
}
func apiWriteMultiReg(context *gin.Context){
func apiWriteMultiReg(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
}
//将字符串中数值取出来
sData := strings.Split(rMbParam.Data,",")
sData := strings.Split(rMbParam.Data, ",")
//fmt.Println(sData)
bData := make([]byte,0)
bData2 := make([]byte,2)
for _,v:= range sData{
bData := make([]byte, 0)
bData2 := make([]byte, 2)
for _, v := range sData {
//tByte,err := strconv.ParseUint(v,10,16)
tByte,_:= strconv.Atoi(v)
binary.BigEndian.PutUint16(bData2,uint16(tByte))
tByte, _ := strconv.Atoi(v)
binary.BigEndian.PutUint16(bData2, uint16(tByte))
bData = append(bData,bData2...)
bData = append(bData, bData2...)
}
fmt.Printf("bData is %d\n",bData)
fmt.Printf("bData is %d\n", bData)
err = mbParamWriteMutilReg(rMbParam.Addr,rMbParam.RegAddr,
rMbParam.RegCnt,bData)
if err != nil{
err = mbParamWriteMutilReg(rMbParam.Addr, rMbParam.RegAddr,
rMbParam.RegCnt, bData)
if err != nil {
aParam.Code = "1"
aParam.Message = "write reg timeout"
sJson,_ := json.Marshal(aParam)
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
context.String(http.StatusOK, string(sJson))
}
aParam.Code = "0"
aParam.Message = ""
sJson,_ := json.Marshal(aParam)
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
context.String(http.StatusOK, string(sJson))
}