|
@@ -0,0 +1,96 @@
|
|
|
+package com.ruoyi.hard.modbus.tcp;
|
|
|
+
|
|
|
+import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
|
|
|
+import com.jwk.spring.boot.constant.DATA_TYPE_WRAPPER;
|
|
|
+import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
|
|
|
+import com.serotonin.modbus4j.msg.ReadResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Jwk
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2023/6/1 12:31
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ModbusTcpWeigherClient {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 称重机光电信号
|
|
|
+ */
|
|
|
+ @Autowired(required = false)
|
|
|
+ @Qualifier("modbusTcpMasterTemplateFirst")
|
|
|
+ private ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读称重机和光电信号
|
|
|
+ *
|
|
|
+ * @param slaveId
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Number read(int slaveId, int offset) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
|
|
|
+ return modbusMasterUtil.readHoldingRegister(slaveId,offset, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读光电1信号
|
|
|
+ *
|
|
|
+ * @param slaveId
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean readPhotoelectricity01(int slaveId, int offset) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
|
|
|
+ Number number = modbusMasterUtil.readHoldingRegister(1, 0, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
|
|
|
+ int i = number.intValue();
|
|
|
+ return i == 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *读光电2信号
|
|
|
+ *
|
|
|
+ * @param slaveId
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean readPhotoelectricity02(int slaveId, int offset) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
|
|
|
+ Number number = modbusMasterUtil.readHoldingRegister(1, 1, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
|
|
|
+ int i = number.intValue();
|
|
|
+ return i == 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读光电3信号
|
|
|
+ *
|
|
|
+ * @param slaveId
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean readPhotoelectricity03(int slaveId, int offset) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
|
|
|
+ Number number = modbusMasterUtil.readHoldingRegister(1, 2, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED);
|
|
|
+ int i = number.intValue();
|
|
|
+ return i == 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读称重机上货物重量
|
|
|
+ *
|
|
|
+ * @param slaveId
|
|
|
+ * @param offset
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public int readCharge(int slaveId, int offset) {
|
|
|
+ ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
|
|
|
+ return modbusMasterUtil.readHoldingRegister(1, 3, DATA_TYPE_WRAPPER.TWO_BYTE_INT_UNSIGNED).intValue();
|
|
|
+ }
|
|
|
+}
|