zhangqian
2023-08-28 f82cb11a0ac571dc7afd3ec339324000a25207c0
pkg/plc/plc4x.go
@@ -46,8 +46,8 @@
         return conn, nil
      }
   }
   // 创建一个上下文,并设置 5 秒超时
   ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   // 创建一个上下文,并设置 3 秒超时
   ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
   defer cancel()
   conn, err := NewGetModbusConnection(ctx, ipAddr)
   if err != nil {
@@ -83,10 +83,11 @@
}
func ReadHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) {
   tag := fmt.Sprintf("tag:%v", address)
   tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
   // 读模式
   readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()
   readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
   if err != nil {
      fmt.Printf("preparing read-request:%s\n", err.Error())
      return nil, err
@@ -100,22 +101,23 @@
   }
   // 判断响应码是否正确
   if readResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK {
      fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("tag").GetName())
   if readResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK {
      fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode(tag).GetName())
      return nil, nil
   }
   value := readResult.GetResponse().GetValue("tag")
   value := readResult.GetResponse().GetValue(tag)
   return value.GetRaw(), err
}
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)
   // 读模式
   readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()
   readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
   if err != nil {
      fmt.Printf("preparing read-request:%s\n", err.Error())
      return nil, err
@@ -129,12 +131,12 @@
   }
   // 判断响应码是否正确
   if readResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK {
      fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("tag").GetName())
      return nil, errors.New("error  code: " + readResult.GetResponse().GetResponseCode("tag").GetName())
   if readResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK {
      fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode(tag).GetName())
      return nil, errors.New("error  code: " + readResult.GetResponse().GetResponseCode(tag).GetName())
   }
   value := readResult.GetResponse().GetValue("tag")
   value := readResult.GetResponse().GetValue(tag)
   var result []byte
@@ -154,10 +156,11 @@
}
func WriteHoldingRegister(connection plc4go.PlcConnection, address int, value any) (string, error) {
   tag := fmt.Sprintf("tag:%v:w", address)
   tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
   // 写模式
   writeRequest, err := connection.WriteRequestBuilder().AddTagAddress("tag", tagAddress, value).Build()
   writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build()
   if err != nil {
      fmt.Printf("preparing read-request:%s\n", err.Error())
      return "", err
@@ -171,9 +174,9 @@
   }
   // 判断响应码是否正确
   if writeResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK {
      fmt.Printf("error an non-ok return code: %s", writeResult.GetResponse().GetResponseCode("tag").GetName())
      return "", errors.New("error  code: " + writeResult.GetResponse().GetResponseCode("tag").GetName())
   if writeResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK {
      fmt.Printf("error an non-ok return code: %s", writeResult.GetResponse().GetResponseCode(tag).GetName())
      return "", errors.New("error  code: " + writeResult.GetResponse().GetResponseCode(tag).GetName())
   }
   result := writeResult.GetResponse().String()