fix
zhangqian
2023-10-19 cab6eea89a0d82710369604ecb51f1cdb122e433
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:UINT", 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:UINT[%d]", address, length)
   tagAddress := getTagAddress(address)
   // 读模式
   readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
@@ -94,13 +95,13 @@
   // 执行
   readResult := <-readRequest.Execute()
   if err := readResult.GetErr(); err != nil {
      logx.Errorf("execting read-request:%s\n", err.Error())
      logx.Errorf("plc4x execute read-request:%s\n", err.Error())
      return nil, err
   }
   // 判断响应码是否正确
   if readResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK {
      logx.Errorf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode(tag).GetName())
      logx.Errorf("plc4x response error code: %s", readResult.GetResponse().GetResponseCode(tag).GetName())
      return nil, errors.New("error  code: " + readResult.GetResponse().GetResponseCode(tag).GetName())
   }
@@ -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,25 +146,25 @@
   }
   defer connection.Close()
   tag := fmt.Sprintf("tag:%v:w", address)
   tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
   tagAddress := getTagAddress(address)
   // 写模式
   writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build()
   if err != nil {
      logx.Errorf("preparing read-request:%s\n", err.Error())
      logx.Errorf("plc4x preparing read-request:%s\n", err.Error())
      return "", err
   }
   // 执行
   writeResult := <-writeRequest.Execute()
   if err := writeResult.GetErr(); err != nil {
      logx.Errorf("execting read-request:%s\n", err.Error())
      logx.Errorf("plc4x execute write-request:%s\n", err.Error())
      return "", err
   }
   // 判断响应码是否正确
   if writeResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK {
      logx.Errorf("error an non-ok return code: %s", writeResult.GetResponse().GetResponseCode(tag).GetName())
      logx.Errorf("plc4x  response error code: %s", writeResult.GetResponse().GetResponseCode(tag).GetName())
      return "", errors.New("error  code: " + writeResult.GetResponse().GetResponseCode(tag).GetName())
   }