一、 Modbus Poll客户端工具安装
1.安装Modbus Poll客户端工具
2.Modbus Poll客户端工具安装好以后的界面
二、Modbus Poll客户端工具自带使用说明
Overview
Modbus Poll uses a multiple windows user interface. That means you can open several windows showing different data areas or data from different slave ID's at the same time. You can write any text in the Alias cells.
In any dialog box you can press the F1 key for more help on that specific topic.
This picture shows two open windows one reading 10 holding registers from ID 1, address 0 and one reading 10 holding registers from ID 2.
If your slave device allows you to change a Holding register then
youdouble click the cell or just start typing a new value in the
cell.Then an edit dialog box is shown.
Change the read/write definition
To change the read/write definition of a window you can press F8 or select "read/write definition" from the Setup menu.
Here you define which data to show in the window. This setup shows how toread 10 Holding Registers from address 0. Address 40001 in someprotocol descriptions. Note that Modbus Poll uses Modbus addresseswhich always counts from 0.
How to make a connection
There is no data to display if you have not made a connection. To do so press F3 or select connect from the connection menu. For more detailed help press F1.
This connection use Modbus TCP/IP.
5 different connection types are available however only 2 of them are standard Modbus connections:
•Serial port
•Modbus TCP/IP
For serial connection you may need an USB to RS485, or an RS232 to RS485 converter.
三、 创建自己的Modbus TCP服务端程序
1、我在自己的程序中做了个ModbusTCP服务器,在服务器开启之后,并且ModbusTCP客户端与服务端通讯成功之后,向ModbusTCP客户端写数据。ModbusPull作为客户端。
服务端程序:
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;
using System.Net.Sockets;
using Modbus.Device;
using Modbus.Data;
namespace ModbusTCPServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定义公共变量
public static bool isModBusServerEnable = true;
public static ModbusSlave modbusTcpSlave = null;
public static TcpListener tcpListener;
public static bool slaveCon = false;
public static DataStore dataStore;
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 1; i < 1000000; i++)
{
SetValueToModBusTcpServer(i, i);//第一个参数为写入的地址,从1开始,第二个为写入的数值
}
bool valueBool = true;
SetValueToModBusTcpServer(1, valueBool);//给地址的第1位写入bool值1
}
private void Form1_Load(object sender, EventArgs e)
{
//创建并开启ModbusTcp服务器
try
{
//dataStore = DataStoreFactory.CreateDefaultDataStore();//初始化服务数据区
tcpListener = new TcpListener(System.Net.IPAddress.Parse("本地电脑IP"), 502);
modbusTcpSlave = ModbusTcpSlave.CreateTcp(1, tcpListener);
//modbusTcpSlave.DataStore = dataStore;
modbusTcpSlave.Listen();
slaveCon = true;
}
catch (Exception)
{
slaveCon = false;
}
}
/// <summary>
/// 向ModBusTcp服务写入模拟量
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
private void SetValueToModBusTcpServer(int index, float value)
{
byte[] buffer = BitConverter.GetBytes(value);
ushort highValue = BitConverter.ToUInt16(buffer, 0);
ushort lowValue = BitConverter.ToUInt16(buffer, 2);
ModbusDataCollection<ushort> data = modbusTcpSlave.DataStore.InputRegisters;
data[index] = lowValue;
data[index + 1] = highValue;
}
/// <summary>
/// 向ModBusTcp服务写入数字量
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
private void SetValueToModBusTcpServer(int index, bool value)
{
ModbusDataCollection<bool> data = modbusTcpSlave.DataStore.InputDiscretes;
lock (modbusTcpSlave.DataStore.SyncRoot)
{
data[index] = value;
}
}
}
}
四、Modbus TCP服务端(自建)与Modbus Poll客户端工具进行Modbus TCP通讯
1、客户端配置
(1)选择通讯方式
(2)选择数据传输寄存器类型
(3)运行Modbus TCP自己创建的服务端程序之后,与Modbus Poll客户端工具建立通信联系,读到的数据如下:
通信不成功Modbus Poll客户端工具界面会有红色字体出现。
获取数据区域(部分字节/双字节)
读到数据,没有红色字体,说明通信成功!
总结
这里Modbus TCP通讯客户端使用了自带的客户端工具Modbus Poll,服务端程序是自己写的。后续也可以自己写一个Modbus TCP通讯的客户端程序。