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.