W
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);
}
k++;
} finally {
con.close();
System.out.println("---------");
}
} while (k < repeat);
} catch (Throwable t) {
System.out.println("Exception: " + t.getMessage());
}
}
}</pre>
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);
}
k++;
} finally {
con.close();
System.out.println("---------");
}
} while (k < repeat);
} catch (Throwable t) {
System.out.println("Exception: " + t.getMessage());
}
}
}</pre>