¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.modbus; |
| | | |
| | | 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.ip.IpParameters; |
| | | import com.serotonin.modbus4j.locator.BaseLocator; |
| | | import com.serotonin.modbus4j.msg.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | /** |
| | | * @Desc: å·¥å
·ç±» |
| | | * @author: Andy |
| | | * @update-time: 2023/8/11 |
| | | */ |
| | | @Slf4j |
| | | public class ModbusUtil2 { |
| | | |
| | | //仿ºé»è®¤å¼ |
| | | private static Integer slaveId = 1; |
| | | |
| | | /** |
| | | * å·¥å |
| | | */ |
| | | static ModbusFactory modbusFactory; |
| | | |
| | | static { |
| | | if (modbusFactory == null) { |
| | | modbusFactory = new ModbusFactory(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * è·åmaster |
| | | * |
| | | * @return |
| | | */ |
| | | 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(2500); |
| | | |
| | | return modbusMaster; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 读å线åå¼å
³ç¶ææ°æ® 0x01 |
| | | * |
| | | * @param offset |
| | | * @return |
| | | * @throws ModbusInitException |
| | | * @throws ModbusTransportException |
| | | * @throws 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; |
| | | } |
| | | |
| | | /** |
| | | * 读离æ£è¾å
¥å¯åå¨ç¶ææ°æ® 0x02 |
| | | * |
| | | * @param offset |
| | | * @return |
| | | * @throws ModbusInitException |
| | | * @throws ModbusTransportException |
| | | * @throws ErrorResponseException |
| | | */ |
| | | 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); |
| | | Boolean res = getMaster(ip, port).getValue(inputStatus); |
| | | return res; |
| | | } |
| | | |
| | | /** |
| | | * è¯»ä¿æå¯å卿°æ® 0x03 |
| | | * |
| | | * @param offset |
| | | * @param dataType |
| | | * @return |
| | | * @throws ModbusInitException |
| | | * @throws ModbusTransportException |
| | | * @throws 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); |
| | | } |
| | | |
| | | /** |
| | | * 读è¾å
¥å¯å卿°æ® 0x04 |
| | | * |
| | | * @param offset |
| | | * @param dataType |
| | | * @return |
| | | * @throws ModbusInitException |
| | | * @throws ModbusTransportException |
| | | * @throws 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); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å线åå¼å
³ç¶ææ°æ® 0x05 |
| | | * |
| | | * @param offset |
| | | * @param status |
| | | * @return |
| | | * @throws ModbusTransportException |
| | | * @throws ModbusInitException |
| | | */ |
| | | public static Boolean writeCoilStatus(String ip, int port, int offset, boolean status) throws ModbusTransportException, ModbusInitException, InterruptedException { |
| | | WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, status); |
| | | WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest); |
| | | return !coilResponse.isException(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * ååä¸ªä¿æå¯å卿°æ® 0x06 |
| | | * |
| | | * @param offset |
| | | * @param value |
| | | * @return |
| | | * @throws ModbusTransportException |
| | | * @throws 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(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * å线åå¼å
³ç¶ææ°æ®ãå¤ã 0x0f |
| | | * |
| | | * @param offset |
| | | * @param booleans |
| | | * @return |
| | | * @throws ModbusTransportException |
| | | * @throws 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(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * åä¿åå¯å卿°æ®ãå¤ã 0x10 |
| | | * |
| | | * @param offset |
| | | * @param nums |
| | | * @return |
| | | * @throws ModbusTransportException |
| | | * @throws 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(); |
| | | } |
| | | } |