kochedykov / jlibmodbus

JLibModbus - is an implementation of the Modbus protocol v1.1b in java language. Java modbus library. It works. Welcome.
http://kochedykov.github.io/jlibmodbus/
Apache License 2.0
307 stars 128 forks source link

Serial RTU Slave doesn't work properly on multi device network. #75

Open du4 opened 2 years ago

du4 commented 2 years ago

Hello everyone! This is my test code

   public class Main {
    final static private int slaveId = 49;
    public static void main(String[] args) {
        String portName = args.length>0 ? args[0] : "/dev/ttyS4";
        try {
            Modbus.setLogLevel(Modbus.LogLevel.LEVEL_DEBUG);
            SerialParameters serialParameters = new SerialParameters(
                    portName,
                    SerialPort.BaudRate.BAUD_RATE_9600,
                    8,
                    1,
                    SerialPort.Parity.NONE
            );
            SerialUtils.setSerialPortFactory(new SerialPortFactoryJSSC());
            ModbusSlave slave = ModbusSlaveFactory.createModbusSlaveRTU(serialParameters) ;
            slave.setServerAddress(slaveId);
            slave.setBroadcastEnabled(true);
            slave.setReadTimeout(args.length>1 ? Integer.valueOf(args[1]) : 10000);

            FrameEventListener listener = new FrameEventListener() {
                @Override
                public void frameSentEvent(FrameEvent event) {
                    System.out.println("frame sent " + DataUtils.toAscii(event.getBytes()));
                }
                @Override
                public void frameReceivedEvent(FrameEvent event) {
                    System.out.println("frame recv " + DataUtils.toAscii(event.getBytes()));
                }
            };
            slave.addListener(listener);
            ModbusHoldingRegisters holdingRegisters = new ModbusHoldingRegisters(1000);
            for (int i = 0; i < holdingRegisters.getQuantity(); i++) {
                holdingRegisters.set(i, i + 1);
            }
            holdingRegisters.setFloat32At(0, (float)(Math.PI * Math.random()));
            slave.getDataHolder().setInputRegisters(holdingRegisters);
            slave.getDataHolder().setHoldingRegisters(new ModbusHoldingRegisters(1000));
            slave.listen();
        } catch (ModbusProtocolException | ModbusIOException | SerialPortException e) {
            e.printStackTrace();
        }
    }
}

This is output:

  INFO: Frame recv: 04031B6C000482A5
  frame recv 04031B6C000482A5
  INFO: Frame recv: 040308438CFE
  frame recv 040308438CFE
  WARNING: End address out of bounds : 38209
  INFO: Frame recv: 5041
  frame recv 5041
  WARNING: null
  INFO: Frame recv: 8BAE
  frame recv 8BAE
  WARNING: null
  INFO: Frame recv: C040
  frame recv C040
  WARNING: null
  INFO: Frame recv: 0204031B5800
  frame recv 0204031B5800
  WARNING: End address out of bounds : 23323
  INFO: Frame recv: 04C3
  frame recv 04C3
  WARNING: null
  INFO: Frame recv: 6B040308439A
  frame recv 6B040308439A
  WARNING: End address out of bounds : 18082
  INFO: Frame recv: 556F
  frame recv 556F
  WARNING: null
  INFO: Frame recv: 45B2
  frame recv 45B2
  WARNING: null
  INFO: Frame recv: 53EC
  frame recv 53EC
  WARNING: null
  INFO: Frame recv: AA6D
  frame recv AA6D
  WARNING: null
  INFO: Frame recv: 070403E80002F1DD
  frame recv 070403E80002F1DD
  INFO: Frame recv: 070404440400
  frame recv 070404440400
  WARNING: End address out of bounds : 2116
 ...

It seems like it get all the packets of all the devices in a network and get some invalid frames too. As a result slave is almost don't respond for request.

megatof commented 1 year ago

Hello @du4 I have the same problem! Did you find a solution?