• 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

    Feature Request: run the main loop of start() method of TcpSlave/UdpSlave in another thread

    Wishlist
    2
    2
    568
    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.
    • G
      gbccccc
      last edited by

      Thanks for you guys' easy-of-use Modbus implementation at the beginning!

      Recently I have been developing a web system used to test Modbus (TCP) devices. What I want to implement now is to offer system users an API to create some Modbus slaves running on the server.
      However, when I want to return whether the new slave is started successfully (IMO, main loop start can means success) to the frontend, it turns out that tcpSlave.start() won't end until an exception happened. If I run this method in a new thread, still I cannot detect whether the slave is started successfully or the port has been occupied. Ways like setting a timeout are not elegant IMO. I think if the main loop of this method is run in a new thread instead of the main thread calling the method, it will be much more convenient for me to use this method.

      Regarding code:

          /** {@inheritDoc} */
          @Override
          public void start() throws ModbusInitException {
              try {
                  serverSocket = new ServerSocket(port);
      
                  Socket socket;
                  while (true) {
                      socket = serverSocket.accept();
                      TcpConnectionHandler handler = new TcpConnectionHandler(socket);
                      executorService.execute(handler);
                      synchronized (listConnections) {
                          listConnections.add(handler);
                      }
                  }
              }
              catch (IOException e) {
                  throw new ModbusInitException(e);
              }
          }
      
      terrypackerT 1 Reply Last reply Reply Quote 0
      • terrypackerT
        terrypacker @gbccccc
        last edited by terrypacker

        @gbccccc first you need to define what "new slave is started successfully means".

        If just binding to the port and waiting for connections is enough then you can assume that if you call the method and a ModbusInitException is thrown then it is not running. Otherwise it is bound to the port and waiting for connections.

        If you want to know that something is connected you could extend the TcpSlave class like this:

        public class MonitoredTcpSlave extends TcpSlave {
        
            public MonitoredTcpSlave(int port, boolean encapsulated) {
                super(port, encapsulated);
            }
        
            public boolean hasConnection() {
                return !listConnections.isEmpty();
            }
        }
        

        As a last resort you could create your own TcpSlave by extending com.serotonin.modbus4j.ModbusSlaveSet.ModbusSlaveSet

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