Menu

Reproducing the Client code

2017-08-17
2017-08-18
  • Ganesh Gebhard

    Ganesh Gebhard - 2017-08-17

    I'm trying to reproduce the client example code, but I get an error everytime I try that. However, the code of your project and my project are exactly the same. Yours is modified in such a way that it only has to connect. So the user clicks a button, the IP and Port are used to connect and a label shows if its connected or not.

    Your code (modified)

    /*
     * Created by SharpDevelop.
     * User: www.rossmann-engineering.de
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace EasyModbusClientExample
    {
        /// <summary>
        /// Description of MainForm.
        /// </summary>
        public partial class MainForm : Form
        {
            private EasyModbus.ModbusClient modbusClient;
    
            public MainForm()
            {
                InitializeComponent();
                modbusClient = new EasyModbus.ModbusClient();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!modbusClient.Connected)
                    {
                        modbusClient.IPAddress = txtIpAddressInput.Text;
                        modbusClient.Port = int.Parse(txtPortInput.Text);
                        modbusClient.Connect();
    
                        label1.Text = "Connected";
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message, "Exception Reading values from Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
    

    My code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private EasyModbus.ModbusClient modbusClient;
    
            public Form1()
            {
                InitializeComponent();
                modbusClient = new EasyModbus.ModbusClient();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                    if (!modbusClient.Connected)
                    {
                        modbusClient.IPAddress = txtIpAddressInput.Text;
                        modbusClient.Port = int.Parse(txtPortInput.Text);
                        modbusClient.Connect();
    
                        label1.Text = "Connected";
                    }
    
            }
        }
    }
    

    The error is: No connection could be made because the target machine actively refused it.
    In: System.Net.Sockets.SocketException occurred
    File 'ModbusClient.cs', line 106.

    Am I missing something?

    I hope you can help me.

     

    Last edit: Ganesh Gebhard 2017-08-17
    • Rossmann Engineering

      Hi, the settings for P-Address and Port of the Modbus Server is correct, and the settings of your Network-Adapter is correct? Yout Firewall allows connection?

      Did you try to connect using my Modbus-Client example?

       
  • Anonymous

    Anonymous - 2017-08-17
    Post awaiting moderation.
  • Ganesh Gebhard

    Ganesh Gebhard - 2017-08-17

    Yep, your version works. So everything from my side is configured correctly. Only when I want to reproduce the example I get the error.

     
    • Rossmann Engineering

      Okay, I think I see. Please Modify:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;

      namespace WindowsFormsApp1
      {
      public partial class Form1 : Form
      {
      private EasyModbus.ModbusClient modbusClient;

          public Form1()
          {
              InitializeComponent();
              modbusClient = new EasyModbus.ModbusClient(txtIpAddressInput.Text, txtPortInput.Text);
          }
      
          private void button1_Click(object sender, EventArgs e)
          {
      
                  if (!modbusClient.Connected)
                  {
      
                      modbusClient.Connect();
      
                      label1.Text = "Connected";
                  }
      
          }
      }
      

      }

      The library supports different Modbus Versions. If you are trying to get the "Connected" Property before you selected to use ModbusTCP (By using the correct constructor or by colling the Connect Method at leasst once) you'll always get TRUE.

       
  • Rossmann Engineering

    Forgot to mention, you could update the Port and IPAddress by using the Properties in the same way you already did.

                if (!modbusClient.Connected)
                {
                    modbusClient.IPAddress = txtIpAddressInput.Text;
                    modbusClient.Port = int.Parse(txtPortInput.Text);
                    modbusClient.Connect();
    
                    label1.Text = "Connected";
                }
    
     
  • Ganesh Gebhard

    Ganesh Gebhard - 2017-08-18

    It works! Thanks! :)

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.