Modbus4J throwing error.
-
I've tested this code to detect a Modbus device ( was discovered by a discovery software) connected to my computer.
but the code throw a null pointer Exception, can someone help me understand it.public class MasterTest {
public static void main(String[] args) throws Exception {String commPortId = "/dev/ttyUSB0"; int baudRate = 9600; int flowControlIn = 0; int flowControlOut = 0; int dataBits = 0; int stopBits = 0; int parity = 1; TestSerialPortWrapper wrapper = new TestSerialPortWrapper(commPortId, baudRate, flowControlIn, flowControlOut, dataBits, stopBits, parity); IpParameters ipParameters = new IpParameters(); ipParameters.setHost("localhost"); ModbusFactory modbusFactory = new ModbusFactory(); ModbusMaster master = modbusFactory.createRtuMaster(wrapper); try { master.setTimeout(1000); master.setRetries(0); master.init(); int slaveId = 2; BaseLocator<Number> locator = BaseLocator.holdingRegister(slaveId, 50, DataType.EIGHT_BYTE_INT_UNSIGNED); master.setValue(locator, 10000000); System.out.println(master.getValue(locator)); } finally { master.destroy(); } }
-
wrapper.getInputStream is returning null.
-
Hi vijay,
Any chance you read line 14 of TestSerialPortWrapper?
/* * This class is not finished */
-
@phildunlap OMG thanks, but how can we fix this?
-
The most proactive thing you could do for the issue of the null pointer is perhaps to fork the repository, implement some tests that use input and output streams, finish the TestSerialPortWrapper class, and submit a pull request.
But, if you're trying to talk to an actual device and not simply playing with the test code, then perhaps you are wanting to use your own implementing class of SerialPortWrapper which presents the input and output streams of your serial port in the methods that return null in the test class.
In Mango, our Modbus module makes a call to the SerialPortManager to get a proxy, and then returns the input stream and output stream from the proxy.
https://github.com/infiniteautomation/ma-core-public/blob/main/Core/src/com/infiniteautomation/mango/io/serial/SerialPortManagerImpl.java#L205
returns
https://github.com/infiniteautomation/ma-core-public/blob/main/Core/src/com/infiniteautomation/mango/io/serial/JsscSerialPortProxy.java
for non-virtual serial ports in Mango, which uses Jssc to actually handle the serial nitty-gritty.