首先我要说明一下,本人新手一枚,本文仅为同样热爱学习的同学提供参考,有不
对的地方还请大家热心指出,本文只起到一个抛砖引玉的作用,希望看到本文的同学可
以从中学习到少许知识,也希望可以跟各位读者成为朋友,多多交流,使学习不再孤单
寂寞。
由于本文太长,顾分为两部分,第二部分连接
初识Modbus TCP/IP-------------C#编写Modbus TCP客户端程序(二)
http://blog.csdn.net/thebestleo/article/details/52331976
废话少说,我们直接上干的,学习知识,第一个是收集和查阅资料,这个是必须的。
1、Modbus官方网站:http://www.modbus.org/
2、Modbus协议规范英文原版:
http://download.csdn.net/download/thebestleo/9609480
3、Modbus协议规范中文版:
http://download.csdn.net/download/thebestleo/9609620
4、Modbus通讯的TCP实现指南:
http://download.csdn.net/download/thebestleo/9609646
5、Modbu TCP服务器测试工具:
http://download.csdn.net/download/thebestleo/9609665
6、Modbu TCP客户端测试工具:
http://download.csdn.net/download/thebestleo/9609676
7、网络数据分析软件Wireshark:
http://download.csdn.net/download/thebestleo/9613131
8、文章中Modbus Slave的设置文件,打包下载一下,便于你的测试
http://download.csdn.net/detail/thebestleo/9614679
9、本文最终所写成的C#的Modbus TCP客户端程序
http://download.csdn.net/download/thebestleo/9614682
下面传一张modbus官网上的一张图片,是一个Modbus TCP的工具包,跟我上面给出的类似
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace Modbus_TCP_Client
{
public partial class Form1 : Form
{
public Socket newclient;
public bool Connected;
public Thread myThread;
public delegate void MyInvoke(string str);
public Form1()
{
InitializeComponent();
}
private void exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void Connect()
{
byte[] data = new byte[1024];
string ipadd = serverIP.Text.Trim();//将服务器IP地址存放在字符串 ipadd中
int port = Convert.ToInt32(serverPort.Text.Trim());//将端口号强制为32位整型,存放在port中
//创建一个套接字
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);
newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//将套接字与远程服务器地址相连
try
{
newclient.Connect(ie);
connect.Enabled = false;//使连接按钮变成虚的,无法点击
Connected = true;
}
catch (SocketException e)
{
MessageBox.Show("连接服务器失败 " + e.Message);
return;
}
ThreadStart myThreaddelegate = new ThreadStart(ReceiveMsg);
myThread = new Thread(myThreaddelegate);
myThread.Start();
tmSend.Enabled = true;//增加定时发送需要将此功能打开
}
private void connect_Click_1(object sender, EventArgs e)
{
Connect();
}
}
}
private void timersend_Tick(object sender, EventArgs e)
{
int isecond = 5000;//以毫秒为单位
timersend.Interval = isecond;//5秒触发一次
byte[] data = new byte[] { 0x00, 0x0f, 0x00, 0x00, 0x00, 0x06, 0x01, 0x04, 0x00, 0x00, 0x00, 0x01 };
newclient.Send(data);
}
private void send01_Click(object sender, EventArgs e)
{
byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x00, 0x14, 0x00, 0x13;
newclient.Send(data);
}
public void ReceiveMsg()
{
while (true)
{
byte[] data = new byte[1024];//定义数据接收数组
newclient.Receive(data);//接收数据到data数组
int length = data[5];//读取数据长度
Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度
for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中
datashow[i] = data[i];
string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
if (data[7] == 0x01) { showMsg01(stringdata + "\r\n"); };
if (data[7] == 0x02) { showMsg02(stringdata + "\r\n"); };
if (data[7] == 0x03) { showMsg03(stringdata + "\r\n"); };
if (data[7] == 0x05) { showMsg05(stringdata + "\r\n"); };
if (data[7] == 0x06) { showMsg06(stringdata + "\r\n"); };
if (data[7] == 0x0F) { showMsg0F(stringdata + "\r\n"); };
if (data[7] == 0x10) { showMsg10(stringdata + "\r\n"); };
}
}
public void showMsg01(string msg)
{
//在线程里以安全方式调用控件
if (receiveMsg01.InvokeRequired)
{
MyInvoke _myinvoke = new MyInvoke(showMsg01);
receiveMsg01.Invoke(_myinvoke, new object[] { msg });
}
else
{
receiveMsg01.AppendText(msg);
}
}
private void send02_Click(object sender, EventArgs e)
{
byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x02, 0x00, 0xC5, 0x00, 0x16 };
newclient.Send(data);
}
public void showMsg02(string msg)
{
//在线程里以安全方式调用控件
if (receive0x01.InvokeRequired)
{
MyInvoke _myinvoke = new MyInvoke(showMsg02);
receive0x02.Invoke(_myinvoke, new object[] { msg });
}
else
{
receive0x02.AppendText(msg);
}
}