package com.iailab.module.data.channel.modbus.collector; import com.iailab.module.data.channel.modbus.dto.ChannelModBusDeviceDTO; import com.serotonin.modbus4j.ModbusFactory; import com.serotonin.modbus4j.ModbusMaster; 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.*; /** * Modbus 工具类 */ public class ModbusUtils { /** * 工厂 */ static ModbusFactory modbusFactory; static ModbusMaster modbusMaster; static { if (modbusFactory == null) { modbusFactory = new ModbusFactory(); } } /** * 获取master * * @param tModbusDTO * @return */ public static ModbusMaster getMaster(ChannelModBusDeviceDTO tModbusDTO) throws ModbusInitException { if (modbusMaster == null) { IpParameters ipParameters = new IpParameters(); ipParameters.setHost(tModbusDTO.getAddress()); ipParameters.setPort(tModbusDTO.getPort()); modbusMaster = modbusFactory.createTcpMaster(ipParameters, true); //设置超时时间 modbusMaster.setTimeout(tModbusDTO.getReadTimeout()); //设置重连次数 modbusMaster.setRetries(tModbusDTO.getAttemptsBeforeTimeout()); modbusMaster.init(); return modbusMaster; } return modbusMaster; } /** * 测试用 * * @return */ public static ModbusMaster getMaster() throws ModbusInitException { if (modbusMaster == null) { IpParameters ipParameters = new IpParameters(); ipParameters.setHost("127.0.0.1"); ipParameters.setPort(502); modbusMaster = modbusFactory.createTcpMaster(ipParameters, true); modbusMaster.init(); return modbusMaster; } return modbusMaster; } /** * 读取线圈开关状态数据 0x01 * * @param slaveId * @param offset * @return * @throws ModbusInitException * @throws ModbusTransportException * @throws ErrorResponseException */ public static Boolean readCoilStatus(ModbusMaster modbusMaster, int slaveId, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException { BaseLocator coilStatus = BaseLocator.coilStatus(slaveId, offset); Boolean res = modbusMaster.getValue(coilStatus); return res; } /** * 读离散输入寄存器状态数据 0x02 * * @param slaveId * @param offset * @return * @throws ModbusInitException * @throws ModbusTransportException * @throws ErrorResponseException */ public static Boolean inputStatus(ModbusMaster modbusMaster, int slaveId, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException { BaseLocator inputStatus = BaseLocator.inputStatus(slaveId, offset); Boolean res = modbusMaster.getValue(inputStatus); return res; } /** * 读保持寄存器数据 0x03 * * @param modbusMaster * @param slaveId * @param offset * @param dataType * @return * @throws ModbusInitException * @throws ModbusTransportException * @throws ErrorResponseException */ public static Number holdingRegister(ModbusMaster modbusMaster, int slaveId, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { BaseLocator holdingRegister = BaseLocator.holdingRegister(slaveId, offset, dataType); Number value = modbusMaster.getValue(holdingRegister); return value; } /** * 测试用 * * @return */ public static Number holdingRegister(int slaveId, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { BaseLocator holdingRegister = BaseLocator.holdingRegister(slaveId, offset, dataType); Number value = getMaster().getValue(holdingRegister); return value; } /** * 读输入寄存器数据 0x04 * * @param slaveId * @param offset * @param dataType * @return * @throws ModbusInitException * @throws ModbusTransportException * @throws ErrorResponseException */ public static Number inputRegister(ModbusMaster modbusMaster, int slaveId, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { BaseLocator inputRegister = BaseLocator.inputRegister(slaveId, offset, dataType); Number value = modbusMaster.getValue(inputRegister); return value; } /** * 写线圈开关状态数据 0x05 * * @param slaveId * @param offset * @param status * @return * @throws ModbusTransportException * @throws ModbusInitException */ public static Boolean writeCoilStatus(ModbusMaster modbusMaster, int slaveId, int offset, boolean status) throws ModbusTransportException, ModbusInitException { boolean coilValue = status; WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, coilValue); WriteCoilResponse coilResponse = (WriteCoilResponse) modbusMaster.send(coilRequest); return !coilResponse.isException(); } /** * 写单个保持寄存器数据 0x06 * * @param slaveId * @param offset * @param value * @return * @throws ModbusTransportException * @throws ModbusInitException */ public static Boolean writeRegister(ModbusMaster modbusMaster, int slaveId, int offset, int value) throws ModbusTransportException, ModbusInitException { WriteRegisterRequest registerRequest = new WriteRegisterRequest(slaveId, offset, value); WriteRegisterResponse registerResponse = (WriteRegisterResponse) modbusMaster.send(registerRequest); return !registerResponse.isException(); } /** * 测试用 * * @return */ public static Boolean writeRegister(int slaveId, int offset, int value) throws ModbusTransportException, ModbusInitException { WriteRegisterRequest registerRequest = new WriteRegisterRequest(slaveId, offset, value); WriteRegisterResponse registerResponse = (WriteRegisterResponse) getMaster().send(registerRequest); return !registerResponse.isException(); } /** * 写线圈开关状态数据【多】 0x0f * * @param slaveId * @param offset * @param booleans * @return * @throws ModbusTransportException * @throws ModbusInitException */ public static Boolean writeCoils(ModbusMaster modbusMaster, int slaveId, int offset, boolean[] booleans) throws ModbusTransportException, ModbusInitException { WriteCoilsRequest writeCoils = new WriteCoilsRequest(slaveId, offset, booleans); WriteCoilsResponse coilsResponse = (WriteCoilsResponse) modbusMaster.send(writeCoils); return !coilsResponse.isException(); } /** * 写保存寄存器数据【多】 0x10 * * @param slaveId * @param offset * @param nums * @return * @throws ModbusTransportException * @throws ModbusInitException */ public static Boolean writeRegisters(ModbusMaster modbusMaster, int slaveId, int offset, short[] nums) throws ModbusTransportException, ModbusInitException { WriteRegistersRequest writeRegisters = new WriteRegistersRequest(slaveId, offset, nums); WriteRegistersResponse registersResponse = (WriteRegistersResponse) modbusMaster.send(writeRegisters); return !registersResponse.isException(); } }