| | |
| | | |
| | | import com.serotonin.modbus4j.ModbusFactory; |
| | | import com.serotonin.modbus4j.ModbusMaster; |
| | | import com.serotonin.modbus4j.code.DataType; |
| | | import com.serotonin.modbus4j.exception.ErrorResponseException; |
| | | import com.serotonin.modbus4j.exception.ModbusInitException; |
| | | import com.serotonin.modbus4j.exception.ModbusTransportException; |
| | |
| | | import com.serotonin.modbus4j.locator.BaseLocator; |
| | | import com.serotonin.modbus4j.msg.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.HashMap; |
| | | |
| | | /** |
| | | * @Desc: 工具类 |
| | |
| | | //从机默认值 |
| | | private static Integer slaveId = 1; |
| | | |
| | | private static HashMap<String, ModbusMaster> masterMap = new HashMap<>(); |
| | | |
| | | /** |
| | | * 工厂 |
| | | */ |
| | | static ModbusFactory modbusFactory; |
| | | //static ModbusMaster modbusMaster; |
| | | |
| | | static { |
| | | if (modbusFactory == null) { |
| | |
| | | * |
| | | * @return |
| | | */ |
| | | public static ModbusMaster getMaster(String ip, int port) throws ModbusInitException { |
| | | String key = ip; |
| | | ModbusMaster modbusMaster = masterMap.get(key); |
| | | if (modbusMaster == null || !modbusMaster.isConnected()) { |
| | | IpParameters ipParameters = new IpParameters(); |
| | | ipParameters.setHost(ip); |
| | | ipParameters.setPort(port); |
| | | modbusMaster = modbusFactory.createTcpMaster(ipParameters, true); |
| | | modbusMaster.init(); |
| | | masterMap.put(key, modbusMaster); |
| | | return modbusMaster; |
| | | } |
| | | public static ModbusMaster getMaster(String ip, int port) throws ModbusInitException, InterruptedException { |
| | | |
| | | IpParameters ipParameters = new IpParameters(); |
| | | ipParameters.setHost(ip); |
| | | ipParameters.setPort(port); |
| | | ModbusMaster modbusMaster = modbusFactory.createTcpMaster(ipParameters, false); |
| | | modbusMaster.init(); |
| | | |
| | | //初始化之后等待,避免出现连接未创建 |
| | | Thread.sleep(3000); |
| | | |
| | | return modbusMaster; |
| | | } |
| | | |
| | |
| | | * @throws ModbusTransportException |
| | | * @throws ErrorResponseException |
| | | */ |
| | | public static Boolean readCoilStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
| | | public static Boolean readCoilStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { |
| | | BaseLocator<Boolean> coilStatus = BaseLocator.coilStatus(slaveId, offset); |
| | | Boolean res = getMaster(ip, port).getValue(coilStatus); |
| | | return res; |
| | |
| | | public static Boolean readInputStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { |
| | | BaseLocator<Boolean> inputStatus = BaseLocator.inputStatus(slaveId, offset); |
| | | |
| | | Thread.sleep(3000); |
| | | // Thread.sleep(3000); |
| | | Boolean res = getMaster(ip, port).getValue(inputStatus); |
| | | return res; |
| | | } |
| | |
| | | * @throws ModbusTransportException |
| | | * @throws ErrorResponseException |
| | | */ |
| | | public static Number readHoldingRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
| | | public static Number readHoldingRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { |
| | | BaseLocator<Number> holdingRegister = BaseLocator.holdingRegister(slaveId, offset, dataType); |
| | | Number value = getMaster(ip, port).getValue(holdingRegister); |
| | | return value; |
| | | } |
| | | |
| | | public static Number readHoldingRegister(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { |
| | | int dataType = DataType.TWO_BYTE_INT_SIGNED; |
| | | return readHoldingRegister(ip, port, offset, dataType); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws ModbusTransportException |
| | | * @throws ErrorResponseException |
| | | */ |
| | | public static Number readInputRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { |
| | | public static Number readInputRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { |
| | | BaseLocator<Number> inputRegister = BaseLocator.inputRegister(slaveId, offset, dataType); |
| | | Number value = getMaster(ip, port).getValue(inputRegister); |
| | | return value; |
| | | } |
| | | |
| | | /** |
| | | * 读输入寄存器数据 0x04 |
| | | * |
| | | * @param offset |
| | | * @return |
| | | * @throws ModbusInitException |
| | | * @throws ModbusTransportException |
| | | * @throws ErrorResponseException |
| | | */ |
| | | public static Number readInputRegister(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { |
| | | int dataType = DataType.TWO_BYTE_INT_SIGNED; |
| | | return readInputRegister(ip, port, offset, dataType); |
| | | } |
| | | |
| | | |
| | |
| | | * @throws ModbusInitException |
| | | */ |
| | | public static Boolean writeCoilStatus(String ip, int port, int offset, boolean status) throws ModbusTransportException, ModbusInitException, InterruptedException { |
| | | // boolean coilValue = status; |
| | | // WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, coilValue); |
| | | // WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest); |
| | | // return !coilResponse.isException(); |
| | | |
| | | WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, status); |
| | | Thread.sleep(3000); |
| | | WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest); |
| | | |
| | | //发送清除命令 |
| | | WriteCoilRequest coilRequest1 = new WriteCoilRequest(slaveId, offset, !status); |
| | | getMaster(ip, port).send(coilRequest1); |
| | | |
| | | return !coilResponse.isException(); |
| | | } |
| | | |
| | |
| | | * @throws ModbusTransportException |
| | | * @throws ModbusInitException |
| | | */ |
| | | public static Boolean writeRegister(String ip, int port, int offset, int value) throws ModbusTransportException, ModbusInitException { |
| | | public static Boolean writeRegister(String ip, int port, int offset, int value) throws ModbusTransportException, ModbusInitException, InterruptedException { |
| | | WriteRegisterRequest registerRequest = new WriteRegisterRequest(slaveId, offset, value); |
| | | WriteRegisterResponse registerResponse = (WriteRegisterResponse) getMaster(ip, port).send(registerRequest); |
| | | return !registerResponse.isException(); |
| | |
| | | * @throws ModbusTransportException |
| | | * @throws ModbusInitException |
| | | */ |
| | | public static Boolean writeCoils(String ip, int port, int offset, boolean[] booleans) throws ModbusTransportException, ModbusInitException { |
| | | public static Boolean writeCoils(String ip, int port, int offset, boolean[] booleans) throws ModbusTransportException, ModbusInitException, InterruptedException { |
| | | WriteCoilsRequest writeCoils = new WriteCoilsRequest(slaveId, offset, booleans); |
| | | WriteCoilsResponse coilsResponse = (WriteCoilsResponse) getMaster(ip, port).send(writeCoils); |
| | | return !coilsResponse.isException(); |
| | |
| | | * @throws ModbusTransportException |
| | | * @throws ModbusInitException |
| | | */ |
| | | public static Boolean writeRegisters(String ip, int port, int offset, short[] nums) throws ModbusTransportException, ModbusInitException { |
| | | public static Boolean writeRegisters(String ip, int port, int offset, short[] nums) throws ModbusTransportException, ModbusInitException, InterruptedException { |
| | | WriteRegistersRequest writeRegisters = new WriteRegistersRequest(slaveId, offset, nums); |
| | | WriteRegistersResponse registersResponse = (WriteRegistersResponse) getMaster(ip, port).send(writeRegisters); |
| | | return !registersResponse.isException(); |