H
Hi all,
I'm a newbie in Modbus world, so apologize possible inaccuracies. I'm trying to read register values from a device connected to a Modbus RTU, via USB_485.
I tried to use Jamod, by modifying an existing class, here the snippet:<pre>
public static void main(String[] args) {
SerialConnection con = null;
ModbusSerialTransaction trans = null;
ReadInputDiscretesRequest req = null;
ReadInputDiscretesResponse res = null;
String portname = null;
int unitid = 0;
int ref = 0;
int count = 0;
int repeat = 1;
//int delay =0;
try {
//1. Setup the parameters
try {
portname = "COM23";
unitid = Integer.parseInt("2");//device address
ref = Integer.parseInt("40001");
count = Integer.parseInt("1");
repeat = Integer.parseInt("0");
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
//2. Set slave identifier for master response parsing
ModbusCoupler.getReference().setUnitID(unitid);
System.out.println("net.wimpi.modbus.debug set to: " +
System.getProperty("net.wimpi.modbus.debug"));
//3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("rtu");
params.setEcho(false);
System.out.println("Encoding [" + params.getEncoding() + "]");
//params.setReceiveTimeout(1000);
//4. Open the connection
con = new SerialConnection(params);
con.open();
//5. Prepare a request
req = new ReadInputDiscretesRequest(ref, count);
req.setUnitID(unitid);
req.setHeadless();
System.out.println("Request: " + req.getHexMessage());
//6. Prepare the transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);
//7. Execute the transaction repeat times
int k = 0;
do {
trans.setTransDelayMS(1000);
trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();
System.out.println("Response: " + res.getHexMessage());
BitVector inputs = res.getDiscretes();
byte ret[] = new byte[inputs.size()];
for (int i = 0; i < count; i++) {
System.out.println("Bit " + i + " = " + inputs.getBit(i));
}
k++;
} while (k < repeat);
//8. Close the connection
con.close();
} catch (Exception ex) {
ex.printStackTrace();
// Close the connection
con.close();
}
}//main</pre>
but I receive a read error:
Sent: 02 02 9c 41 00 01 c7 bd
Last request: 02 02 9c 41 00 01 c7 bd
Error reading response
net.wimpi.modbus.ModbusIOException: I/O exception - failed to read
at net.wimpi.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:163)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:187)
at net.wimpi.modbus.cmd.SerialDITest.main(SerialDITest.java:105)
The RX led of the device blink during the connection, but TX led do not. I changed parameters many times, then added a transfer delay, but nothing.
I then tried to use JSSC, because the testing software for the device used that library (testing software works without problems, so there aren't physical issues), but I didn't find a way or an example to "ask" a register.
I wrote something like:<pre>
public class ReadTry {
public static void main(String[] args) {
SerialPort serialPort = new SerialPort("COM23");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(9600, 8, 1, 0);//Set params.
int i=0;
do {
i++;
serialPort.writeString("my request to the device I guess");
}while(i<100);
byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
System.out.println(Arrays.toString(buffer));
serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}</pre>
but obviously write "Hello device" through the COM port is not a good approach.
Any help with Jamod or with JSSC will be highly appreciated.
I'm a newbie in Modbus world, so apologize possible inaccuracies. I'm trying to read register values from a device connected to a Modbus RTU, via USB_485.
I tried to use Jamod, by modifying an existing class, here the snippet:<pre>
public static void main(String[] args) {
SerialConnection con = null;
ModbusSerialTransaction trans = null;
ReadInputDiscretesRequest req = null;
ReadInputDiscretesResponse res = null;
String portname = null;
int unitid = 0;
int ref = 0;
int count = 0;
int repeat = 1;
//int delay =0;
try {
//1. Setup the parameters
try {
portname = "COM23";
unitid = Integer.parseInt("2");//device address
ref = Integer.parseInt("40001");
count = Integer.parseInt("1");
repeat = Integer.parseInt("0");
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
//2. Set slave identifier for master response parsing
ModbusCoupler.getReference().setUnitID(unitid);
System.out.println("net.wimpi.modbus.debug set to: " +
System.getProperty("net.wimpi.modbus.debug"));
//3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("rtu");
params.setEcho(false);
System.out.println("Encoding [" + params.getEncoding() + "]");
//params.setReceiveTimeout(1000);
//4. Open the connection
con = new SerialConnection(params);
con.open();
//5. Prepare a request
req = new ReadInputDiscretesRequest(ref, count);
req.setUnitID(unitid);
req.setHeadless();
System.out.println("Request: " + req.getHexMessage());
//6. Prepare the transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);
//7. Execute the transaction repeat times
int k = 0;
do {
trans.setTransDelayMS(1000);
trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();
System.out.println("Response: " + res.getHexMessage());
BitVector inputs = res.getDiscretes();
byte ret[] = new byte[inputs.size()];
for (int i = 0; i < count; i++) {
System.out.println("Bit " + i + " = " + inputs.getBit(i));
}
k++;
} while (k < repeat);
//8. Close the connection
con.close();
} catch (Exception ex) {
ex.printStackTrace();
// Close the connection
con.close();
}
}//main</pre>
but I receive a read error:
Sent: 02 02 9c 41 00 01 c7 bd
Last request: 02 02 9c 41 00 01 c7 bd
Error reading response
net.wimpi.modbus.ModbusIOException: I/O exception - failed to read
at net.wimpi.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:163)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:187)
at net.wimpi.modbus.cmd.SerialDITest.main(SerialDITest.java:105)
The RX led of the device blink during the connection, but TX led do not. I changed parameters many times, then added a transfer delay, but nothing.
I then tried to use JSSC, because the testing software for the device used that library (testing software works without problems, so there aren't physical issues), but I didn't find a way or an example to "ask" a register.
I wrote something like:<pre>
public class ReadTry {
public static void main(String[] args) {
SerialPort serialPort = new SerialPort("COM23");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(9600, 8, 1, 0);//Set params.
int i=0;
do {
i++;
serialPort.writeString("my request to the device I guess");
}while(i<100);
byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
System.out.println(Arrays.toString(buffer));
serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}</pre>
but obviously write "Hello device" through the COM port is not a good approach.
Any help with Jamod or with JSSC will be highly appreciated.