Serial Modbus Slave doesn't respond

552 views Asked by At

I have trouble starting a Modbus Slave through RTU.

TCP works fine but RTU seems to stop at listening stage.

I think it goes on an infinite loop(more like a hang) so I tried to look for something like timeout method but couldn't find any.

P.S.Since it's not an error, I can't handle it at catch phrase either. It just stops at that line.

case "TCP":
    try
    {
        TcpListener slaveTcpListener = new TcpListener(IP, Port);
        slaveTcpListener.Start();
        slave = ModbusTcpSlave.CreateTcp(SlaveID, slaveTcpListener);
        slave.DataStore = DataStoreFactory.CreateDefaultDataStore(MaxCoil, MaxDI, MaxHR, MaxIR);
        slave.DataStore.DataStoreReadFrom += new EventHandler<DataStoreEventArgs>(dataStore_DataStoreReadFrom);
        slave.ModbusSlaveRequestReceived += Slave_ModbusSlaveRequestReceived;

        slave.Listen();

        ReadShm.IsBackground = true;
        ReadShm.Start();
    }
    catch
    {

    }
case "RTU":
    try
    {
        SerialPort ModbusSerialPort = new SerialPort();

        ModbusSerialPort.PortName = strPortName;
        ModbusSerialPort.BaudRate = nBaudRate;
        ModbusSerialPort.DataBits = nDataBits;
        ModbusSerialPort.Parity = Parity.None;
        ModbusSerialPort.StopBits = StopBits.One;        

        ModbusSerialPort.Open();

        ModbusSerialSlave Slave;

        // Create RTU
        Slave = ModbusSerialSlave.CreateRtu(1, ModbusSerialPort);
        Slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
        Slave.ModbusSlaveRequestReceived += Slave_ModbusSlaveRequestReceived;
        Slave.DataStore.DataStoreReadFrom += dataStore_DataStoreReadFrom;

        Slave.Listen();

        ReadShm.IsBackground = true;
        ReadShm.Start();
    }
    catch
    {

    }
0

There are 0 answers