Help understanding addresses (offsets)
-
I'm new to modbus4j and MODBUS. I have been tasked with reading data from a CLICK (Koyo) PLC. According to the CLICK Programming Software, the MODBUS 984 Address for the BIT I am trying to read (Address X001) is 100001 with function code 02:
However, the 100001 address doesn't seem to work with modbus4j. I suspect the value used for OFFSET is not correct. Here is what I have tried:
public static void main( String[] args ) throws Exception { IpParameters ipParameters = new IpParameters(); ipParameters.setHost( "10.3.17.132" ); ipParameters.setPort( 502 ); ModbusMaster master = new ModbusFactory().createTcpMaster( ipParameters, false ); master.init(); // Define the location (X001). BaseLocator< Boolean > inputStatus = BaseLocator.inputStatus( 1, 100001 ); BaseLocator< Boolean > inputRegisterBit = BaseLocator.inputRegisterBit( 1, 100001, 1 ); // Read the value System.out.println( master.getValue( inputStatus ) ); System.out.println( master.getValue( inputRegisterBit ) ); }
This...
BaseLocator< Boolean > inputStatus = BaseLocator.inputStatus( 1, 100001 );
...always returns FALSE. Although the BIT is TRUE/ON (software and indicator light confirm this) the code is always reporting FALSE.
This...
BaseLocator< Boolean > inputRegisterBit = BaseLocator.inputRegisterBit( 1, 100001, 1 );
... returns an exception:
com.serotonin.modbus4j.exception.ModbusTransportException: Invalid offset: 100001Would someone please help me understand how to convert the MODBUS 984 Address to a modbus4j offset? Or am I going about this completely wrong?
-
@vanjr something isn't quite right with the image you posted. First try to understand what function code 02 is doing, Read an Input Status at an offset starting from 10001.
https://www.simplymodbus.ca/FC02.htm
Maybe I'm missing something but it doesn't seem possible that there is an input register at location 100001 as that offset from 10001 is larger than the 2 bytes that are allowed for the offset in the protocol message of function code 02. My guess is that there is a display error in the software you are using and the address is really 10001 in which case the offset is 0.
-
@terrypacker
NOW I understand! The offset is determined using predefined starting addresses by function from the MODBUS specification! Your explanation and that website you pointed me to was far more informative than watching a bunch of YouTube videos! Thank you so much for you help!For those who find this thread later in life, here is what works (as of CLICK firmware 3.21):
public static void main( String[] args ) throws Exception { /* Define Connection */ IpParameters ipParameters = new IpParameters(); ipParameters.setHost( "10.3.17.132" ); ipParameters.setPort( 502 ); /* Make the connection */ ModbusMaster master = new ModbusFactory().createTcpMaster( ipParameters, false ); master.init(); /* Test connectivity */ System.out.println( "testSlaveNode 1: " + master.testSlaveNode( 1 ) ); /* Define the location points and type */ BaseLocator< Boolean > clickX001 = BaseLocator.inputStatus( 1, 0 ); // Click X001: input BIT on CPU BaseLocator< Boolean > clickY001 = BaseLocator.coilStatus( 1, 0 ); // Click Y001: output COIL on CPU BaseLocator< Boolean > clickY002 = BaseLocator.coilStatus( 1, 1 ); // Click Y002: output COIL on CPU BaseLocator< Number > clickDS1 = BaseLocator.holdingRegister( 1, 0, DataType.TWO_BYTE_INT_SIGNED ); // Click DS1: Data Register Single Word /* Set/Write a value to a point */ master.setValue( clickDS1, 1 ); /* Get/Read the current value of each point */ System.out.println( "clickX001 : " + master.getValue( clickX001 ) ); System.out.println( "clickY001 : " + master.getValue( clickY001 ) ); System.out.println( "clickY002 : " + master.getValue( clickY002 ) ); System.out.println( "clickDS1 : " + master.getValue( clickDS1 ) ); }
PLC Program for testing: