Please, help to organyze time out. I have a program which connects to another device by COM port. If device is disconnected and I try to connect this device, the time out exeption must be. Can anyone give me example of making time out here?

private void OnButton_Reading_Click(object sender, EventArgs e)
                using (SerialPort port = new SerialPort(textBox_port.Text))
                    // configure serial port
                    port.BaudRate = Convert.ToInt32(this.textBox_speed.Text);
                    port.DataBits = 8;
                    port.Parity = Parity.None;
                    port.StopBits = StopBits.One;
                    // create modbus master
                    IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
                    byte slaveId = Convert.ToByte(textBox_adress.Text);
                    ushort startAddress = UInt16.Parse(textBox_Register.Text, NumberStyles.HexNumber);
                    ushort numregisters = 1;
                    // read registers
                    ushort[] registers1 = master.ReadHoldingRegisters(slaveId, startAddress, numregisters);
                    string Mlb1 = Convert.ToString(registers1[0]);
                    this.textBox_Reading.Text = Mlb1;
            catch (Exception g)

I don't quite understand what you're saying, but if you want to make the program wait a few seconds, use the Thread.Sleep method:

System.Threading.Thread.Sleep(5000); // waits 5 seconds

You are using the NModbus ModbusSerialMaster.
If I remember correctly this uses the serial port's timeout settings.
Set port.ReadTimeout to a suitable value (in milliseconds).
The ModbusSerialMaster will retry 3 times before failing with an exception.

This article has been dead for over six months. Start a new discussion instead.