前言
本文通过C#演示了如何使用HslCommunication库实现ModBusTcp客户端,进行读取和写入数据的操作。详细介绍了代码实现,包括连接、断开、读取线圈状态以及写入数据等步骤,并提供了源代码注释解释。
正文
1、建立一个程序,引用HslCommunication.dll和Newtonsoft.Json.dll
2、界面
3、界面代码
4、源代码
using System;
using System.Windows.Forms;
using HslCommunication.ModBus;
using HslCommunication;
namespace ModbusDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 初始化类
/// </summary>
private ModbusTcpNet busTcpClient =null;
/// <summary>
/// 监听状态
/// </summary>
private bool IsEnable = false;
private void Form1_Load(object sender, EventArgs e)
{
txtPort.Text = "502";
txtIp.Text = "172.30.16.220";
textBox2.Text = "00141";
}
/// <summary>
/// 开启服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button5_Click(object sender, EventArgs e)
{
try
{
if (IsEnable)
{
MessageBox.Show("请勿重复建立连接!");
return;
}
string ip = txtIp.Text.Trim();
int port = Convert.ToInt32(txtPort.Text);
if (ip==null || ip=="")
{
MessageBox.Show("ip不能为空!");
return;
}
busTcpClient = new ModbusTcpNet(ip, port, 0x01);
OperateResult res = busTcpClient.ConnectServer();
if (res.IsSuccess==true) //接收状态返回值
{
IsEnable = true;
MessageBox.Show("开启连接成功");
}
else
{
MessageBox.Show("开启连接失败");
}
}
catch (Exception ex)
{
MessageBox.Show("开启连接失败!", ex.Message.ToString());
}
}
private void Button6_Click(object sender, EventArgs e)
{
try
{
if (!IsEnable)
{
MessageBox.Show("尚未建立连接!");
return;
}
busTcpClient.ConnectClose();
IsEnable = false;
MessageBox.Show("关闭连接成功!");
}
catch (Exception ex)
{
MessageBox.Show("关闭连接失败!", ex.Message.ToString());
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void Button3_Click(object sender, EventArgs e)
{
try
{
if (!IsEnable)
{
MessageBox.Show("尚未建立连接!");
return;
}
if (busTcpClient == null)
{
MessageBox.Show("尚未初始化对象!");
return;
}
string txt = textBox2.Text.Trim();
if (txt=="")
{
MessageBox.Show("地址不能为空!");
return;
}
bool coil100 = busTcpClient.ReadCoil(txt).Content; // 读取线圈100的通断
textBox1.Text = "";
MessageBox.Show("监听成功!");
textBox1.Text = coil100 == true ? "true" : "false";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
5、对源码进行加注释和说明
在之前的文章中《modbus调试工具的使用》,并且根据IP进行模拟通信这个方法配置完成后,这是前提。
5.1、打开Modbus Slave,按F8进入这个界面,函数选择线圈,数量选择20,点击ok
5.2、同理也打开Modbus Poll,按F8进入这个界面,函数选择线圈,数量选择20,点击ok
5.3、这时他们可以进行通信了,修改其中一个参数,另一个软件也会进行变化
5.4源代码从上到下,注释说明
6、所有注释说明过的代码
using System;
using System.Windows.Forms;
using HslCommunication.ModBus;
using HslCommunication;
namespace ModbusDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 初始化类
/// </summary>
private ModbusTcpNet busTcpClient = null;
/// <summary>
/// 监听状态
/// </summary>
private bool IsEnable = false;
private void Form1_Load(object sender, EventArgs e)
{
txtPort.Text = "502";//这里是端口号
txtIp.Text = "127.0.0.1";//这里是本机的IP地址,原配置是172.30.16.220,修改成127.0.0.1
//这里的值,先看第二列,00000,第一列是别名,就和sql语句中的as是一样的。
//再看行0,1,2,3,4,5,6,7,8,9
//列值和行值组合在一起,就是我们要的地址。例如00001 00002 00003 00004 以此类推,第一列到00009,第二列到00019。
//所以,这里我们要读谁的值,或者写入谁的值,就写入谁的地址。
textBox2.Text = "00002";
}
/// <summary>
/// 开启服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button5_Click(object sender, EventArgs e)
{
try
{
if (IsEnable)
{
MessageBox.Show("请勿重复建立连接!");
return;
}
string ip = txtIp.Text.Trim();
int port = Convert.ToInt32(txtPort.Text);
if (ip == null || ip == "")
{
MessageBox.Show("ip不能为空!");
return;
}
busTcpClient = new ModbusTcpNet(ip, port, 0x01);
OperateResult res = busTcpClient.ConnectServer();
if (res.IsSuccess == true) //接收状态返回值
{
IsEnable = true;
MessageBox.Show("开启连接成功");
}
else
{
MessageBox.Show("开启连接失败");
}
}
catch (Exception ex)
{
MessageBox.Show("开启连接失败!", ex.Message.ToString());
}
}
private void Button6_Click(object sender, EventArgs e)
{
try
{
if (!IsEnable)
{
MessageBox.Show("尚未建立连接!");
return;
}
busTcpClient.ConnectClose();
IsEnable = false;
MessageBox.Show("关闭连接成功!");
}
catch (Exception ex)
{
MessageBox.Show("关闭连接失败!", ex.Message.ToString());
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void Button3_Click(object sender, EventArgs e)
{
try
{
if (!IsEnable)
{
MessageBox.Show("尚未建立连接!");
return;
}
if (busTcpClient == null)
{
MessageBox.Show("尚未初始化对象!");
return;
}
string txt = textBox2.Text.Trim();
if (txt == "")
{
MessageBox.Show("地址不能为空!");
return;
}
bool coil100 = busTcpClient.ReadCoil(txt).Content; // 这里写谁的地址,读的就是谁的值
textBox1.Text = "";
MessageBox.Show("监听成功!");
textBox1.Text = coil100 == true ? "true" : "false";
// 读取操作,以下txt写谁,读取的就是谁的值
bool coil1001 = busTcpClient.ReadCoil(txt).Content; // 读取线圈100的通断
short short100 = busTcpClient.ReadInt16(txt).Content; // 读取寄存器100的short值
ushort ushort100 = busTcpClient.ReadUInt16(txt).Content; // 读取寄存器100的ushort值
int int100 = busTcpClient.ReadInt32(txt).Content; // 读取寄存器100-101的int值
uint uint100 = busTcpClient.ReadUInt32(txt).Content; // 读取寄存器100-101的uint值
float float100 = busTcpClient.ReadFloat(txt).Content; // 读取寄存器100-101的float值
long long100 = busTcpClient.ReadInt64(txt).Content; // 读取寄存器100-103的long值
ulong ulong100 = busTcpClient.ReadUInt64(txt).Content; // 读取寄存器100-103的ulong值
string str100 = busTcpClient.ReadString(txt, 5).Content;// 读取100到104共10个字符的字符串
// 写入操作,以下txt写谁,就是写入谁的值
busTcpClient.WriteCoil(txt, true);// 写入线圈100为通
busTcpClient.Write(txt, (short)12345);// 写入寄存器100为12345
busTcpClient.Write(txt, (ushort)45678);// 写入寄存器100为45678
busTcpClient.Write(txt, 123456789);// 写入寄存器100-101为123456789
busTcpClient.Write(txt, (uint)123456778);// 写入寄存器100-101为123456778
busTcpClient.Write(txt, 123.456);// 写入寄存器100-101为123.456
busTcpClient.Write(txt, 12312312312414L);//写入寄存器100-103为一个大数据
busTcpClient.Write(txt, 12634534534543656UL);// 写入寄存器100-103为一个大数据
busTcpClient.Write(txt, 123.456d);// 写入寄存器100-103为一个双精度的数据
busTcpClient.Write(txt, "K123456789");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
7、效果。
最后我们进行读取和写入的验证
读取
读取00002地址的值,成功
写入
写入00001的值,还未写入的时候是0
写入后,0变成1
最后
如果你觉得这篇文章对你有帮助,不妨点个赞支持一下!你的支持是我继续分享知识的动力。如果有任何疑问或需要进一步的帮助,欢迎随时留言。
也可以加入微信公众号 [DotNet技术匠] 社区,与其他热爱技术的同行一起交流心得,共同成长!
优秀是一种习惯,欢迎大家留言学习!
作者:故里2130
出处:blog.csdn.net/u012563853/article/details/124491983
声明:网络内容,仅供学习,尊重版权,侵权速删,歉意致谢!