J2Mod basic Master and Slave Questions

不羁的心 提交于 2019-12-22 12:54:21

问题


I am trying to make an 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)

thx in advance


回答1:


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!



来源:https://stackoverflow.com/questions/42356179/j2mod-basic-master-and-slave-questions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!