Witam.
Od niedawna bawię się w programowanie, obecnie pracuję hobbistycznie nad aplikacją, która będzie komunikować się ze sterownikiem PLC po modbusie i ściągać z niego dane. W tym celu wykorzystuje znalezioną w internecie bibliotekę Jamod. Jednak podczas wysyłania zapytania (wywołanie fukcji execute) dostaję wyjątek:

java.lang.NullPointerException at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:168)

i oczywiście brak odpowiedzi, ze strony sterownika. Aplikację pisze pod NetBeans'em

Czy może ktoś wie, w czym leży przyczyna takiego pojawiania się błędu i gdzie szukać rozwiązania?
A może lepiej wykorzystać jakaś inną bibliotekę? Prosiłbym o rade.

Poniżej umieściłem kawałek programu. Z góry dziękuję za odpowiedź!!

public int funk() {
SerialConnection con = null; //the connection
ModbusSerialTransaction trans = null; //the transaction
ReadInputRegistersRequest req = null; //the request
ReadInputRegistersResponse res = null; //the response

int unitid = 1; //the unit identifier we will be talking to
int ref = 100; //the reference, where to start reading from
int count = 1; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction
String portName = "COM1";

int liczba=0;

ModbusCoupler.getReference().setUnitID(1);
SerialParameters params = new SerialParameters();
params.setPortName(portName);
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("Odd");
params.setStopbits(1);
params.setEncoding("rtu");
params.setEcho(false);
con = new SerialConnection(params);

req = new ReadInputRegistersRequest(ref, count);
req.setUnitID(unitid);
req.setHeadless();

trans = new ModbusSerialTransaction(con);
trans.setRequest(req);

try {
trans.execute();
} catch (ModbusIOException ex) {
Logger.getLogger(Okno_mod.class.getName()).log(Level.SEVERE, null, ex);
} catch (ModbusSlaveException ex) {
Logger.getLogger(Okno_mod.class.getName()).log(Level.SEVERE, null, ex);
} catch (ModbusException ex) {
Logger.getLogger(Okno_mod.class.getName()).log(Level.SEVERE, null, ex);
} catch(Exception ex){
String aa;
ex.printStackTrace();
}
int k = 0;
do {
res = (ReadInputRegistersResponse) trans.getResponse();
for (int n = 0; n < res.getWordCount(); n++) {
//liczba = res.getRegisterValue(n);
System.out.println("Word " + n + "=" + res.getRegisterValue(n));
}
k++;
} while (k < repeat);

con.close();
return liczba;
}