Crc errors using jamod with Rxtx library

W

Thread Starter

walgrind

I wrote simple modbus application in java using jamod library compiled with rxtx library. I use this application to pool modbus devices over our network. We have several Modbus devices connected to the network with RealPort converter (RS485->ethernet).

When I pool any device with Modpoll, I get always the correct answer. But when I use my application, I get often CRC error instead of the correct answer.

I use Modbus RTU encoding with 9600 baudrate, 8 databits, parity set to none and 1 stop bit.

Modpoll has as default 1000 ms delay between pools. I set the same delay in my app but it did not help.

This is source code (it comes from jamod library examples, I just added small modifications):<pre>
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.util.*;

public class Mbtest {

public static void main(String[] args) {

/* The important instances of the classes mentioned before */
SerialConnection con = null; // the connection
ModbusSerialTransaction trans = null; // the transaction
ReadInputRegistersRequest req = null; // the request
ReadInputRegistersResponse res = null; // the response

// Variables for storing the parameters
String portname = null; // the name of the serial port to be used
int unitid = 0; // the unit identifier we will be talking to
int ref = 0; // the reference, where to start reading from
int count = 0; // the count of IR's to read
int timeout = 0; // a timeout for receiving response
int retries = 0;
int repeat = 1; // a loop for repeating the transaction
int delay = 0; // a delay for repeating the transaction

// 1. Setup the parameters
if (args.length < 5) {
System.exit(1);
} else {
try {
portname = args[0];
unitid = Integer.parseInt(args[1]);
ref = Integer.parseInt(args[2]);
count = Integer.parseInt(args[3]);
timeout = Integer.parseInt(args[4]);
retries = Integer.parseInt(args[5]);
delay = Integer.parseInt(args[6]);
if (args.length == 8) {
repeat = Integer.parseInt(args[7]);
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
System.setProperty("gnu.io.rxtx.SerialPorts", portname);


// 2. Set master identifier
// ModbusCoupler.createModbusCoupler(null);
ModbusCoupler.getReference().setUnitID(1);

// 3. Setup serial parameters
try {
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("rtu");
params.setReceiveTimeout(timeout);
params.setEcho(false);

// 4. Open the connection
con = new SerialConnection(params);
// 7. Open connection repeat times

int k = 0;
do {
try {
con.open();

// 5. Prepare a request
req = new ReadInputRegistersRequest(ref, count);
req.setUnitID(unitid);
req.setHeadless();

// 6. Prepare a transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);
trans.setRetries(retries);
trans.setTransDelayMS(delay);


trans.execute();
res = (ReadInputRegistersResponse) trans.getResponse();

for (int n = 0; n < res.getWordCount(); n++) {
System.out.println("Word " + n + "="
+ res.getRegisterValue(n));
}
k++;

} finally {
con.close();
System.out.println("---------");
}
} while (k < repeat);

} catch (Throwable t) {
System.out.println("Exception: " + t.getMessage());
}
}
}</pre>
 
I am experiencing the same issue using Jamod and the older javax.comm serial api.

I have found the issue only occurs when using the read registers command, and only when I request a large block of registers. When I set count to something small (say 5 registers), the error does not occur.

ReadInputRegistersRequest(ref, count)

Additionally, I am trying to read a PLC with an octal register space, and reads that span two different octets cause an invalid reference (as the register reference is in decimal, and not all decimal references exist in octal).

Let me know if you figure this one out.
Ben
 
I poll usually two consecutive registers. But when I try to poll more, an issue also occurs.

Registers are polled in loop, one by one.
 
Both RxTx and Jamod, as downloaded from the Interwebs have issues.

That said, Jamod can be made to spew out debugging data. I'd look at the messages and make sure you aren't actually losing characters, or having excessive delays in packets that are causing the master or slave to assume the packet has ended. Once you know if your data is being lost, corrupted, or delayed, you'll have a clue which way to go.

I use both of those -- RxTx and Jamod -- and I run my buses between 70 and 90 percent busy, reading up to 50 registers at a whack, with no problems.
 
Top