modbus slave

main
syneffort 3 years ago
parent 39750266a2
commit 352e655814
  1. 87
      ModbusStudy/ModbusSlave/Dialog/InputValueDialog.Designer.cs
  2. 31
      ModbusStudy/ModbusSlave/Dialog/InputValueDialog.cs
  3. 120
      ModbusStudy/ModbusSlave/Dialog/InputValueDialog.resx
  4. 147
      ModbusStudy/ModbusSlave/MainForm.Designer.cs
  5. 189
      ModbusStudy/ModbusSlave/MainForm.cs
  6. 9
      ModbusStudy/ModbusSlave/ModbusSlave.csproj

@ -0,0 +1,87 @@

namespace ModbusSlave.Dialog
{
partial class InputValueDialog
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.tbValue = new System.Windows.Forms.TextBox();
this.btnOk = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 20);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(37, 12);
this.label1.TabIndex = 0;
this.label1.Text = "Value";
//
// tbValue
//
this.tbValue.Location = new System.Drawing.Point(56, 16);
this.tbValue.Name = "tbValue";
this.tbValue.Size = new System.Drawing.Size(118, 21);
this.tbValue.TabIndex = 1;
//
// btnOk
//
this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOk.Location = new System.Drawing.Point(184, 14);
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size(75, 23);
this.btnOk.TabIndex = 2;
this.btnOk.Text = "OK";
this.btnOk.UseVisualStyleBackColor = true;
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
//
// InputValueDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(272, 53);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.tbValue);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "InputValueDialog";
this.Text = "Value";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox tbValue;
private System.Windows.Forms.Button btnOk;
}
}

@ -0,0 +1,31 @@
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 ModbusSlave.Dialog
{
public partial class InputValueDialog : Form
{
public string Value
{
get { return tbValue.Text; }
set { tbValue.Text = value; }
}
public InputValueDialog()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
this.Value = tbValue.Text;
}
}
}

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

@ -42,7 +42,7 @@ namespace ModbusSlave
this.chkAutoScroll = new System.Windows.Forms.CheckBox(); this.chkAutoScroll = new System.Windows.Forms.CheckBox();
this.btnClear = new System.Windows.Forms.Button(); this.btnClear = new System.Windows.Forms.Button();
this.rtbLog = new System.Windows.Forms.RichTextBox(); this.rtbLog = new System.Windows.Forms.RichTextBox();
this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabLv = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage(); this.tabPage1 = new System.Windows.Forms.TabPage();
this.lv0x = new System.Windows.Forms.ListView(); this.lv0x = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
@ -63,7 +63,7 @@ namespace ModbusSlave
((System.ComponentModel.ISupportInitialize)(this.nudAddress)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nudAddress)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudPort)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nudPort)).BeginInit();
this.groupBox2.SuspendLayout(); this.groupBox2.SuspendLayout();
this.tabControl1.SuspendLayout(); this.tabLv.SuspendLayout();
this.tabPage1.SuspendLayout(); this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout(); this.tabPage2.SuspendLayout();
this.tabPage3.SuspendLayout(); this.tabPage3.SuspendLayout();
@ -80,9 +80,11 @@ namespace ModbusSlave
this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.tbStatus); this.groupBox1.Controls.Add(this.tbStatus);
this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Location = new System.Drawing.Point(12, 15);
this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 131); this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox1.Size = new System.Drawing.Size(250, 164);
this.groupBox1.TabIndex = 0; this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Server"; this.groupBox1.Text = "Server";
@ -90,9 +92,10 @@ namespace ModbusSlave
// btnStop // btnStop
// //
this.btnStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnStop.Location = new System.Drawing.Point(162, 99); this.btnStop.Location = new System.Drawing.Point(162, 124);
this.btnStop.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btnStop.Name = "btnStop"; this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 23); this.btnStop.Size = new System.Drawing.Size(75, 29);
this.btnStop.TabIndex = 3; this.btnStop.TabIndex = 3;
this.btnStop.Text = "Stop"; this.btnStop.Text = "Stop";
this.btnStop.UseVisualStyleBackColor = true; this.btnStop.UseVisualStyleBackColor = true;
@ -101,9 +104,10 @@ namespace ModbusSlave
// btnRun // btnRun
// //
this.btnRun.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnRun.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnRun.Location = new System.Drawing.Point(81, 99); this.btnRun.Location = new System.Drawing.Point(81, 124);
this.btnRun.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btnRun.Name = "btnRun"; this.btnRun.Name = "btnRun";
this.btnRun.Size = new System.Drawing.Size(75, 23); this.btnRun.Size = new System.Drawing.Size(75, 29);
this.btnRun.TabIndex = 3; this.btnRun.TabIndex = 3;
this.btnRun.Text = "Run"; this.btnRun.Text = "Run";
this.btnRun.UseVisualStyleBackColor = true; this.btnRun.UseVisualStyleBackColor = true;
@ -111,31 +115,38 @@ namespace ModbusSlave
// //
// nudAddress // nudAddress
// //
this.nudAddress.Location = new System.Drawing.Point(81, 72); this.nudAddress.Location = new System.Drawing.Point(81, 90);
this.nudAddress.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.nudAddress.Name = "nudAddress"; this.nudAddress.Name = "nudAddress";
this.nudAddress.Size = new System.Drawing.Size(156, 21); this.nudAddress.Size = new System.Drawing.Size(156, 23);
this.nudAddress.TabIndex = 2; this.nudAddress.TabIndex = 2;
this.nudAddress.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; this.nudAddress.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.nudAddress.Value = new decimal(new int[] {
1,
0,
0,
0});
// //
// label3 // label3
// //
this.label3.AutoSize = true; this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(7, 76); this.label3.Location = new System.Drawing.Point(7, 95);
this.label3.Name = "label3"; this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(52, 12); this.label3.Size = new System.Drawing.Size(49, 15);
this.label3.TabIndex = 0; this.label3.TabIndex = 0;
this.label3.Text = "Address"; this.label3.Text = "Address";
// //
// nudPort // nudPort
// //
this.nudPort.Location = new System.Drawing.Point(81, 45); this.nudPort.Location = new System.Drawing.Point(81, 56);
this.nudPort.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.nudPort.Maximum = new decimal(new int[] { this.nudPort.Maximum = new decimal(new int[] {
65535, 65535,
0, 0,
0, 0,
0}); 0});
this.nudPort.Name = "nudPort"; this.nudPort.Name = "nudPort";
this.nudPort.Size = new System.Drawing.Size(156, 21); this.nudPort.Size = new System.Drawing.Size(156, 23);
this.nudPort.TabIndex = 2; this.nudPort.TabIndex = 2;
this.nudPort.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; this.nudPort.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.nudPort.Value = new decimal(new int[] { this.nudPort.Value = new decimal(new int[] {
@ -147,9 +158,9 @@ namespace ModbusSlave
// label2 // label2
// //
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(7, 49); this.label2.Location = new System.Drawing.Point(7, 61);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(55, 12); this.label2.Size = new System.Drawing.Size(54, 15);
this.label2.TabIndex = 0; this.label2.TabIndex = 0;
this.label2.Text = "TCP port"; this.label2.Text = "TCP port";
// //
@ -157,10 +168,11 @@ namespace ModbusSlave
// //
this.tbStatus.BackColor = System.Drawing.Color.Silver; this.tbStatus.BackColor = System.Drawing.Color.Silver;
this.tbStatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.tbStatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbStatus.Location = new System.Drawing.Point(81, 18); this.tbStatus.Location = new System.Drawing.Point(81, 22);
this.tbStatus.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tbStatus.Name = "tbStatus"; this.tbStatus.Name = "tbStatus";
this.tbStatus.ReadOnly = true; this.tbStatus.ReadOnly = true;
this.tbStatus.Size = new System.Drawing.Size(156, 21); this.tbStatus.Size = new System.Drawing.Size(156, 23);
this.tbStatus.TabIndex = 1; this.tbStatus.TabIndex = 1;
this.tbStatus.Text = "Stop"; this.tbStatus.Text = "Stop";
this.tbStatus.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.tbStatus.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
@ -168,9 +180,9 @@ namespace ModbusSlave
// label1 // label1
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 22); this.label1.Location = new System.Drawing.Point(7, 28);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 12); this.label1.Size = new System.Drawing.Size(40, 15);
this.label1.TabIndex = 0; this.label1.TabIndex = 0;
this.label1.Text = "Status"; this.label1.Text = "Status";
// //
@ -179,9 +191,11 @@ namespace ModbusSlave
this.groupBox2.Controls.Add(this.chkAutoScroll); this.groupBox2.Controls.Add(this.chkAutoScroll);
this.groupBox2.Controls.Add(this.btnClear); this.groupBox2.Controls.Add(this.btnClear);
this.groupBox2.Controls.Add(this.rtbLog); this.groupBox2.Controls.Add(this.rtbLog);
this.groupBox2.Location = new System.Drawing.Point(12, 149); this.groupBox2.Location = new System.Drawing.Point(12, 186);
this.groupBox2.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox2.Name = "groupBox2"; this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(250, 225); this.groupBox2.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox2.Size = new System.Drawing.Size(250, 281);
this.groupBox2.TabIndex = 0; this.groupBox2.TabIndex = 0;
this.groupBox2.TabStop = false; this.groupBox2.TabStop = false;
this.groupBox2.Text = "Log"; this.groupBox2.Text = "Log";
@ -191,9 +205,10 @@ namespace ModbusSlave
this.chkAutoScroll.AutoSize = true; this.chkAutoScroll.AutoSize = true;
this.chkAutoScroll.Checked = true; this.chkAutoScroll.Checked = true;
this.chkAutoScroll.CheckState = System.Windows.Forms.CheckState.Checked; this.chkAutoScroll.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkAutoScroll.Location = new System.Drawing.Point(9, 203); this.chkAutoScroll.Location = new System.Drawing.Point(9, 254);
this.chkAutoScroll.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.chkAutoScroll.Name = "chkAutoScroll"; this.chkAutoScroll.Name = "chkAutoScroll";
this.chkAutoScroll.Size = new System.Drawing.Size(84, 16); this.chkAutoScroll.Size = new System.Drawing.Size(84, 19);
this.chkAutoScroll.TabIndex = 1; this.chkAutoScroll.TabIndex = 1;
this.chkAutoScroll.Text = "Auto scroll"; this.chkAutoScroll.Text = "Auto scroll";
this.chkAutoScroll.UseVisualStyleBackColor = true; this.chkAutoScroll.UseVisualStyleBackColor = true;
@ -201,9 +216,10 @@ namespace ModbusSlave
// btnClear // btnClear
// //
this.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnClear.Location = new System.Drawing.Point(162, 196); this.btnClear.Location = new System.Drawing.Point(162, 245);
this.btnClear.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btnClear.Name = "btnClear"; this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 23); this.btnClear.Size = new System.Drawing.Size(75, 29);
this.btnClear.TabIndex = 3; this.btnClear.TabIndex = 3;
this.btnClear.Text = "Clear log"; this.btnClear.Text = "Clear log";
this.btnClear.UseVisualStyleBackColor = true; this.btnClear.UseVisualStyleBackColor = true;
@ -214,32 +230,36 @@ namespace ModbusSlave
this.rtbLog.BackColor = System.Drawing.Color.Black; this.rtbLog.BackColor = System.Drawing.Color.Black;
this.rtbLog.BorderStyle = System.Windows.Forms.BorderStyle.None; this.rtbLog.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.rtbLog.ForeColor = System.Drawing.Color.Lime; this.rtbLog.ForeColor = System.Drawing.Color.Lime;
this.rtbLog.Location = new System.Drawing.Point(9, 21); this.rtbLog.Location = new System.Drawing.Point(9, 26);
this.rtbLog.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.rtbLog.Name = "rtbLog"; this.rtbLog.Name = "rtbLog";
this.rtbLog.ReadOnly = true; this.rtbLog.ReadOnly = true;
this.rtbLog.Size = new System.Drawing.Size(228, 169); this.rtbLog.Size = new System.Drawing.Size(228, 211);
this.rtbLog.TabIndex = 0; this.rtbLog.TabIndex = 0;
this.rtbLog.TabStop = false;
this.rtbLog.Text = ""; this.rtbLog.Text = "";
// //
// tabControl1 // tabLv
// //
this.tabControl1.Controls.Add(this.tabPage1); this.tabLv.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2); this.tabLv.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3); this.tabLv.Controls.Add(this.tabPage3);
this.tabControl1.Controls.Add(this.tabPage4); this.tabLv.Controls.Add(this.tabPage4);
this.tabControl1.Location = new System.Drawing.Point(268, 12); this.tabLv.Location = new System.Drawing.Point(268, 15);
this.tabControl1.Name = "tabControl1"; this.tabLv.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabControl1.SelectedIndex = 0; this.tabLv.Name = "tabLv";
this.tabControl1.Size = new System.Drawing.Size(263, 362); this.tabLv.SelectedIndex = 0;
this.tabControl1.TabIndex = 1; this.tabLv.Size = new System.Drawing.Size(263, 452);
this.tabLv.TabIndex = 1;
// //
// tabPage1 // tabPage1
// //
this.tabPage1.Controls.Add(this.lv0x); this.tabPage1.Controls.Add(this.lv0x);
this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage1.Name = "tabPage1"; this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3); this.tabPage1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage1.Size = new System.Drawing.Size(255, 336); this.tabPage1.Size = new System.Drawing.Size(255, 426);
this.tabPage1.TabIndex = 0; this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "0x"; this.tabPage1.Text = "0x";
this.tabPage1.UseVisualStyleBackColor = true; this.tabPage1.UseVisualStyleBackColor = true;
@ -252,10 +272,11 @@ namespace ModbusSlave
this.lv0x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv0x.Dock = System.Windows.Forms.DockStyle.Fill;
this.lv0x.FullRowSelect = true; this.lv0x.FullRowSelect = true;
this.lv0x.HideSelection = false; this.lv0x.HideSelection = false;
this.lv0x.Location = new System.Drawing.Point(3, 3); this.lv0x.Location = new System.Drawing.Point(3, 4);
this.lv0x.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.lv0x.MultiSelect = false; this.lv0x.MultiSelect = false;
this.lv0x.Name = "lv0x"; this.lv0x.Name = "lv0x";
this.lv0x.Size = new System.Drawing.Size(249, 330); this.lv0x.Size = new System.Drawing.Size(249, 418);
this.lv0x.TabIndex = 0; this.lv0x.TabIndex = 0;
this.lv0x.UseCompatibleStateImageBehavior = false; this.lv0x.UseCompatibleStateImageBehavior = false;
this.lv0x.View = System.Windows.Forms.View.Details; this.lv0x.View = System.Windows.Forms.View.Details;
@ -274,9 +295,10 @@ namespace ModbusSlave
// //
this.tabPage2.Controls.Add(this.lv1x); this.tabPage2.Controls.Add(this.lv1x);
this.tabPage2.Location = new System.Drawing.Point(4, 22); this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage2.Name = "tabPage2"; this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3); this.tabPage2.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage2.Size = new System.Drawing.Size(255, 336); this.tabPage2.Size = new System.Drawing.Size(255, 426);
this.tabPage2.TabIndex = 1; this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "1x"; this.tabPage2.Text = "1x";
this.tabPage2.UseVisualStyleBackColor = true; this.tabPage2.UseVisualStyleBackColor = true;
@ -289,10 +311,11 @@ namespace ModbusSlave
this.lv1x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv1x.Dock = System.Windows.Forms.DockStyle.Fill;
this.lv1x.FullRowSelect = true; this.lv1x.FullRowSelect = true;
this.lv1x.HideSelection = false; this.lv1x.HideSelection = false;
this.lv1x.Location = new System.Drawing.Point(3, 3); this.lv1x.Location = new System.Drawing.Point(3, 4);
this.lv1x.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.lv1x.MultiSelect = false; this.lv1x.MultiSelect = false;
this.lv1x.Name = "lv1x"; this.lv1x.Name = "lv1x";
this.lv1x.Size = new System.Drawing.Size(249, 330); this.lv1x.Size = new System.Drawing.Size(249, 418);
this.lv1x.TabIndex = 1; this.lv1x.TabIndex = 1;
this.lv1x.UseCompatibleStateImageBehavior = false; this.lv1x.UseCompatibleStateImageBehavior = false;
this.lv1x.View = System.Windows.Forms.View.Details; this.lv1x.View = System.Windows.Forms.View.Details;
@ -311,8 +334,10 @@ namespace ModbusSlave
// //
this.tabPage3.Controls.Add(this.lv3x); this.tabPage3.Controls.Add(this.lv3x);
this.tabPage3.Location = new System.Drawing.Point(4, 22); this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage3.Name = "tabPage3"; this.tabPage3.Name = "tabPage3";
this.tabPage3.Size = new System.Drawing.Size(255, 336); this.tabPage3.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage3.Size = new System.Drawing.Size(255, 426);
this.tabPage3.TabIndex = 2; this.tabPage3.TabIndex = 2;
this.tabPage3.Text = "3x"; this.tabPage3.Text = "3x";
this.tabPage3.UseVisualStyleBackColor = true; this.tabPage3.UseVisualStyleBackColor = true;
@ -325,10 +350,11 @@ namespace ModbusSlave
this.lv3x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv3x.Dock = System.Windows.Forms.DockStyle.Fill;
this.lv3x.FullRowSelect = true; this.lv3x.FullRowSelect = true;
this.lv3x.HideSelection = false; this.lv3x.HideSelection = false;
this.lv3x.Location = new System.Drawing.Point(0, 0); this.lv3x.Location = new System.Drawing.Point(3, 4);
this.lv3x.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.lv3x.MultiSelect = false; this.lv3x.MultiSelect = false;
this.lv3x.Name = "lv3x"; this.lv3x.Name = "lv3x";
this.lv3x.Size = new System.Drawing.Size(255, 336); this.lv3x.Size = new System.Drawing.Size(249, 418);
this.lv3x.TabIndex = 1; this.lv3x.TabIndex = 1;
this.lv3x.UseCompatibleStateImageBehavior = false; this.lv3x.UseCompatibleStateImageBehavior = false;
this.lv3x.View = System.Windows.Forms.View.Details; this.lv3x.View = System.Windows.Forms.View.Details;
@ -346,9 +372,11 @@ namespace ModbusSlave
// tabPage4 // tabPage4
// //
this.tabPage4.Controls.Add(this.lv4x); this.tabPage4.Controls.Add(this.lv4x);
this.tabPage4.Location = new System.Drawing.Point(4, 22); this.tabPage4.Location = new System.Drawing.Point(4, 24);
this.tabPage4.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage4.Name = "tabPage4"; this.tabPage4.Name = "tabPage4";
this.tabPage4.Size = new System.Drawing.Size(255, 336); this.tabPage4.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.tabPage4.Size = new System.Drawing.Size(255, 424);
this.tabPage4.TabIndex = 3; this.tabPage4.TabIndex = 3;
this.tabPage4.Text = "4x"; this.tabPage4.Text = "4x";
this.tabPage4.UseVisualStyleBackColor = true; this.tabPage4.UseVisualStyleBackColor = true;
@ -361,10 +389,11 @@ namespace ModbusSlave
this.lv4x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv4x.Dock = System.Windows.Forms.DockStyle.Fill;
this.lv4x.FullRowSelect = true; this.lv4x.FullRowSelect = true;
this.lv4x.HideSelection = false; this.lv4x.HideSelection = false;
this.lv4x.Location = new System.Drawing.Point(0, 0); this.lv4x.Location = new System.Drawing.Point(3, 4);
this.lv4x.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.lv4x.MultiSelect = false; this.lv4x.MultiSelect = false;
this.lv4x.Name = "lv4x"; this.lv4x.Name = "lv4x";
this.lv4x.Size = new System.Drawing.Size(255, 336); this.lv4x.Size = new System.Drawing.Size(249, 416);
this.lv4x.TabIndex = 1; this.lv4x.TabIndex = 1;
this.lv4x.UseCompatibleStateImageBehavior = false; this.lv4x.UseCompatibleStateImageBehavior = false;
this.lv4x.View = System.Windows.Forms.View.Details; this.lv4x.View = System.Windows.Forms.View.Details;
@ -381,14 +410,16 @@ namespace ModbusSlave
// //
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White; this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(536, 386); this.ClientSize = new System.Drawing.Size(536, 482);
this.Controls.Add(this.tabControl1); this.Controls.Add(this.tabLv);
this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1); this.Controls.Add(this.groupBox1);
this.Font = new System.Drawing.Font("맑은 고딕", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "MainForm"; this.Name = "MainForm";
this.Text = "Modbus Slave"; this.Text = "Modbus Slave";
this.groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
@ -397,7 +428,7 @@ namespace ModbusSlave
((System.ComponentModel.ISupportInitialize)(this.nudPort)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.nudPort)).EndInit();
this.groupBox2.ResumeLayout(false); this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout(); this.groupBox2.PerformLayout();
this.tabControl1.ResumeLayout(false); this.tabLv.ResumeLayout(false);
this.tabPage1.ResumeLayout(false); this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false); this.tabPage2.ResumeLayout(false);
this.tabPage3.ResumeLayout(false); this.tabPage3.ResumeLayout(false);
@ -421,7 +452,7 @@ namespace ModbusSlave
private System.Windows.Forms.CheckBox chkAutoScroll; private System.Windows.Forms.CheckBox chkAutoScroll;
private System.Windows.Forms.Button btnClear; private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.RichTextBox rtbLog; private System.Windows.Forms.RichTextBox rtbLog;
private System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabControl tabLv;
private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.ListView lv0x; private System.Windows.Forms.ListView lv0x;
private System.Windows.Forms.ColumnHeader columnHeader1; private System.Windows.Forms.ColumnHeader columnHeader1;

@ -9,26 +9,25 @@ using System.Net.NetworkInformation;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Timers;
using ModbusSlave.Dialog;
namespace ModbusSlave namespace ModbusSlave
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
private readonly int DATA_COUNT = 20;
private ModbusServer server;
private System.Timers.Timer updateTimer;
private bool isFirstLog = true;
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
InitInstance(); InitInstance();
} }
#region Field
private ModbusServer server;
private bool isFirstLog = true;
#endregion
#region Button event
private void btnRun_Click(object sender, EventArgs e) private void btnRun_Click(object sender, EventArgs e)
{ {
try try
@ -51,6 +50,8 @@ namespace ModbusSlave
tbStatus.Text = "Run"; tbStatus.Text = "Run";
tbStatus.BackColor = Color.Lime; tbStatus.BackColor = Color.Lime;
updateTimer.Start();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -71,6 +72,8 @@ namespace ModbusSlave
tbStatus.Text = "Stop"; tbStatus.Text = "Stop";
tbStatus.BackColor = Color.Silver; tbStatus.BackColor = Color.Silver;
updateTimer.Stop();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -83,25 +86,74 @@ namespace ModbusSlave
ClearLog(); ClearLog();
} }
#endregion private void Lv_MouseDoubleClick(object sender, MouseEventArgs e)
{
#region Timer event if (btnRun.Enabled)
return;
#endregion
#region Listview event ListView lv = sender as ListView;
if (lv.SelectedItems.Count < 1)
return;
#endregion int idx = lv.SelectedItems[0].Index;
short tmp = 0;
InputValueDialog dlg = new InputValueDialog();
dlg.Value = lv.SelectedItems[0].SubItems[1].Text;
dlg.StartPosition = FormStartPosition.CenterParent;
switch (lv.Name)
{
case "lv0x":
server.coils[idx + 1] = !server.coils[idx + 1];
break;
case "lv1x":
server.discreteInputs[idx + 1] = !server.discreteInputs[idx + 1];
break;
case "lv3x":
if (dlg.ShowDialog() != DialogResult.OK)
return;
if (!short.TryParse(dlg.Value, out tmp))
{
Log($"[ERROR] Value should be a number and in range of {short.MinValue} to {short.MaxValue}");
return;
}
server.inputRegisters[idx + 1] = tmp;
break;
case "lv4x":
if (dlg.ShowDialog() != DialogResult.OK)
return;
if (!short.TryParse(dlg.Value, out tmp))
{
Log($"[ERROR] Value should be a number and in range of {short.MinValue} to {short.MaxValue}");
return;
}
server.holdingRegisters[idx + 1] = tmp;
break;
}
}
#region Etc event private void InitInstance()
{
server = new ModbusServer();
updateTimer = new System.Timers.Timer();
updateTimer.Interval = 1000;
updateTimer.Elapsed += UpdateTimer_Elapsed;
#endregion lv0x.MouseDoubleClick += Lv_MouseDoubleClick;
lv1x.MouseDoubleClick += Lv_MouseDoubleClick;
lv3x.MouseDoubleClick += Lv_MouseDoubleClick;
lv4x.MouseDoubleClick += Lv_MouseDoubleClick;
#region Method InitListView();
}
private void InitInstance() private void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
{ {
server = new ModbusServer(); UpdateListView(0);
UpdateListView(1);
UpdateListView(3);
UpdateListView(4);
} }
private void Log(string text) private void Log(string text)
@ -146,6 +198,99 @@ namespace ModbusSlave
return isAvailable; return isAvailable;
} }
#endregion private void InitListView()
{
for (int i = 0; i < 5; i++)
{
if (i == 2)
continue;
string controlName = $"lv{i}x";
Control[] foundControls = this.Controls.Find(controlName, true);
if (foundControls.Length < 1 || !(foundControls[0] is ListView))
continue;
ListView listView = foundControls[0] as ListView;
listView.GetType()
.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.SetValue(listView, true, null);
listView.BeginUpdate();
listView.Items.Clear();
for (int j = 0; j < DATA_COUNT; j++)
{
ListViewItem item = new ListViewItem();
switch(i)
{
case 0: // 0x: 00001 ~ 00010
item.Text = string.Format("{0:D5}", j + 1);
break;
case 1: // 1x: 10001 ~ 10010
item.Text = (10000 + j + 1).ToString();
break;
case 3: // 3x: 30001 ~ 30010
item.Text = (30000 + j + 1).ToString();
break;
case 4: // 4x: 40001 ~ 40010
item.Text = (40000 + j + 1).ToString();
break;
}
item.SubItems.Add("0");
listView.Items.Add(item);
}
listView.Columns[0].Width = -2;
listView.Columns[1].Width = -2;
listView.EndUpdate();
}
}
private void UpdateListView(int regNum)
{
string controlName = $"lv{regNum}x";
Control[] foundControls = this.Controls.Find(controlName, true);
if (foundControls.Length < 1 || !(foundControls[0] is ListView))
return;
ListView listView = foundControls[0] as ListView;
if (listView.InvokeRequired)
{
listView.BeginInvoke(new MethodInvoker(delegate
{
UpdateListView(regNum);
}));
return;
}
listView.BeginUpdate();
for (int i = 0; i < DATA_COUNT; i++)
{
string value = "";
switch (regNum)
{
case 0:
value = server.coils[i + 1] ? "1" : "0";
break;
case 1:
value = server.discreteInputs[i + 1] ? "1" : "0";
break;
case 3:
value = server.inputRegisters[i + 1].ToString();
break;
case 4:
value = server.holdingRegisters[i + 1].ToString();
break;
}
listView.Items[i].SubItems[1].Text = value;
}
listView.EndUpdate();
}
} }
} }

@ -49,6 +49,12 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Dialog\InputValueDialog.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Dialog\InputValueDialog.Designer.cs">
<DependentUpon>InputValueDialog.cs</DependentUpon>
</Compile>
<Compile Include="MainForm.cs"> <Compile Include="MainForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
@ -57,6 +63,9 @@
</Compile> </Compile>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Dialog\InputValueDialog.resx">
<DependentUpon>InputValueDialog.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="MainForm.resx"> <EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon> <DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>

Loading…
Cancel
Save