• 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

    Modbus4J 2.0.2. Problems with the listener and the threads.

    Modbus4J general discussion
    1
    2
    2.3k
    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.
    • ramonhlR
      ramonhl
      last edited by

      Hello.
      I am testing the ListeterTest2.java and works fine.

      public class ListenerTest2Mon1 {
          static Random random = new Random();
          static float ir1Value = -100;
      
          public static void main(String[] args) throws Exception {
              ModbusFactory modbusFactory = new ModbusFactory();
              final ModbusSlaveSet listener = modbusFactory.createTcpSlave(false);
              listener.addProcessImage(getModscanProcessImage(127));       
      
              // When the "listener" is started it will use the current thread to run. So, if an exception is not thrown
              // (and we hope it won't be), the method call will not return. Therefore, we start the listener in a separate
              // thread so that we can use this thread to modify the values.
              new Thread(new Runnable() {
                  public void run() {
                      try {
                          listener.start();
                      }
                      catch (ModbusInitException e) {
                          e.printStackTrace();
                      }
                  }
              }).start();
      
              while (true) {
                  updateProcessImage1((BasicProcessImage) listener.getProcessImage(127));
                 
                  synchronized (listener) {
                      listener.wait(100);
                  }
              }
          }
      
          static void updateProcessImage1(BasicProcessImage processImage) {
              processImage.setNumeric(RegisterRange.HOLDING_REGISTER, 0, DataType.TWO_BYTE_INT_UNSIGNED,
                      random.nextInt(10000));
              processImage.setNumeric(RegisterRange.HOLDING_REGISTER, 2, DataType.FOUR_BYTE_INT_UNSIGNED_SWAPPED,
                      random.nextInt(10000));
              processImage.setNumeric(RegisterRange.HOLDING_REGISTER, 10, DataType.TWO_BYTE_INT_UNSIGNED,
                      random.nextInt(10000));
              processImage.setNumeric(RegisterRange.HOLDING_REGISTER, 12, DataType.FOUR_BYTE_INT_UNSIGNED_SWAPPED,
                      random.nextInt(10000));
              processImage.setNumeric(RegisterRange.HOLDING_REGISTER, 20, DataType.TWO_BYTE_INT_UNSIGNED,
                      random.nextInt(10000));
              processImage.setNumeric(RegisterRange.HOLDING_REGISTER, 22, DataType.FOUR_BYTE_INT_UNSIGNED_SWAPPED,
                      random.nextInt(10000));
          }
       
              static BasicProcessImage getModscanProcessImage(int slaveId) {
              BasicProcessImage processImage = new BasicProcessImage(slaveId);
              processImage.setAllowInvalidAddress(true);
              processImage.setInvalidAddressValue(Short.MIN_VALUE);
              processImage.setExceptionStatus((byte) 151);
      
              return processImage;
          }
      }
      

      But when I try to insert this code in my java program I have problems with the threads.
      I do:

      • Change the code of the listener test and create a new class.
      • In java I have a jframe with two buttons, start server, stop server.
      • In the jframe I create a new instance of my listener class.
      • In the start button I start using my class the listener.start().
      • The start button freezes, if I debug in the listener.start() the code stops.

      Do you have some example with listener in jframe or similar?

      Thanks in advance.

      Best regards.

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