From 352e6558143aae83c3bbcb02399a5ca656159f16 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 13 Oct 2022 17:31:24 +0900 Subject: [PATCH] modbus slave --- .../Dialog/InputValueDialog.Designer.cs | 87 ++++++++ .../ModbusSlave/Dialog/InputValueDialog.cs | 31 +++ .../ModbusSlave/Dialog/InputValueDialog.resx | 120 +++++++++++ ModbusStudy/ModbusSlave/MainForm.Designer.cs | 147 ++++++++------ ModbusStudy/ModbusSlave/MainForm.cs | 189 ++++++++++++++++-- ModbusStudy/ModbusSlave/ModbusSlave.csproj | 9 + 6 files changed, 503 insertions(+), 80 deletions(-) create mode 100644 ModbusStudy/ModbusSlave/Dialog/InputValueDialog.Designer.cs create mode 100644 ModbusStudy/ModbusSlave/Dialog/InputValueDialog.cs create mode 100644 ModbusStudy/ModbusSlave/Dialog/InputValueDialog.resx diff --git a/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.Designer.cs b/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.Designer.cs new file mode 100644 index 0000000..28f174c --- /dev/null +++ b/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.Designer.cs @@ -0,0 +1,87 @@ + +namespace ModbusSlave.Dialog +{ + partial class InputValueDialog + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + 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; + } +} \ No newline at end of file diff --git a/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.cs b/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.cs new file mode 100644 index 0000000..843b81e --- /dev/null +++ b/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.cs @@ -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; + } + } +} diff --git a/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.resx b/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ModbusStudy/ModbusSlave/Dialog/InputValueDialog.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ModbusStudy/ModbusSlave/MainForm.Designer.cs b/ModbusStudy/ModbusSlave/MainForm.Designer.cs index 6732e2a..548c471 100644 --- a/ModbusStudy/ModbusSlave/MainForm.Designer.cs +++ b/ModbusStudy/ModbusSlave/MainForm.Designer.cs @@ -42,7 +42,7 @@ namespace ModbusSlave this.chkAutoScroll = new System.Windows.Forms.CheckBox(); this.btnClear = new System.Windows.Forms.Button(); 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.lv0x = new System.Windows.Forms.ListView(); 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.nudPort)).BeginInit(); this.groupBox2.SuspendLayout(); - this.tabControl1.SuspendLayout(); + this.tabLv.SuspendLayout(); this.tabPage1.SuspendLayout(); this.tabPage2.SuspendLayout(); this.tabPage3.SuspendLayout(); @@ -80,9 +80,11 @@ namespace ModbusSlave this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.tbStatus); 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.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.TabStop = false; this.groupBox1.Text = "Server"; @@ -90,9 +92,10 @@ namespace ModbusSlave // btnStop // 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.Size = new System.Drawing.Size(75, 23); + this.btnStop.Size = new System.Drawing.Size(75, 29); this.btnStop.TabIndex = 3; this.btnStop.Text = "Stop"; this.btnStop.UseVisualStyleBackColor = true; @@ -101,9 +104,10 @@ namespace ModbusSlave // btnRun // 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.Size = new System.Drawing.Size(75, 23); + this.btnRun.Size = new System.Drawing.Size(75, 29); this.btnRun.TabIndex = 3; this.btnRun.Text = "Run"; this.btnRun.UseVisualStyleBackColor = true; @@ -111,31 +115,38 @@ namespace ModbusSlave // // 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.Size = new System.Drawing.Size(156, 21); + this.nudAddress.Size = new System.Drawing.Size(156, 23); this.nudAddress.TabIndex = 2; this.nudAddress.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; + this.nudAddress.Value = new decimal(new int[] { + 1, + 0, + 0, + 0}); // // label3 // 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.Size = new System.Drawing.Size(52, 12); + this.label3.Size = new System.Drawing.Size(49, 15); this.label3.TabIndex = 0; this.label3.Text = "Address"; // // 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[] { 65535, 0, 0, 0}); 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.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; this.nudPort.Value = new decimal(new int[] { @@ -147,9 +158,9 @@ namespace ModbusSlave // label2 // 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.Size = new System.Drawing.Size(55, 12); + this.label2.Size = new System.Drawing.Size(54, 15); this.label2.TabIndex = 0; this.label2.Text = "TCP port"; // @@ -157,10 +168,11 @@ namespace ModbusSlave // this.tbStatus.BackColor = System.Drawing.Color.Silver; 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.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.Text = "Stop"; this.tbStatus.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; @@ -168,9 +180,9 @@ namespace ModbusSlave // label1 // 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.Size = new System.Drawing.Size(40, 12); + this.label1.Size = new System.Drawing.Size(40, 15); this.label1.TabIndex = 0; this.label1.Text = "Status"; // @@ -179,9 +191,11 @@ namespace ModbusSlave this.groupBox2.Controls.Add(this.chkAutoScroll); this.groupBox2.Controls.Add(this.btnClear); 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.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.TabStop = false; this.groupBox2.Text = "Log"; @@ -191,9 +205,10 @@ namespace ModbusSlave this.chkAutoScroll.AutoSize = true; this.chkAutoScroll.Checked = true; 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.Size = new System.Drawing.Size(84, 16); + this.chkAutoScroll.Size = new System.Drawing.Size(84, 19); this.chkAutoScroll.TabIndex = 1; this.chkAutoScroll.Text = "Auto scroll"; this.chkAutoScroll.UseVisualStyleBackColor = true; @@ -201,9 +216,10 @@ namespace ModbusSlave // btnClear // 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.Size = new System.Drawing.Size(75, 23); + this.btnClear.Size = new System.Drawing.Size(75, 29); this.btnClear.TabIndex = 3; this.btnClear.Text = "Clear log"; this.btnClear.UseVisualStyleBackColor = true; @@ -214,32 +230,36 @@ namespace ModbusSlave this.rtbLog.BackColor = System.Drawing.Color.Black; this.rtbLog.BorderStyle = System.Windows.Forms.BorderStyle.None; 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.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.TabStop = false; this.rtbLog.Text = ""; // - // tabControl1 + // tabLv // - this.tabControl1.Controls.Add(this.tabPage1); - this.tabControl1.Controls.Add(this.tabPage2); - this.tabControl1.Controls.Add(this.tabPage3); - this.tabControl1.Controls.Add(this.tabPage4); - this.tabControl1.Location = new System.Drawing.Point(268, 12); - this.tabControl1.Name = "tabControl1"; - this.tabControl1.SelectedIndex = 0; - this.tabControl1.Size = new System.Drawing.Size(263, 362); - this.tabControl1.TabIndex = 1; + this.tabLv.Controls.Add(this.tabPage1); + this.tabLv.Controls.Add(this.tabPage2); + this.tabLv.Controls.Add(this.tabPage3); + this.tabLv.Controls.Add(this.tabPage4); + this.tabLv.Location = new System.Drawing.Point(268, 15); + this.tabLv.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.tabLv.Name = "tabLv"; + this.tabLv.SelectedIndex = 0; + this.tabLv.Size = new System.Drawing.Size(263, 452); + this.tabLv.TabIndex = 1; // // tabPage1 // this.tabPage1.Controls.Add(this.lv0x); 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.Padding = new System.Windows.Forms.Padding(3); - this.tabPage1.Size = new System.Drawing.Size(255, 336); + this.tabPage1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.tabPage1.Size = new System.Drawing.Size(255, 426); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "0x"; this.tabPage1.UseVisualStyleBackColor = true; @@ -252,10 +272,11 @@ namespace ModbusSlave this.lv0x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv0x.FullRowSelect = true; 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.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.UseCompatibleStateImageBehavior = false; this.lv0x.View = System.Windows.Forms.View.Details; @@ -274,9 +295,10 @@ namespace ModbusSlave // this.tabPage2.Controls.Add(this.lv1x); 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.Padding = new System.Windows.Forms.Padding(3); - this.tabPage2.Size = new System.Drawing.Size(255, 336); + this.tabPage2.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.tabPage2.Size = new System.Drawing.Size(255, 426); this.tabPage2.TabIndex = 1; this.tabPage2.Text = "1x"; this.tabPage2.UseVisualStyleBackColor = true; @@ -289,10 +311,11 @@ namespace ModbusSlave this.lv1x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv1x.FullRowSelect = true; 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.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.UseCompatibleStateImageBehavior = false; this.lv1x.View = System.Windows.Forms.View.Details; @@ -311,8 +334,10 @@ namespace ModbusSlave // this.tabPage3.Controls.Add(this.lv3x); 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.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.Text = "3x"; this.tabPage3.UseVisualStyleBackColor = true; @@ -325,10 +350,11 @@ namespace ModbusSlave this.lv3x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv3x.FullRowSelect = true; 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.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.UseCompatibleStateImageBehavior = false; this.lv3x.View = System.Windows.Forms.View.Details; @@ -346,9 +372,11 @@ namespace ModbusSlave // tabPage4 // 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.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.Text = "4x"; this.tabPage4.UseVisualStyleBackColor = true; @@ -361,10 +389,11 @@ namespace ModbusSlave this.lv4x.Dock = System.Windows.Forms.DockStyle.Fill; this.lv4x.FullRowSelect = true; 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.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.UseCompatibleStateImageBehavior = false; this.lv4x.View = System.Windows.Forms.View.Details; @@ -381,14 +410,16 @@ namespace ModbusSlave // // 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.BackColor = System.Drawing.Color.White; - this.ClientSize = new System.Drawing.Size(536, 386); - this.Controls.Add(this.tabControl1); + this.ClientSize = new System.Drawing.Size(536, 482); + this.Controls.Add(this.tabLv); this.Controls.Add(this.groupBox2); 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.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "MainForm"; this.Text = "Modbus Slave"; this.groupBox1.ResumeLayout(false); @@ -397,7 +428,7 @@ namespace ModbusSlave ((System.ComponentModel.ISupportInitialize)(this.nudPort)).EndInit(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); - this.tabControl1.ResumeLayout(false); + this.tabLv.ResumeLayout(false); this.tabPage1.ResumeLayout(false); this.tabPage2.ResumeLayout(false); this.tabPage3.ResumeLayout(false); @@ -421,7 +452,7 @@ namespace ModbusSlave private System.Windows.Forms.CheckBox chkAutoScroll; private System.Windows.Forms.Button btnClear; 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.ListView lv0x; private System.Windows.Forms.ColumnHeader columnHeader1; diff --git a/ModbusStudy/ModbusSlave/MainForm.cs b/ModbusStudy/ModbusSlave/MainForm.cs index 8708cae..6101cf6 100644 --- a/ModbusStudy/ModbusSlave/MainForm.cs +++ b/ModbusStudy/ModbusSlave/MainForm.cs @@ -9,26 +9,25 @@ using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using System.Timers; +using ModbusSlave.Dialog; namespace ModbusSlave { 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() { InitializeComponent(); InitInstance(); } - #region Field - - private ModbusServer server; - private bool isFirstLog = true; - - #endregion - - #region Button event - private void btnRun_Click(object sender, EventArgs e) { try @@ -51,6 +50,8 @@ namespace ModbusSlave tbStatus.Text = "Run"; tbStatus.BackColor = Color.Lime; + + updateTimer.Start(); } catch (Exception ex) { @@ -71,6 +72,8 @@ namespace ModbusSlave tbStatus.Text = "Stop"; tbStatus.BackColor = Color.Silver; + + updateTimer.Stop(); } catch (Exception ex) { @@ -83,25 +86,74 @@ namespace ModbusSlave ClearLog(); } - #endregion - - #region Timer event - - #endregion + private void Lv_MouseDoubleClick(object sender, MouseEventArgs e) + { + if (btnRun.Enabled) + return; - #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) @@ -146,6 +198,99 @@ namespace ModbusSlave 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(); + } } } diff --git a/ModbusStudy/ModbusSlave/ModbusSlave.csproj b/ModbusStudy/ModbusSlave/ModbusSlave.csproj index e8feed4..2de80de 100644 --- a/ModbusStudy/ModbusSlave/ModbusSlave.csproj +++ b/ModbusStudy/ModbusSlave/ModbusSlave.csproj @@ -49,6 +49,12 @@ + + Form + + + InputValueDialog.cs + Form @@ -57,6 +63,9 @@ + + InputValueDialog.cs + MainForm.cs