• Recent
    • Tags
    • Popular
    • Register
    • Login

    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 Mango 5 Documentation Website

    NullPointerException when trying to create RTU or ASCII Slave

    Modbus4J general discussion
    1
    3
    2.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rstoeck
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • R
        rstoeck
        last edited by

        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?

        1 Reply Last reply Reply Quote 0
        • First post
          Last post