1. Introduce pom dependencies
<dependency> <groupId>-j</groupId> <artifactId>modbus4</artifactId> <version>1.2.0</version> </dependency>
2. Code implementation
package ; import .; import .; import .; import .; import .; import ..*; import .; public class ReadAWriteUtil { /** * Bulk write data to hold register * @param ip slave IP * @param port modbus port * @param slaveId SlaveID * @param start Start address offset * @param values Data to be written */ public static void modbusWTCP(String ip, int port, int slaveId, int start, short[] values) { ModbusFactory modbusFactory = new ModbusFactory(); // The IP and port of the device ModbusTCP, if the port is not set, the default is 502 IpParameters params = new IpParameters(); (ip); // Set the port, default 502 if (502 != port) { (port); } ModbusMaster tcpMaster = null; // Parameter 1: IP and port information Parameter 2: Keep the connection active tcpMaster = (params, true); try { (); ("=======Initialization successfully========"); } catch (ModbusInitException e) { ("Initialization exception"); } try { WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values); WriteRegistersResponse response = (WriteRegistersResponse) (request); if (()){ ("Exception response: message=" + ()); }else{ ("Success"); } } catch (ModbusTransportException e) { (); } } /** * Read the contents on the hold register * @param ip slave IP * @param port modbus port * @param start Start address offset * @param readLenth Number of registers to be read * @return */ public static ByteQueue modbusTCP(String ip, int port, int start, int readLenth) { ModbusFactory modbusFactory = new ModbusFactory(); // The IP and port of the device ModbusTCP, if the port is not set, the default is 502 IpParameters params = new IpParameters(); (ip); //Set the port, default 502 if(502!=port){ (port); } ModbusMaster tcpMaster = null; tcpMaster = (params, true); try { (); ("========Initialization successfully======="); } catch (ModbusInitException e) { return null; } ModbusRequest modbusRequest=null; try { //Function code 03 Read the value of the hold register modbusRequest = new ReadHoldingRegistersRequest(1, start, readLenth); } catch (ModbusTransportException e) { (); } ModbusResponse modbusResponse=null; try { modbusResponse = (modbusRequest); } catch (ModbusTransportException e) { (); } ByteQueue byteQueue= new ByteQueue(1024); (byteQueue); ("Function code:"+()); ("Slave address:"+()); ("Size of response information received:"+()); ("Response information received:"+byteQueue); return byteQueue; } public static void main(String[] args) { // short [] list={100,20,30,9,67,0,65,0,89,90}; // modbusWTCP("127.0.0.1",502,1,0,list); //Read the data modbusTCP("10.34.194.18",8080,0,2); } }
3. Code details
This code is a sample code for Modbus TCP communication written in Java language, mainly used to communicate with Modbus TCP slave devices.
ModbusFactory modbusFactory = new ModbusFactory();
This line of code creates a ModbusFactory object to create a ModbusMaster instance.
IpParameters params = new IpParameters(); (ip); if(502 != port){ (port); }
Here, an IpParameters object is created to set the IP address and port number of Modbus TCP communication. If the port is not the default port 502, set the specified port number.
ModbusMaster tcpMaster = null; tcpMaster = (params, true);
This line of code creates a ModbusMaster instance, which is initialized using the IpParameters object that was set up previously. Setting the second parameter to true means that non-blocking mode is used.
try { (); ("========Initialization successfully======="); } catch (ModbusInitException e) { return null; } Here,pass()The method is initializedModbusMasterExample,如果Initialization successfully,Will print"========Initialization successfully======="。 ModbusRequest modbusRequest = null; try { modbusRequest = new ReadHoldingRegistersRequest(1, start, readLenth); } catch (ModbusTransportException e) { (); }
This code creates a Modbus request object, uses function code 03 (read the value of the hold register), and specifies the start address and read length.
ModbusResponse modbusResponse = null; try { modbusResponse = (modbusRequest); } catch (ModbusTransportException e) { (); }
This code sends the Modbus request object that was created previously and tries to receive the response data returned from the device.
ByteQueue byteQueue = new ByteQueue(1024); (byteQueue);
Finally, this code writes the response data returned from the device into a ByteQueue object for subsequent processing of the data.
These codes are mainly used to establish a connection to the Modbus TCP slave device, send it a request to read the hold register, and then process the response data returned by the slave device.
This is the end of this article about Java's simple implementation of digital acquisition through Modbus. For more related Java Modbus content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!