Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
du4 opened this issue May 4, 2022 · 1 comment
Open

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

du4 opened this issue May 4, 2022 · 1 comment

Comments

@du4
Copy link

du4 commented May 4, 2022

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
Copy link

megatof commented Mar 8, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants