窗体设计器生成的代码
namespace ModbusTcp_Server
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.button3 = new System.Windows.Forms.Button();
this.holValue = new System.Windows.Forms.NumericUpDown();
this.coliValue = new System.Windows.Forms.NumericUpDown();
this.holAddress = new System.Windows.Forms.NumericUpDown();
this.coliAddress = new System.Windows.Forms.NumericUpDown();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.holValue)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.coliValue)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.holAddress)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.coliAddress)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.richTextBox1);
this.groupBox1.Location = new System.Drawing.Point(434, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(363, 436);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(6, 20);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(351, 410);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Controls.Add(this.textBox2);
this.groupBox2.Controls.Add(this.textBox1);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(12, 12);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(416, 138);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "groupBox2";
//
// button2
//
this.button2.Location = new System.Drawing.Point(201, 77);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "停止";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(53, 77);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "启动";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(261, 29);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(115, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "6000";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(53, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(115, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "172.17.1.153";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(214, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 1;
this.label2.Text = "Port";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(17, 12);
this.label1.TabIndex = 0;
this.label1.Text = "IP";
//
// groupBox3
//
this.groupBox3.Location = new System.Drawing.Point(12, 156);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(416, 88);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "groupBox3";
//
// groupBox4
//
this.groupBox4.Controls.Add(this.button3);
this.groupBox4.Controls.Add(this.holValue);
this.groupBox4.Controls.Add(this.coliValue);
this.groupBox4.Controls.Add(this.holAddress);
this.groupBox4.Controls.Add(this.coliAddress);
this.groupBox4.Controls.Add(this.label6);
this.groupBox4.Controls.Add(this.label5);
this.groupBox4.Controls.Add(this.label4);
this.groupBox4.Controls.Add(this.label3);
this.groupBox4.Location = new System.Drawing.Point(12, 250);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(416, 198);
this.groupBox4.TabIndex = 3;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "groupBox4";
//
// button3
//
this.button3.Location = new System.Drawing.Point(270, 169);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 9;
this.button3.Text = "写入";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// holValue
//
this.holValue.Location = new System.Drawing.Point(270, 129);
this.holValue.Maximum = new decimal(new int[] {
999,
0,
0,
0});
this.holValue.Name = "holValue";
this.holValue.Size = new System.Drawing.Size(120, 21);
this.holValue.TabIndex = 8;
this.holValue.Value = new decimal(new int[] {
100,
0,
0,
0});
//
// coliValue
//
this.coliValue.Location = new System.Drawing.Point(270, 81);
this.coliValue.Name = "coliValue";
this.coliValue.Size = new System.Drawing.Size(120, 21);
this.coliValue.TabIndex = 7;
this.coliValue.Value = new decimal(new int[] {
1,
0,
0,
0});
//
// holAddress
//
this.holAddress.Location = new System.Drawing.Point(97, 129);
this.holAddress.Name = "holAddress";
this.holAddress.Size = new System.Drawing.Size(120, 21);
this.holAddress.TabIndex = 6;
this.holAddress.Value = new decimal(new int[] {
1,
0,
0,
0});
//
// coliAddress
//
this.coliAddress.Location = new System.Drawing.Point(97, 81);
this.coliAddress.Name = "coliAddress";
this.coliAddress.Size = new System.Drawing.Size(120, 21);
this.coliAddress.TabIndex = 5;
this.coliAddress.Value = new decimal(new int[] {
3,
0,
0,
0});
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(51, 131);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(41, 12);
this.label6.TabIndex = 4;
this.label6.Text = "寄存器";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(51, 83);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(29, 12);
this.label5.TabIndex = 3;
this.label5.Text = "线圈";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(268, 32);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(17, 12);
this.label4.TabIndex = 2;
this.label4.Text = "值";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(51, 32);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 1;
this.label3.Text = "地址";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 487);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Server";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.holValue)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.coliValue)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.holAddress)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.coliAddress)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.NumericUpDown holValue;
private System.Windows.Forms.NumericUpDown coliValue;
private System.Windows.Forms.NumericUpDown holAddress;
private System.Windows.Forms.NumericUpDown coliAddress;
private System.Windows.Forms.Button button3;
}
}
后台代码
using Modbus.Data;
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ModbusTcp_Server
{
public partial class Form1 : Form
{
private TcpListener listener;
private ModbusSlave slave;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
listener = new TcpListener(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
listener.Start();
slave = ModbusTcpSlave.CreateTcp(1, listener);
slave.Transport.Retries = 0;
//slave.Transport.ReadTimeout = 3000;
//slave.Transport.WriteTimeout= 3000;
//创建寄存器存储对象
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.DataStore.DataStoreWrittenTo += DataStore_DataStoreWrittenTo;
slave.ModbusSlaveRequestReceived += Slave_ModbusSlaveRequestReceived;
slave.WriteComplete += Slave_WriteComplete;
slave.Listen();
button1.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Slave_WriteComplete(object sender, ModbusSlaveRequestEventArgs e)
{
if (richTextBox1.InvokeRequired)
{
this.BeginInvoke(new Action(delegate
{
richTextBox1.AppendText("[写入数据完成:]\r\n" + Newtonsoft.Json.JsonConvert.SerializeObject(e.Message) + "\r\n ******** \r\n \r\n");
}));
}
}
private void Slave_ModbusSlaveRequestReceived(object sender, ModbusSlaveRequestEventArgs e)
{
if (richTextBox1.InvokeRequired)
{
this.BeginInvoke(new Action(delegate
{
richTextBox1.AppendText("[收到数据:]\r\n" + Newtonsoft.Json.JsonConvert.SerializeObject(e.Message) + "\r\n ******** \r\n \r\n");
}));
}
}
private void DataStore_DataStoreWrittenTo(object sender, DataStoreEventArgs e)
{
switch (e.ModbusDataType)
{
//线圈
case ModbusDataType.Coil: //code 5
ModbusDataCollection<bool> discretes = slave.DataStore.CoilDiscretes;
if (richTextBox1.InvokeRequired)
{
this.BeginInvoke(new Action(delegate
{
richTextBox1.AppendText("[ModbusDataType.Coil]\r\n" + Newtonsoft.Json.JsonConvert.SerializeObject(discretes) + "\r\n ******** \r\n \r\n");
}));
}
break;
//寄存器
case ModbusDataType.HoldingRegister: //code 15
ModbusDataCollection<ushort> holdingRegisters = slave.DataStore.HoldingRegisters;
if (richTextBox1.InvokeRequired)
{
this.BeginInvoke(new Action(delegate
{
richTextBox1.AppendText("[ModbusDataType.HoldingRegister]\r\n" + Newtonsoft.Json.JsonConvert.SerializeObject(holdingRegisters) + "\r\n ******** \r\n \r\n");
}));
}
break;
default:
if (richTextBox1.InvokeRequired)
{
this.BeginInvoke(new Action(delegate
{
richTextBox1.AppendText($"[{e.ModbusDataType}]\r\n" + Newtonsoft.Json.JsonConvert.SerializeObject(e.Data) + "\r\n ******** \r\n \r\n");
}));
}
break;
}
}
/// <summary>
/// 断开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
slave.Dispose();
button1.Enabled = true;
}
/// <summary>
/// 写数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
//CoilDiscretes表示一个Bit,也就是一个bool类型
slave.DataStore.CoilDiscretes[(int)coliAddress.Value] = coliValue.Value == 1 ? true : false;
//HoldingRegisters表示一个无符号的16位整数(2的16次幂:0-65535)
slave.DataStore.HoldingRegisters[(int)holAddress.Value] = (ushort)holValue.Value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}