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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
Anonymous
-
2017-08-17
Post awaiting moderation.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)
My code:
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
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?
Yep, your version works. So everything from my side is configured correctly. Only when I want to reproduce the example I get the error.
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;
}
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.
Forgot to mention, you could update the Port and IPAddress by using the Properties in the same way you already did.
It works! Thanks! :)