Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

NullPointerException when trying to create RTU or ASCII Slave


  • 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


  • Adding to this discussion: the listener.start() calls into RtuSlave.start(), which immediately calls SerialSlave.start():

    in SerialSlave:

    
        @Override
        public void start() throws ModbusInitException {
            try {
                serialPort = SerialUtils.openSerialPort(serialParameters);
                transport = new StreamTransport(serialPort.getInputStream(), serialPort.getOutputStream());
                transport.start("Modbus4J SerialSlave");  // <--- This line throws the exception
            }
            catch (Exception e) {
                throw new ModbusInitException(e);
            }
        }
    
    

    The line that throws the NullPointerException is the third line, the transport.start("Modbus4J SerialSlave").

    This calls into the StreamTransport class of seroUtils.jar, which we do not have the source code for.

    Would it be possible for me to get this source code so that I can debug this problem?