0

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.

1 Answer 1

0

Try reading from the values in the "Address" column not the "Register" column. It is confusing, but in the Modbus protocol registers (Input, Holding, etc.) are actually 0 based (0 - 65535). The 0xxxx (coils), 1xxxx (discrete inputs), 3xxxx (input registers), 4xxxx (holding registers) "registers" come from the Modicon Modbus protocol. The Modicon "register" addresses are often still used in documentation because some BMS systems expect those values, but in my experience, they usually need to be changed to a Modbus address:

00001-09999 -> read coils 0-9998
10001-19999 -> read discrete inputs 0-9998
30001-39999 -> read input registers 0-9998
40001-49999 -> read holding registers 0-9998

If you need the "DI-00_counterValueHigh" value, try:

final int[] readInputRegisters = modbusClient.ReadInputRegisters(0x20, 1);

Note, this only gives the upper 16 bits of that counter, to read the entire value, try:

final int[] readInputRegisters = modbusClient.ReadInputRegisters(0x20, 2);

This should give you 2 16-bit values that you will need to combine into a 32-bit value by doing something like the following (NOTE: I haven't used Java in a long time so the syntax might not be completely correct. In C/C++ I would use unsigned values to prevent sign extension, etc.)

int counter = (readInputRegisters[0] << 16) | (readInputRegisters[1]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.