zhangqian
2023-09-18 83b652cd2f1be2375d33a67366a7c359cf6e22c0
pkg/plc/apacheplc4x/modbus.go
@@ -1,6 +1,7 @@
package apacheplc4x
import (
   "apsClient/conf"
   "apsClient/pkg/logx"
   "context"
   "errors"
@@ -52,7 +53,7 @@
func readHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) {
   tag := fmt.Sprintf("tag:%v", address)
   tagAddress := fmt.Sprintf("holding-register:%d:DINT", address)
   tagAddress := getTagAddress(address)
   // 读模式
   readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
@@ -82,7 +83,7 @@
func readHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) {
   tag := fmt.Sprintf("tag:%v:%v", address, length)
   tagAddress := fmt.Sprintf("holding-register:%d:DINT[%d]", address, length)
   tagAddress := getTagAddress(address)
   // 读模式
   readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
@@ -129,6 +130,14 @@
   return readHoldingRegisterSingle(connection, address)
}
func getTagAddress(address int) string {
   intType := conf.Conf.PLC.ModbusIntType
   if intType == "" {
      intType = "DINT"
   }
   return fmt.Sprintf("holding-register:%d:%v", address, intType)
}
func WriteHoldingRegister(ipAddr string, address int, value any) (string, error) {
   connection, err := GetModbusConnection(ipAddr)
   dealErr(err, ipAddr)
@@ -137,7 +146,7 @@
   }
   defer connection.Close()
   tag := fmt.Sprintf("tag:%v:w", address)
   tagAddress := fmt.Sprintf("holding-register:%d:DINT", address)
   tagAddress := getTagAddress(address)
   // 写模式
   writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build()