shay Avital
Member
- Joined
- Oct 9, 2020
- Messages
- 12
- Programming Experience
- Beginner
i am trying to change the slave Id of my energy meter by writing a new value to right register. once the value is changed the connection is obviously lost but i can't find a way to renew it. i cant close the port without closing my entire application, there is no respond to modbusClient.Disconnect(); how can i please recover the connection and continue from there?
C#:
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);
}