We are trying to see if Modbus4J will work in our upcoming product. We are making a slave (server) application.
I can get the TCP slave to work just fine, but when I try to create an RTU or ASCII slave, I get a NullPointerException immediately after calling ModbusSlaveSet start() method.
Here's the code:
public static void main(final String[] args)
throws Exception
{
ModbusSlaveSet listener;
boolean tcp = false; // TCP works, but RTU and ASCII do not
final ModbusFactory modbusFactory = new ModbusFactory();
if (tcp)
{
listener = modbusFactory.createTcpSlave(false);
}
else
{
final SerialParameters serialParams = new SerialParameters();
serialParams.setCommPortId("/dev/ttyUSB0");
serialParams.setBaudRate(9600);
serialParams.setDataBits(8);
serialParams.setStopBits(1);
serialParams.setParity(2);
listener = modbusFactory.createRtuSlave(serialParams);
}
final BasicProcessImage processImage = new BasicProcessImage(1);
processImage.setAllowInvalidAddress(true);
processImage.setInvalidAddressValue((short)0);
listener.addProcessImage(processImage);
new Thread(new Runnable()
{
public void run()
{
try
{
listener.start();
}
catch (ModbusInitException e)
{
e.printStackTrace();
}
}
}).start();
}
Here's the output:
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
RXTX Warning: Removing stale lock file. /var/lock/LCK..ttyUSB0
com.serotonin.modbus4j.exception.ModbusInitException: java.lang.NullPointerException
at com.serotonin.modbus4j.serial.SerialSlave.start(SerialSlave.java:31)
at com.serotonin.modbus4j.serial.rtu.RtuSlave.start(RtuSlave.java:23)
at com.prozesstech.spectron.sandbox.Modbus4JTest$1.run(Modbus4JTest.java:103)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at com.serotonin.messaging.StreamTransport.start(StreamTransport.java:32)
at com.serotonin.modbus4j.serial.SerialSlave.start(SerialSlave.java:28)
... 3 more
Any idea what might be going on?
Thanks,
-Rob