I have a device connected via a serial port using the Modbus protocol. I learned how to read input registers, write holding registers, but I can't read files. To do this, the command 0x14 must be used, this is provided by the method ReadFileRecordRequest.
Below is my code but after executing I get an error com.ghgande.j2mod.modbus.ModbusException: Assertion failed, transaction not executable Where I made mistake?
import com.ghgande.j2mod.modbus.ModbusException;
import com.ghgande.j2mod.modbus.io.ModbusSerialTransaction;
import com.ghgande.j2mod.modbus.msg.*;
import net.wimpi.modbus.net.SerialConnection;
import net.wimpi.modbus.util.SerialParameters;
public class j2mod {
public static void main(String[] args) {
ModbusSerialTransaction trans = null;
ReadFileRecordRequest req = null;
ReadFileRecordResponse res = null;
SerialConnection con = null;
int unitid = 1; // the unit identifier we will be talking to
int repeat = 1; // a loop for repeating the transaction
// Set port settings
SerialParameters params = new SerialParameters();
params.setPortName("COM482");
params.setBaudRate("9600");
params.setDatabits("8");
params.setParity("NONE");
params.setStopbits("1");
params.setEncoding("rtu");
params.setEcho(false);
// Open connection
con = new SerialConnection(params); // set connection with parameters
try {
con.open(); // open
} catch (Exception e) {
e.printStackTrace();
}
// 4. Open the connection
try {
// 5. Prepare a request
req = new ReadFileRecordRequest();
req.setUnitID(unitid);
req.setHeadless();
// 6. Prepare a transaction
trans = new ModbusSerialTransaction();
trans.setRequest(req);
int k = 0;
do {
trans.setTransDelayMS(50);
trans.execute();
res = (ReadFileRecordResponse) trans.getResponse();
for (int n = 0; n < res.getByteCount(); n++) {
System.out.println("Word " + n + "=" + res.getRecordCount());
}
k++;
} while (k < repeat);
con.close();
// 8. Close the connection
} catch (ModbusException ex) {
System.out.print(ex);
}
}
}
Please help me find the error
I'm trying to output the reading result to the console