我正在尝试通过将新值写入正确的寄存器来更改能量计的从属 ID。一旦更改了值,连接显然会丢失,但我找不到更新它的方法。我无法在不关闭整个应用程序的情况下关闭端口,没有响应 modbusClient.Disconnect(); 我怎样才能恢复连接并从那里继续?
private void button1_Click(object sender, EventArgs e)
{
try
{
ModbusClient modbusClient = new ModbusClient(ConnectionSetUp.SetValueForCom);
modbusClient.UnitIdentifier = byte.Parse(ConnectionSetUp.SetValueForAdress);
// Not necessary since default baudrate = 9600
modbusClient.Baudrate = int.Parse(ConnectionSetUp.SetValueForBuad);
modbusClient.Parity = System.IO.Ports.Parity.None;
modbusClient.StopBits = System.IO.Ports.StopBits.One;
modbusClient.ConnectionTimeout = int.Parse(ConnectionSetUp.SetValueDelayTime);
modbusClient.Connect();
int Mult = int.Parse(MeterMult.Text);
int Adress = int.Parse(MeterAdress.Text);
int Baud = int.Parse(MeterBaud.Text);
//mult
modbusClient.WriteMultipleRegisters(4001, new int[1] { Mult });
//Adress
modbusClient.WriteMultipleRegisters(4002, new int[1] { Adress });
ConnectionSetUp.SetValueForAdress = string.Format("{0}", Adress);
modbusClient.Disconnect();
modbusClient.Disconnect();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "bad settings",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}