I am new to java serial port programming, I have trying to read data from the modbus slave device through modbus RTU over serial port from my java application. I am using the Jamod java library to. Download jamod.jar. Apt src/site/apt/kb/serial_master_howto.apt src/site/apt/kb. Site/resources/images/serial_modbus_netarch.png src/site. Modbus4J Modbus RTU master This topic has. Jamod and Modbus4Java library. Modbus Master Init Error: com.serotonin.io.serial.SerialPortException.

Balasubramaniam - 2014-09-15

​Hi, i am new to java serial port programming​,

I have trying to read data from the modbus slave device through modbus RTU over serial port from my java application.

i am using the Jamod java library to read modbus protocal.

In my case my application failed to receive entire modbus response from the device. please find my java coding and error log for your reference.

Watkins

Any one can suggest me what may be the reason for the error.

Thanks in advance

ERROR
Serial Port Connection
[email protected]
Serial port status..true
Sent: 01 04 03 e8 00 05 b0 79
Last request: 01 04 03 e8 00 05 b0 79
CRC Error in received frame: 0 bytes:
Response: 01 84
execute try 1 error: I/O exception - failed to read
Clear input: 02 c2 c1
Sent: 01 04 03 e8 00 05 b0 79
Last request: 01 04 03 e8 00 05 b0 79
CRC Error in received frame: 0 bytes:
execute try 2 error: I/O exception - failed to read
Response: 01 84
Clear input: 02 c2 c1
Sent: 01 04 03 e8 00 05 b0 79
Last request: 01 04 03 e8 00 05 b0 79
CRC Error in received frame: 0 bytes:
Response: 01 84
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 modbusnewapplication.ModbusConnection.main(ModbusConnection.java:8

Serial Port modbus Program ---------------------------

package modbusnewapplication;

import java.io.;
import javax.comm.;
import net.wimpi.modbus.ModbusCoupler;
import net.wimpi.modbus.io.ModbusSerialTransaction;
import net.wimpi.modbus.msg.ReadInputRegistersRequest;
import net.wimpi.modbus.msg.ReadInputRegistersResponse;
import net.wimpi.modbus.net.SerialConnection;
import net.wimpi.modbus.util.SerialParameters;

public class ModbusConnection {

Modbus Serial Master Jamod Watkinsville Ga

public static void main(String[] args) {
//if (args.length < 4) {
// System.out.println('not enough args');
// System.exit(1);
//}else{
try {
System.out.println('Serial Port Connection');
/ 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= 'COM1'; //the name of the serial port to be used
int unitid = 1; //the unit identifier we will be talking to
int ref = 1000; //the reference, where to start reading from
int count = 5; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction
boolean isopen = false;
// 2. Set master identifier
// ModbusCoupler.createModbusCoupler(null);
// ModbusCoupler.getReference().setMaster(master); I added this in
// ModbusCoupler.getReference().setMaster(true);
// ModbusCoupler.getReference().setUnitID(1);

// 3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName('COM1');
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity('None');
params.setStopbits(1);
params.setEncoding('RTU');
params.setEcho(false);

System.setProperty('net.wimpi.modbus.debug', 'true');
// 4. Open the connection
con = new SerialConnection(params);
System.out.println('Connection..' + con.toString());
con.open();

isopen = con.isOpen();
System.out.println('Serial port status..' + isopen);
// 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);

// 7. Execute the transaction repeat times
int k = 0;
do {
trans.execute();
res = (ReadInputRegistersResponse) trans.getResponse();
for (int n = 0; n < res.getWordCount(); n++) {
System.out.println('Word ' + n + '=' + res.getRegisterValue(n));
}
k++;
} while (k < repeat);

// 8. Close the connection
con.close();

} catch (Exception ex) {
ex.printStackTrace();
}
//}//else
}//main

}//class SerialAITest