3

I am trying to make a Modbus Slave and Master using j2mod (it's version 1.0.6 tho, so that is compatible with another program).

And I do have some general questions about the code I found online.

I have found almost no useful documentation what so ever, so I'm kind of clueless.

TcpMaster

this.addr = InetAddress.getByName("127.0.0.1");

conn = new TCPMasterConnection(addr);
        conn.setPort(port);
        conn.connect();

req = new ReadInputDiscretesRequest(ref, count);

trans = new ModbusTCPTransaction(conn);
        trans.setRequest(req);
        
        trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();

TcpSlave

spi = new SimpleProcessImage();
spi.addDigitalOut(bitOut);

ModbusCoupler.getReference().setProcessImage(spi);
ModbusCoupler.getReference().setMaster(false);
ModbusCoupler.getReference().setUnitID(1);

addr = InetAddress.getByName("127.0.0.1");

listener = new ModbusTCPListener(3);
listener.setPort(port);
listener.setAddress(addr);
listener.setUnit(1);
listener.setListening(true);
listener.run();

So right now I'm getting an Illegal Data Address error at trans.execute()
and my questions are:

What exactly are the two parameters on the Request

req = new ReadInputDiscretesRequest(ref, count);

Where do I define the UnitId the Master has to access (in the Master class).

catch23
  • 17,519
  • 42
  • 144
  • 217

1 Answers1

4

I hope I'm not too late to share my thoughts on this. I too find J2Mod confusing at times.

For your first question - "What exactly are the two parameters on the Request?": On a standard Modbus Device data is stored in 4 tables with 9999 values each:

  1. Coils (Discrete Output Coils) - they are read-write and they are addressed from 0000 to 270E internally. They occupy registers 1-9999 on the Modbus device.
  2. Discrete Inputs (Discrete Input Contacts) - they are read-only and they are addressed from 0000 to 270E internally. They occupy registers 10001-19999 on the Modbus device.
  3. Input Registers (Analog Input Registers) - they are read-only and they are addressed from 0000 to 270E internally. They occupy registers 30001-39999 on the Modbus device.
  4. Holding Registers (Analog Output Registers) - they are read-write and they are addressed from 0000 to 270E internally. They occupy registers 40001-49999 on the Modbus device.

Each Coil(1) or Contact(2) is 1 bit (1 byte).

Each Register(3,4) is 1 word or 16 bits (2 bytes).

Going back to your question, ref is the contact (in your case) that you want to read. For example, if you want to read the first contact under number 10001, you would pass 0 (as they are addressed 0000-270E internally). The second parameter, count, is the number of contacts (bits) that you want to read. If you only want to read contact 10001 then you can pass 1 (one contact only).

As for your second question, I'm not sure as I don't have much experience with J2mod.

Usually on Modbus TCP/IP you have the MBAP Header which is 7 bytes.

  • Transaction ID - 2 bytes
  • Protocol ID - 2 bytes (0000 for Modbus)
  • Length - 2 bytes - this shows the number of bytes that follow (includes Unit ID)
  • Unit ID - 1 byte

I'll have a look on how to set the ID and will get back to you with my findings.

Hope you are making progress!

Bazindrix
  • 1,041
  • 8
  • 8
  • First of all thanks for anwering, I kind of solved some of the problems by myself. And I at least am able to read and write coils right now. Your explanation on how the data is stored sure is useful to me though so thanks for that :) – JulianGartner Mar 17 '17 at 16:47