0

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)
        {
            try
            {
                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;
                    port.Open();
 
                    // 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)
            {
                MessageBox.Show(g.Message);
            }
        }

Edited by Streamx1: n/a

3
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by nick.crane
0

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

Edited by Kimpl: n/a

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.