package com.ruoyi.hard.modbus.tcp; import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate; import com.jwk.spring.boot.modbus4j.ModbusMasterUtil; import com.serotonin.modbus4j.msg.ReadResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import static com.ruoyi.hard.modbus.tcp.ButtonBoxClient.BUTTON_BOX_CODE.*; /** * 按钮盒对接 * * @author JWK * @version 1.0 * @date 2022/12/21 21:04 */ @Service public class ButtonBoxClient { /** * 按钮盒 */ @Autowired(required = false) @Qualifier("modbusTcpMasterTemplateThird") private ModbusTcpMasterTemplate modbusTcpMasterTemplateThird; /** * 按钮盒命令 */ public enum BUTTON_BOX_CODE { /** * 1号按钮状态 value代表读的长度(读到1代表按钮被按过) value1代表清除按下标记 */ BTN_STS_01(1, 1, 1, 0, 0, 0, 0), /** * 1号按钮状态 value代表读的长度(读到1代表按钮被按过) value1代表清除按下标记 */ BTN_STS_02(1, 2, 1, 0, 0, 0, 0), /** * 1号灯状态 value代表读的长度(0:灭 1:亮 2:快闪 3慢闪) value1-4代表写(0:灭 1:亮 2:快闪 3慢闪) */ LAMP_STS_01(1, 5, 1, 0, 1, 2, 3), /** * 1号灯状态 value代表读的长度(0:灭 1:亮 2:快闪 3慢闪) value1-4代表写(0:灭 1:亮 2:快闪 3慢闪) */ LAMP_STS_02(1, 6, 1, 0, 1, 2, 3); /** * 从节点id */ private int slaveId; /** * 地址 */ private int offset; /** * 读的长度 */ private int value; /** * 写的值 */ private int value1; private int value2; private int value3; private int value4; BUTTON_BOX_CODE(int slaveId, int offset, int value, int value1, int value2, int value3, int value4) { this.slaveId = slaveId; this.offset = offset; this.value = value; this.value1 = value1; this.value2 = value2; this.value3 = value3; this.value4 = value4; } public int getSlaveId() { return slaveId; } public int getOffset() { return offset; } public int getValue() { return value; } public int getValue1() { return value1; } public int getValue2() { return value2; } public int getValue3() { return value3; } public int getValue4() { return value4; } } /** * 读按钮1状态 * * @return true 代表按钮被按下 */ public Boolean readBtn01() { short i = read(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue()); return i == 1; } /** * 读按钮2状态 * * @return true 代表按钮被按下 */ public Boolean readBtn02() { short i = read(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue()); return i == 1; } /** * 清除按钮1状态 * * @return true */ public Boolean clearBtn01() { return write(BTN_STS_01.getSlaveId(), BTN_STS_01.getOffset(), BTN_STS_01.getValue1()); } /** * 清除按钮2状态 * * @return true */ public Boolean clearBtn02() { return write(BTN_STS_02.getSlaveId(), BTN_STS_02.getOffset(), BTN_STS_02.getValue1()); } /** * 写灯1亮 * * @return true */ public Boolean writeLamp01Bright() { return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue2()); } /** * 写灯2亮 * * @return true */ public Boolean writeLamp02Bright() { return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue2()); } /** * 写灯1灭 * * @return true */ public Boolean writeLamp01Death() { return write(LAMP_STS_01.getSlaveId(), LAMP_STS_01.getOffset(), LAMP_STS_01.getValue1()); } /** * 写灯2灭 * * @return true */ public Boolean writeLamp02Death() { return write(LAMP_STS_02.getSlaveId(), LAMP_STS_02.getOffset(), LAMP_STS_02.getValue1()); } private short read(int slaveId, int offset, int len) { ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil(); ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, len); if (readResponse == null) { return 0; } short[] shortData = readResponse.getShortData(); return shortData[0]; } private Boolean write(int slaveId, int offset, int value) { ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateThird.getModbusMasterUtil(); return modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value); } }