I'm struggling a little bit with reading the correct modbus values.
Here some background-information We settled up a virtual comport, that is connected to the existing Commport-Server and ioLogik R1210 Model (https://www.moxa.com/support/sarch_result.aspx?prod_id=922&type_id=7&type=doc). Last component has attached an own Pushbutton which increases the counter in de DI/Channel "0" of the ioLogik R1210 Modul. I can see this through ioSearch-Software which is given by the MOXA company, that also produces the ioLogik R1210 modul. The counter is also increasing if we push the button. Thus, hardware is fine and connection from my client to the ioLogikModul via settled virtual comport also - otherwise ioSearch wouldn't show the correct counter.
Now I'm trying to read and write the counter in Channel "0" via Java. I tried several Modbus-Libraries, but always get the same problem that I don't know how to read the correct register/coil/Input.
In the following user manual and chapter "ioLogik R1210 Modbus Address and Register Map" (Page 45 or Appendix A-3) are given the registers: https://www.moxa.com/doc/man/ioLogik_R1200_Series_UM_e3.3.pdf
Whereas this line sounds like it is the one register I need to read: 0x0020; 30033; R; word;DI-00_counterValueHigh;04:INPUT; REGISTER ;high word
But if I try with the ModbusClient-Library (https://sourceforge.net/projects/easymodbustcp-udp-java/) with the expected register, I got a weird output.
Here's some of my code:
package com.mg.moxa.modbus;
import java.io.IOException;
import de.re.easymodbus.exceptions.ModbusException;
import de.re.easymodbus.modbusclient.ModbusClient;
public class ModbusClientTest {
public static void main(final String[] args) {
modbusLibrary();
}
private static void modbusLibrary() {
final ModbusClient modbusClient = new ModbusClient();
try {
// modbusClient.Connect();
modbusClient.Connect("COM5");
// final int[] readHolding = modbusClient.ReadHoldingRegisters(0, 32);
// System.out.print("readHolding [");
// for (int i = 0; i < readHolding.length; i++) {
// System.out.print(readHolding[i] + ", ");
// }
// System.out.println("]");
final boolean[] readCoils = modbusClient.ReadCoils(32, 1);
System.out.print("readCoils [");
for (int i = 0; i < readCoils.length; i++) {
System.out.print(readCoils[i] + ", ");
}
System.out.println("]");
final int[] readInputRegisters = modbusClient.ReadInputRegisters(30033, 16); // high value
System.out.print("readInputRegisters [");
for (int i = 0; i < readInputRegisters.length; i++) {
System.out.print(readInputRegisters[i] + ", ");
}
System.out.println("]");
final int[] readInputRegistersLow = modbusClient.ReadInputRegisters(30033, 8); // low Value
System.out.print("readInputRegisters [");
for (int i = 0; i < readInputRegistersLow.length; i++) {
System.out.print(readInputRegistersLow[i] + ", ");
}
System.out.println("]");
//modbusClient.WriteSingleCoil(0, true);
// final boolean[] readDiscreteInputs = modbusClient.ReadDiscreteInputs(0, 32);
// System.out.print("readDiscreteInputs [");
// for (int i = 0; i < readDiscreteInputs.length; i++) {
// System.out.print(readDiscreteInputs[i] + ", ");
// }
// System.out.println("]");
modbusClient.Disconnect();
} catch (final ModbusException e) {
// TODO Auto-generated catch block
System.err.println("Fehler beim lesen des Modbus: " + e.toString());
e.printStackTrace();
} catch (final IOException e) {
// TODO Auto-generated catch block
System.err.println("Fehler beim lesen des Modbus: " + e.toString());
e.printStackTrace();
} catch (final Exception e) {
// TODO Auto-generated catch block
System.err.println("Fehler beim lesen des Modbus: " + e.toString());
e.printStackTrace();
}
}
}
The output for this code snippet is as follows:
readCoils [false, ]
readInputRegisters [12031, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]
readInputRegisters [12031, 0, 0, 0, 0, 0, 0, 0, ]
Where as the input registers values at position 0 are always different (mostly -18180 or 12031 if that helps for analyzing)
What am I doing wrong here? I think I'm missunderstanding the way to read the registers correctly.
Is there a better/easier way to just get the counter-value on specific channel.
I also need to reset the counter with java. Nice to have would be setting the preferences of the channel.
I hope you can help me.