Modbus TCP Client Example
public class run { public static void main(String[] args) { ModbusClient modbusClient = new ModbusClient("127.0.0.1",502); try { modbusClient.Connect(); modbusClient.WriteSingleCoil(0, true); modbusClient.WriteSingleRegister(0, 1234); modbusClient.WriteMultipleRegisters(11, ModbusClient.ConvertFloatToTwoRegisters((float) 123.56)); System.out.println(modbusClient.ReadCoils(0, 1)[0]); System.out.println(modbusClient.ReadHoldingRegisters(0, 1)[0]); System.out.println(ModbusClient.ConvertRegistersToFloat(modbusClient.ReadHoldingRegisters(11, 2))); } catch (Exception e) { } } }
Read an Write 32 Bit Values
public static void main(String[] args) { ModbusClient modbusClient = new ModbusClient("127.0.0.1", 1536); try { modbusClient.Connect(); //Write Float value to Register 10 and 11 modbusClient.WriteMultipleRegisters(9, ModbusClient.ConvertFloatToTwoRegisters((float)(1323.55))); //Write 32 bit value to Register 12 and 13 modbusClient.WriteMultipleRegisters(11, ModbusClient.ConvertDoubleToTwoRegisters((1323554))); //Read Float Value from Register 10 and 11 System.out.println(ModbusClient.ConvertRegistersToFloat(modbusClient.ReadHoldingRegisters(9, 2))); //Read 32 Bit Value from Register 12 and 13 System.out.println(ModbusClient.ConvertRegistersToDouble(modbusClient.ReadHoldingRegisters(11, 2))); } catch (Exception e) { System.out.println(e.toString()); } }
References
https://www.youtube.com/user/emileackbarali/videos
https://www.udemy.com/the-1-hour-modbus-rs485-primer/
https://www.rtaautomation.com/technologies/modbus-rtu/
http://easymodbustcp.net/java-modbus-tcp-client-example
http://easymodbustcp.net/java-read-an-write-32-bit-values
https://en.wikipedia.org/wiki/Modbus