zhangqian
2023-11-03 53d80c839ad2aad0a0482ca43fca1fc3a95b9d1c
service/plc.go
@@ -112,28 +112,20 @@
         return nil, errors.New("conf.Conf.Services.Serial config not set yet")
      }
      label := fmt.Sprintf("D%d", address)
      return plccom.ReadPLC(plccom.DeviceTypeMitsubishi, ipAddr, label)
      return plccom.ReadPLC(plccom.DeviceTypeMitsubishi, ipAddr, label, dataLength)
   }
   return
}
func ReadByModbusTCP(ipAddr string, address, length int) ([]byte, error) {
   if conf.Conf.PLC.Package == constvar.PlcPackageApache {
      newLength := length / 2
      if newLength == 0 {
         newLength = 1
      }
      return apacheplc4x.ReadHoldingRegister(ipAddr, address, newLength)
      return apacheplc4x.ReadHoldingRegister(ipAddr, address, length)
   } else if conf.Conf.PLC.Package == constvar.PlcPackageApacheLongConnection {
      conn, err := plc.GetModbusConnection(ipAddr)
      if err != nil {
         return nil, err
      }
      newLength := length / 2
      if newLength == 0 {
         newLength = 1
      }
      return plc.ReadHoldingRegister(conn, address, newLength)
      return plc.ReadHoldingRegister(conn, address, length)
   } else {
      return modbusx.Read(ipAddr, uint16(address), uint16(length))
   }
@@ -164,11 +156,7 @@
      Parity:     plcConfig.Parity,
   }
   if conf.Conf.PLC.Package == constvar.PlcPackageApache {
      newLength := length / 2
      if newLength == 0 {
         newLength = 1
      }
      return apacheplc4x.ReadHoldingRegisterByRTU(rtuConfig, address, newLength)
      return apacheplc4x.ReadHoldingRegisterByRTU(rtuConfig, address, length)
   } else {
      return modbusx.ReadByRTU(rtuConfig, uint16(address), uint16(length))
   }