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);
}
}