Closed WolfgangFahl closed 7 years ago
just found https://sourceforge.net/p/j2mod/discussion/general/thread/0afca16d/ and trying the debug and Encoding options
serialParams.setEncoding(Modbus.SERIAL_ENCODING_RTU);
serialParams.setEcho(true);
System.setProperty("com.ghgande.modbus.debug", "true");
now I get WARNUNG: Stacktrace: com.ghgande.j2mod.modbus.ModbusIOException: I/O exception - failed to read response for request [01 03 03 E8 00 02] - getResponse serial port exception - Cannot read from serial port - truncated
I believe part of the problem is that you aren't setting up your SerialParameters object correctly.
Here's a class that I used to test this library on a VirtualBox VM running Ubuntu 14.04LTS with a USB-to-RS232 converter (ttyUSB0)
import com.ghgande.j2mod.modbus.; import com.ghgande.j2mod.modbus.facade.ModbusSerialMaster; import com.ghgande.j2mod.modbus.net.; import com.ghgande.j2mod.modbus.procimg.Register; import com.ghgande.j2mod.modbus.util.*;
public class ModbusMaster {
/**
* @param args
*/
public static void main(String[] args) {
/* The important instances of the classes mentioned before */
ModbusSerialMaster serialMaster = null; // the connection
/* Variables for storying the parameters */
String portname = "/dev/ttyUSB0"; // the name of the serial port to be used
int unitID = 2; // the unit identifier we will be talking to
int startingRegister = 0; // the reference, where to start reading from
int registerCount = 4; // the count of the input registers to read
Register[] slaveResponse = new Register[registerCount];
try {
/* Setup the serial parameters */
SerialParameters parameters = new SerialParameters();
parameters.setPortName(portname);
parameters.setBaudRate(9600);
parameters.setDatabits(8);
parameters.setParity(AbstractSerialConnection.NO_PARITY);
parameters.setStopbits(AbstractSerialConnection.ONE_STOP_BIT);
parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU);
parameters.setEcho(false);
/* Open the connection */
serialMaster = new ModbusSerialMaster(parameters);
serialMaster.connect();
} catch (Exception exception) {
exception.printStackTrace();
}
/* Read the first four holding registers */
try {
slaveResponse = serialMaster.readMultipleRegisters(unitID, startingRegister, registerCount);
for (int i = 0; i < slaveResponse.length; i++) {
System.out.println("reg" + i + " = " + slaveResponse[i]);
}
} catch (ModbusException e) {
e.printStackTrace();
}
/* Close the connection */
serialMaster.disconnect();
} // main
} // class ModbusMaster
(please forgive me for not being able to figure out how to properly place code into a comment)
Please note that this is very crude code and I would not use this in a production setting, but I was using this as a simple test of the serial capabilities of this library on Linux.
Something else you may be running up against is privilege restrictions on the port. Check the jSerialComms page for instructions on placing your user into the correct group (e.g. dialout, tty, etc.)
I hope this helps!
thankx. These modifications seem to make the difference:
@@ -35,10 +35,10 @@ public class SModbus {
serialParams.setPortName(portName);
serialParams.setBaudRate(9600);
serialParams.setDatabits(8);
- serialParams.setParity("None");
- serialParams.setStopbits(1);
+ serialParams.setParity(AbstractSerialConnection.NO_PARITY);
+ serialParams.setStopbits(AbstractSerialConnection.ONE_STOP_BIT);
serialParams.setEncoding(Modbus.SERIAL_ENCODING_RTU);
- serialParams.setEcho(true);
+ serialParams.setEcho(false);
BTW code comments can be created with three backticks or clicking the "insert code" icon
I was facing this issue...
I cloned the code and installed via mvn install -DskipTests
and magically worked.
I am also facing the same issue.. Will somebody help me?
Thank you for keeping this library with an excellent api alive. I am having issues comparable to:
with the following code on a raspberry PI using /dev/ttyUSB0 with a UART USB-connector applied. The connection works with QModBus at 9600 baud, 8 databits 1 stop bit and no parity. What might be wrong in this setup and how can I debug it?