Answered recover connection after changing slave ID in easymodbus RTU c#

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);
            }
 
Moving to 3rd Party...
 
Hmm, is ModbusClient IDisposable? Perhaps try wrapping up your client in a using statement. I don't have any experience with ModbusClient, but I'd imagine going to the developers for support might be a better option than a third party forum.
 
Seems logical if you can use a using statement, that the port you are piping with the client would relinquish it's hold on that port when the connection is disposed; providing the client is a disposable object. Have you tried contacting the developers or consulting their documentation?
 
Back
Top Bottom