There are cases in which an application may wish to communicate with a modbus device that is sitting behind a "serial device server", for example, a Moxa 5130 (http://www.moxa.com/product/NPort_5100A.htm).
This is Modbus-over-TCP (not the same as Modbus-TCP); also known as Modbus-RTU/IP. The application builds a Modbus-RTU message, including checksum, and then sends it over a TCP/IP connection to the gateway device, which puts the message on the Modbus wire -- the server takes any modbus messages it receives over the modbus wire and sends them back over the TCP connection to the requesting application.
I have written some code to handle this (it's a pretty trivial extension) and am happy to contribute it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
We also have the requirement for connecting to devices behind serial device server.
We will need to connect to MOXA Nport 5232. As you have mentioned your implementation is Modbus RTU over TCP/IP, I guess this would work in our case as well.
Please let me know if you can share some samples on implementation.
Thanks,
Sumeet
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here are the simplest adaptations to this use case, which is the most
common one I suppose. One could (should) create new classes
for that purpose, but changing the original files would also work if
just a local copy of the j2mod package is needed:
i) in the function writeMessage in io/ModbusTCPTransport.java add
the CRC to the contents by adding 4 lines before the final write():
...
if (message != null && message.length > 0)
m_ByteOut.write(message);
/* block of 4 lines to be inserted: RJM 2014-07-08 */
int len = m_ByteOut.size();
int[] crc = ModbusUtil.calculateCRC(m_ByteOut.getBuffer(), 0, len);
m_ByteOut.writeByte(crc[0]);
m_ByteOut.writeByte(crc[1]);
m_Output.write(m_ByteOut.toByteArray());
...
ii) as a basis for the main reader start from cmd/ReadInputRegistersTest.java
and insert a setheadless() in the loop over the repeats:
// 3. Create the command.
req = new ReadInputRegistersRequest(ref, count);
req.setUnitID(unit);
req.setHeadless(); // RJM 2014-07-08
and insert a setHeadless() in the (derived) class also in the loop:
/*** Convert four successive bytes to a JUMO specific float value.* @param b1 The array of two bytes, in the MODBUS order* @param b2 The array of two bytes, in the MODBUS order* @return The float value obtained by glueing the two bytes*/
publicstaticfloattoFloat(finalbyte[]b1,finalbyte[]b2,finalbooleanswap){/* two ways of doing it: first b1 or first b2 ? */intbits;if(swap){bits=b2[0]&0xff;bits=(bits<<8)+(b2[1]&0xff);bits=(bits<<8)+(b1[0]&0xff);bits=(bits<<8)+(b1[1]&0xff);}else{bits=b1[0]&0xff;bits=(bits<<8)+(b1[1]&0xff);bits=(bits<<8)+(b2[0]&0xff);bits=(bits<<8)+(b2[1]&0xff);}returnFloat.intBitsToFloat(bits);}/* toFloat */
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm actually trying to communicate via TCP/IP to a RTU slave setup but I'm not able to modify the libraries of j2mod, is there another way to get working CRC calculations on my TCP transaction? I have a full TCP/IP master working, but needing CRC calculations and slaveID setup to the modbus message.
thanks in advance
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There are cases in which an application may wish to communicate with a modbus device that is sitting behind a "serial device server", for example, a Moxa 5130 (http://www.moxa.com/product/NPort_5100A.htm).
This is Modbus-over-TCP (not the same as Modbus-TCP); also known as Modbus-RTU/IP. The application builds a Modbus-RTU message, including checksum, and then sends it over a TCP/IP connection to the gateway device, which puts the message on the Modbus wire -- the server takes any modbus messages it receives over the modbus wire and sends them back over the TCP connection to the requesting application.
I have written some code to handle this (it's a pretty trivial extension) and am happy to contribute it.
Hi Chris,
We also have the requirement for connecting to devices behind serial device server.
We will need to connect to MOXA Nport 5232. As you have mentioned your implementation is Modbus RTU over TCP/IP, I guess this would work in our case as well.
Please let me know if you can share some samples on implementation.
Thanks,
Sumeet
This should be the standard case of making the packet "headless". What am I missing here?
Here are the simplest adaptations to this use case, which is the most
common one I suppose. One could (should) create new classes
for that purpose, but changing the original files would also work if
just a local copy of the j2mod package is needed:
i) in the function writeMessage in io/ModbusTCPTransport.java add
the CRC to the contents by adding 4 lines before the final write():
ii) as a basis for the main reader start from cmd/ReadInputRegistersTest.java
and insert a setheadless() in the loop over the repeats:
and insert a setHeadless() in the (derived) class also in the loop:
Optionally add a function for JUMO devices:
Hello.
I'm actually trying to communicate via TCP/IP to a RTU slave setup but I'm not able to modify the libraries of j2mod, is there another way to get working CRC calculations on my TCP transaction? I have a full TCP/IP master working, but needing CRC calculations and slaveID setup to the modbus message.
thanks in advance
You need to mark the packets as "headless". When you do that the 6 byte TCP header is skipped and the two byte CRC is added.