配置pom.xml
< dependency> < groupId> com.infiniteautomation</ groupId> < artifactId> modbus4j</ artifactId> < version> 3.0.3</ version>
</ dependency> < repositories> < repository> < releases> < enabled> false</ enabled> </ releases> < snapshots> < enabled> true</ enabled> </ snapshots> < id> ias-snapshots</ id> < name> Infinite Automation Snapshot Repository</ name> < url> https://maven.mangoautomation.net/repository/ias-snapshot/</ url> </ repository> < repository> < releases> < enabled> true</ enabled> </ releases> < snapshots> < enabled> false</ enabled> </ snapshots> < id> ias-releases</ id> < name> Infinite Automation Release Repository</ name> < url> https://maven.mangoautomation.net/repository/ias-release/</ url> </ repository>
</ repositories>
import com. serotonin. modbus4j. BatchRead ;
import com. serotonin. modbus4j. BatchResults ;
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 ;
public class Modbus4jUtils { static ModbusFactory modbusFactory; static { if ( modbusFactory == null ) { modbusFactory = new ModbusFactory ( ) ; } } public static ModbusMaster getMaster ( ) throws ModbusInitException { IpParameters params = new IpParameters ( ) ; params. setHost ( "192.168.2.80" ) ; params. setPort ( 502 ) ; ModbusMaster master = modbusFactory. createTcpMaster ( params, true ) ; master. setRetries ( 5 ) ; master. init ( ) ; return master; } public static Boolean readCoilStatus ( int slaveId, int offset) throws ModbusTransportException , ErrorResponseException , ModbusInitException { BaseLocator < Boolean > loc = BaseLocator . coilStatus ( slaveId, offset) ; Boolean value = getMaster ( ) . getValue ( loc) ; return value; } public static Boolean readInputStatus ( int slaveId, int offset) throws ModbusTransportException , ErrorResponseException , ModbusInitException { BaseLocator < Boolean > loc = BaseLocator . inputStatus ( slaveId, offset) ; Boolean value = getMaster ( ) . getValue ( loc) ; return value; } public static Number readHoldingRegister ( int slaveId, int offset, int dataType) throws ModbusTransportException , ErrorResponseException , ModbusInitException { BaseLocator < Number > loc = BaseLocator . holdingRegister ( slaveId, offset, dataType) ; Number value = getMaster ( ) . getValue ( loc) ; return value; } public static Number readInputRegisters ( int slaveId, int offset, int dataType) throws ModbusTransportException , ErrorResponseException , ModbusInitException { BaseLocator < Number > loc = BaseLocator . inputRegister ( slaveId, offset, dataType) ; Number value = getMaster ( ) . getValue ( loc) ; return value; } public static void batchRead ( ) throws ModbusTransportException , ErrorResponseException , ModbusInitException { BatchRead < Integer > batch = new BatchRead < Integer > ( ) ; batch. addLocator ( 0 , BaseLocator . holdingRegister ( 1 , 1 , DataType . FOUR_BYTE_FLOAT ) ) ; batch. addLocator ( 1 , BaseLocator . inputStatus ( 1 , 0 ) ) ; ModbusMaster master = getMaster ( ) ; batch. setContiguousRequests ( false ) ; BatchResults < Integer > results = master. send ( batch) ; System . out. println ( results. getValue ( 0 ) ) ; System . out. println ( results. getValue ( 1 ) ) ; } public static void main ( String [ ] args) { try { while ( true ) { try { for ( int i = 2 ; i < 18 ; i++ ) { Number number = readHoldingRegister ( 1 , i, 2 ) ; System . err. print ( number. intValue ( ) / 10 ) ; } } catch ( Exception e) { } Thread . currentThread ( ) . sleep ( 2000 ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } }
}