From 665724aff41332933d5d765a6960403a26cb420f Mon Sep 17 00:00:00 2001 From: BennyZhao Date: Wed, 13 Nov 2019 08:29:39 +0800 Subject: [PATCH] =?UTF-8?q?fix=20ModbusRtu=E3=80=81ModBusAscii=E3=80=81?= =?UTF-8?q?=E4=B8=B2=E5=8F=A3=E7=AD=89=20=E5=A5=87=E5=81=B6=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E3=80=82=20ModBusTcp=E8=AF=BB=E5=86=99=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E4=BD=BF=E7=94=A8=E8=AF=B4=E6=98=8E=E3=80=82=20?= =?UTF-8?q?=E8=A5=BF=E9=97=A8=E5=AD=90S7=20300=20beta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/MitsubishiMCControl.Designer.cs | 2 + .../Controls/ModBusAsciiControl.Designer.cs | 369 ++++++++++-------- IoTClient.Tool/Controls/ModBusAsciiControl.cs | 20 +- .../Controls/ModBusRtuControl.Designer.cs | 105 +++-- IoTClient.Tool/Controls/ModBusRtuControl.cs | 10 +- .../Controls/ModBusTcpControl.Designer.cs | 6 +- .../Controls/PortsControl.Designer.cs | 58 +-- IoTClient.Tool/Controls/PortsControl.cs | 12 +- .../Controls/SiemensControl.Designer.cs | 2 + .../Controls/SiemensS7_300Control.Designer.cs | 10 + .../Controls/SiemensS7_300Control.cs | 303 +++++++++++++- IoTClient.Tool/IndexForm.Designer.cs | 26 +- IoTClient.Tool/Model/VersionCheckInput.cs | 2 +- IoTClient/Clients/ModBus/ModBusRtuClient.cs | 11 +- IoTClient/Clients/PLC/SiemensClient.cs | 28 +- IoTClient/Constants/SiemensConstant.cs | 37 +- IoTClient/Enums/SiemensVersion.cs | 5 + IoTClient/IoTClient/IoTClient.xml | 27 +- IoTServer/Servers/ModBus/ModBusAsciiServer.cs | 3 +- README.md | 46 ++- 20 files changed, 801 insertions(+), 281 deletions(-) diff --git a/IoTClient.Tool/Controls/MitsubishiMCControl.Designer.cs b/IoTClient.Tool/Controls/MitsubishiMCControl.Designer.cs index 70af841..3d8f8ad 100644 --- a/IoTClient.Tool/Controls/MitsubishiMCControl.Designer.cs +++ b/IoTClient.Tool/Controls/MitsubishiMCControl.Designer.cs @@ -353,6 +353,7 @@ this.txt_address.Name = "txt_address"; this.txt_address.Size = new System.Drawing.Size(121, 21); this.txt_address.TabIndex = 9; + this.txt_address.Text = "M2634"; // // but_write // @@ -370,6 +371,7 @@ this.txt_value.Name = "txt_value"; this.txt_value.Size = new System.Drawing.Size(100, 21); this.txt_value.TabIndex = 12; + this.txt_value.Text = "18.88"; // // txt_content // diff --git a/IoTClient.Tool/Controls/ModBusAsciiControl.Designer.cs b/IoTClient.Tool/Controls/ModBusAsciiControl.Designer.cs index b83ceb0..f646fea 100644 --- a/IoTClient.Tool/Controls/ModBusAsciiControl.Designer.cs +++ b/IoTClient.Tool/Controls/ModBusAsciiControl.Designer.cs @@ -29,7 +29,18 @@ private void InitializeComponent() { this.components = new System.ComponentModel.Container(); + this.rd_ulong = new System.Windows.Forms.RadioButton(); + this.rd_short = new System.Windows.Forms.RadioButton(); + this.rd_bit = new System.Windows.Forms.RadioButton(); + this.rd_discrete = new System.Windows.Forms.RadioButton(); + this.button6 = new System.Windows.Forms.Button(); + this.rd_float = new System.Windows.Forms.RadioButton(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.cb_baudRate = new System.Windows.Forms.ComboBox(); + this.cb_parity = new System.Windows.Forms.ComboBox(); + this.label3 = new System.Windows.Forms.Label(); this.cb_portNameSend_server = new System.Windows.Forms.ComboBox(); + this.label8 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.txt_stopBit = new System.Windows.Forms.TextBox(); this.label6 = new System.Windows.Forms.Label(); @@ -41,17 +52,8 @@ this.but_open = new System.Windows.Forms.Button(); this.label5 = new System.Windows.Forms.Label(); this.txt_stationNumber = new System.Windows.Forms.TextBox(); - this.rd_ulong = new System.Windows.Forms.RadioButton(); - this.label8 = new System.Windows.Forms.Label(); - this.rd_short = new System.Windows.Forms.RadioButton(); - this.rd_bit = new System.Windows.Forms.RadioButton(); - this.label2 = new System.Windows.Forms.Label(); - this.rd_discrete = new System.Windows.Forms.RadioButton(); - this.button6 = new System.Windows.Forms.Button(); - this.rd_float = new System.Windows.Forms.RadioButton(); - this.groupBox2 = new System.Windows.Forms.GroupBox(); this.label1 = new System.Windows.Forms.Label(); - this.txt_baudRate = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); this.rd_double = new System.Windows.Forms.RadioButton(); this.rd_ushort = new System.Windows.Forms.RadioButton(); this.groupBox3 = new System.Windows.Forms.GroupBox(); @@ -75,112 +77,6 @@ this.groupBox1.SuspendLayout(); this.SuspendLayout(); // - // cb_portNameSend_server - // - this.cb_portNameSend_server.FormattingEnabled = true; - this.cb_portNameSend_server.Location = new System.Drawing.Point(614, 21); - this.cb_portNameSend_server.Name = "cb_portNameSend_server"; - this.cb_portNameSend_server.Size = new System.Drawing.Size(47, 20); - this.cb_portNameSend_server.TabIndex = 24; - // - // label7 - // - this.label7.AutoSize = true; - this.label7.Location = new System.Drawing.Point(250, 23); - this.label7.Name = "label7"; - this.label7.Size = new System.Drawing.Size(41, 12); - this.label7.TabIndex = 21; - this.label7.Text = "停止位"; - // - // txt_stopBit - // - this.txt_stopBit.Location = new System.Drawing.Point(292, 19); - this.txt_stopBit.Name = "txt_stopBit"; - this.txt_stopBit.Size = new System.Drawing.Size(24, 21); - this.txt_stopBit.TabIndex = 22; - this.txt_stopBit.Text = "1"; - // - // label6 - // - this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(176, 24); - this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(41, 12); - this.label6.TabIndex = 19; - this.label6.Text = "数据位"; - // - // txt_dataBit - // - this.txt_dataBit.Location = new System.Drawing.Point(217, 20); - this.txt_dataBit.Name = "txt_dataBit"; - this.txt_dataBit.Size = new System.Drawing.Size(24, 21); - this.txt_dataBit.TabIndex = 20; - this.txt_dataBit.Text = "8"; - // - // cb_portNameSend - // - this.cb_portNameSend.FormattingEnabled = true; - this.cb_portNameSend.Location = new System.Drawing.Point(34, 20); - this.cb_portNameSend.Name = "cb_portNameSend"; - this.cb_portNameSend.Size = new System.Drawing.Size(47, 20); - this.cb_portNameSend.TabIndex = 18; - // - // but_close - // - this.but_close.Location = new System.Drawing.Point(466, 19); - this.but_close.Name = "but_close"; - this.but_close.Size = new System.Drawing.Size(75, 23); - this.but_close.TabIndex = 17; - this.but_close.Text = "断开"; - this.but_close.UseVisualStyleBackColor = true; - // - // but_server_close - // - this.but_server_close.Location = new System.Drawing.Point(768, 19); - this.but_server_close.Name = "but_server_close"; - this.but_server_close.Size = new System.Drawing.Size(75, 23); - this.but_server_close.TabIndex = 16; - this.but_server_close.Text = "关闭服务"; - this.but_server_close.UseVisualStyleBackColor = true; - this.but_server_close.Click += new System.EventHandler(this.but_server_close_Click); - // - // but_server_open - // - this.but_server_open.Location = new System.Drawing.Point(665, 19); - this.but_server_open.Name = "but_server_open"; - this.but_server_open.Size = new System.Drawing.Size(99, 23); - this.but_server_open.TabIndex = 0; - this.but_server_open.Text = "本地模拟服务"; - this.but_server_open.UseVisualStyleBackColor = true; - this.but_server_open.Click += new System.EventHandler(this.but_server_open_Click); - // - // but_open - // - this.but_open.Location = new System.Drawing.Point(385, 18); - this.but_open.Name = "but_open"; - this.but_open.Size = new System.Drawing.Size(75, 23); - this.but_open.TabIndex = 1; - this.but_open.Text = "连接"; - this.but_open.UseVisualStyleBackColor = true; - this.but_open.Click += new System.EventHandler(this.but_open_Click); - // - // label5 - // - this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(324, 23); - this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(29, 12); - this.label5.TabIndex = 15; - this.label5.Text = "站号"; - // - // txt_stationNumber - // - this.txt_stationNumber.Location = new System.Drawing.Point(353, 19); - this.txt_stationNumber.Name = "txt_stationNumber"; - this.txt_stationNumber.Size = new System.Drawing.Size(25, 21); - this.txt_stationNumber.TabIndex = 14; - this.txt_stationNumber.Text = "1"; - // // rd_ulong // this.rd_ulong.AutoSize = true; @@ -191,15 +87,6 @@ this.rd_ulong.Text = "ulong"; this.rd_ulong.UseVisualStyleBackColor = true; // - // label8 - // - this.label8.AutoSize = true; - this.label8.Location = new System.Drawing.Point(587, 25); - this.label8.Name = "label8"; - this.label8.Size = new System.Drawing.Size(29, 12); - this.label8.TabIndex = 23; - this.label8.Text = "端口"; - // // rd_short // this.rd_short.AutoSize = true; @@ -222,15 +109,6 @@ this.rd_bit.Text = "线圈"; this.rd_bit.UseVisualStyleBackColor = true; // - // label2 - // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(86, 24); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(41, 12); - this.label2.TabIndex = 4; - this.label2.Text = "波特率"; - // // rd_discrete // this.rd_discrete.AutoSize = true; @@ -262,6 +140,9 @@ // // groupBox2 // + this.groupBox2.Controls.Add(this.cb_baudRate); + this.groupBox2.Controls.Add(this.cb_parity); + this.groupBox2.Controls.Add(this.label3); this.groupBox2.Controls.Add(this.cb_portNameSend_server); this.groupBox2.Controls.Add(this.label8); this.groupBox2.Controls.Add(this.label7); @@ -277,29 +158,182 @@ this.groupBox2.Controls.Add(this.txt_stationNumber); this.groupBox2.Controls.Add(this.label1); this.groupBox2.Controls.Add(this.label2); - this.groupBox2.Controls.Add(this.txt_baudRate); this.groupBox2.Location = new System.Drawing.Point(13, 11); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(855, 50); this.groupBox2.TabIndex = 37; this.groupBox2.TabStop = false; // + // cb_baudRate + // + this.cb_baudRate.FormattingEnabled = true; + this.cb_baudRate.Items.AddRange(new object[] { + "1200", + "4800", + "9600", + "14400"}); + this.cb_baudRate.Location = new System.Drawing.Point(121, 17); + this.cb_baudRate.Name = "cb_baudRate"; + this.cb_baudRate.Size = new System.Drawing.Size(47, 20); + this.cb_baudRate.TabIndex = 45; + // + // cb_parity + // + this.cb_parity.FormattingEnabled = true; + this.cb_parity.Items.AddRange(new object[] { + "无", + "奇", + "偶"}); + this.cb_parity.Location = new System.Drawing.Point(325, 17); + this.cb_parity.Name = "cb_parity"; + this.cb_parity.Size = new System.Drawing.Size(35, 20); + this.cb_parity.TabIndex = 44; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(297, 21); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(29, 12); + this.label3.TabIndex = 43; + this.label3.Text = "奇偶"; + // + // cb_portNameSend_server + // + this.cb_portNameSend_server.FormattingEnabled = true; + this.cb_portNameSend_server.Location = new System.Drawing.Point(615, 19); + this.cb_portNameSend_server.Name = "cb_portNameSend_server"; + this.cb_portNameSend_server.Size = new System.Drawing.Size(47, 20); + this.cb_portNameSend_server.TabIndex = 42; + // + // label8 + // + this.label8.AutoSize = true; + this.label8.Location = new System.Drawing.Point(588, 23); + this.label8.Name = "label8"; + this.label8.Size = new System.Drawing.Size(29, 12); + this.label8.TabIndex = 41; + this.label8.Text = "端口"; + // + // label7 + // + this.label7.AutoSize = true; + this.label7.Location = new System.Drawing.Point(233, 21); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(41, 12); + this.label7.TabIndex = 39; + this.label7.Text = "停止位"; + // + // txt_stopBit + // + this.txt_stopBit.Location = new System.Drawing.Point(274, 17); + this.txt_stopBit.Name = "txt_stopBit"; + this.txt_stopBit.Size = new System.Drawing.Size(21, 21); + this.txt_stopBit.TabIndex = 40; + this.txt_stopBit.Text = "1"; + this.txt_stopBit.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(168, 22); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(41, 12); + this.label6.TabIndex = 37; + this.label6.Text = "数据位"; + // + // txt_dataBit + // + this.txt_dataBit.Location = new System.Drawing.Point(209, 18); + this.txt_dataBit.Name = "txt_dataBit"; + this.txt_dataBit.Size = new System.Drawing.Size(21, 21); + this.txt_dataBit.TabIndex = 38; + this.txt_dataBit.Text = "8"; + this.txt_dataBit.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // cb_portNameSend + // + this.cb_portNameSend.FormattingEnabled = true; + this.cb_portNameSend.Location = new System.Drawing.Point(34, 18); + this.cb_portNameSend.Name = "cb_portNameSend"; + this.cb_portNameSend.Size = new System.Drawing.Size(47, 20); + this.cb_portNameSend.TabIndex = 36; + // + // but_close + // + this.but_close.Location = new System.Drawing.Point(495, 16); + this.but_close.Name = "but_close"; + this.but_close.Size = new System.Drawing.Size(75, 23); + this.but_close.TabIndex = 35; + this.but_close.Text = "断开"; + this.but_close.UseVisualStyleBackColor = true; + this.but_close.Click += new System.EventHandler(this.but_close_Click); + // + // but_server_close + // + this.but_server_close.Location = new System.Drawing.Point(768, 17); + this.but_server_close.Name = "but_server_close"; + this.but_server_close.Size = new System.Drawing.Size(75, 23); + this.but_server_close.TabIndex = 34; + this.but_server_close.Text = "关闭服务"; + this.but_server_close.UseVisualStyleBackColor = true; + this.but_server_close.Click += new System.EventHandler(this.but_server_close_Click); + // + // but_server_open + // + this.but_server_open.Location = new System.Drawing.Point(666, 17); + this.but_server_open.Name = "but_server_open"; + this.but_server_open.Size = new System.Drawing.Size(99, 23); + this.but_server_open.TabIndex = 28; + this.but_server_open.Text = "本地模拟服务"; + this.but_server_open.UseVisualStyleBackColor = true; + this.but_server_open.Click += new System.EventHandler(this.but_server_open_Click); + // + // but_open + // + this.but_open.Location = new System.Drawing.Point(416, 16); + this.but_open.Name = "but_open"; + this.but_open.Size = new System.Drawing.Size(75, 23); + this.but_open.TabIndex = 29; + this.but_open.Text = "连接"; + this.but_open.UseVisualStyleBackColor = true; + this.but_open.Click += new System.EventHandler(this.but_open_Click); + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(363, 21); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(29, 12); + this.label5.TabIndex = 33; + this.label5.Text = "站号"; + // + // txt_stationNumber + // + this.txt_stationNumber.Location = new System.Drawing.Point(392, 17); + this.txt_stationNumber.Name = "txt_stationNumber"; + this.txt_stationNumber.Size = new System.Drawing.Size(21, 21); + this.txt_stationNumber.TabIndex = 32; + this.txt_stationNumber.Text = "1"; + this.txt_stationNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(7, 24); + this.label1.Location = new System.Drawing.Point(7, 22); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(29, 12); - this.label1.TabIndex = 3; + this.label1.TabIndex = 30; this.label1.Text = "端口"; // - // txt_baudRate + // label2 // - this.txt_baudRate.Location = new System.Drawing.Point(127, 19); - this.txt_baudRate.Name = "txt_baudRate"; - this.txt_baudRate.Size = new System.Drawing.Size(42, 21); - this.txt_baudRate.TabIndex = 5; - this.txt_baudRate.Text = "9600"; + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(82, 22); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(41, 12); + this.label2.TabIndex = 31; + this.label2.Text = "波特率"; // // rd_double // @@ -340,7 +374,7 @@ // // but_sendData // - this.but_sendData.Location = new System.Drawing.Point(616, 20); + this.but_sendData.Location = new System.Drawing.Point(616, 18); this.but_sendData.Name = "but_sendData"; this.but_sendData.Size = new System.Drawing.Size(75, 23); this.but_sendData.TabIndex = 16; @@ -350,7 +384,7 @@ // // txt_dataPackage // - this.txt_dataPackage.Location = new System.Drawing.Point(424, 21); + this.txt_dataPackage.Location = new System.Drawing.Point(424, 19); this.txt_dataPackage.Name = "txt_dataPackage"; this.txt_dataPackage.Size = new System.Drawing.Size(186, 21); this.txt_dataPackage.TabIndex = 17; @@ -367,7 +401,7 @@ // // but_read // - this.but_read.Location = new System.Drawing.Point(133, 18); + this.but_read.Location = new System.Drawing.Point(133, 16); this.but_read.Name = "but_read"; this.but_read.Size = new System.Drawing.Size(75, 23); this.but_read.TabIndex = 7; @@ -390,10 +424,11 @@ this.txt_address.Name = "txt_address"; this.txt_address.Size = new System.Drawing.Size(88, 21); this.txt_address.TabIndex = 9; + this.txt_address.Text = "1"; // // but_write // - this.but_write.Location = new System.Drawing.Point(337, 20); + this.but_write.Location = new System.Drawing.Point(337, 18); this.but_write.Name = "but_write"; this.but_write.Size = new System.Drawing.Size(75, 23); this.but_write.TabIndex = 10; @@ -403,15 +438,16 @@ // // txt_value // - this.txt_value.Location = new System.Drawing.Point(256, 20); + this.txt_value.Location = new System.Drawing.Point(256, 18); this.txt_value.Name = "txt_value"; this.txt_value.Size = new System.Drawing.Size(74, 21); this.txt_value.TabIndex = 12; + this.txt_value.Text = "3344"; // // lab_value // this.lab_value.AutoSize = true; - this.lab_value.Location = new System.Drawing.Point(228, 24); + this.lab_value.Location = new System.Drawing.Point(228, 22); this.lab_value.Name = "lab_value"; this.lab_value.Size = new System.Drawing.Size(17, 12); this.lab_value.TabIndex = 11; @@ -498,30 +534,13 @@ } #endregion - - private System.Windows.Forms.ComboBox cb_portNameSend_server; - private System.Windows.Forms.Label label7; - private System.Windows.Forms.TextBox txt_stopBit; - private System.Windows.Forms.Label label6; - private System.Windows.Forms.TextBox txt_dataBit; - private System.Windows.Forms.ComboBox cb_portNameSend; - private System.Windows.Forms.Button but_close; - private System.Windows.Forms.Button but_server_close; - private System.Windows.Forms.Button but_server_open; - private System.Windows.Forms.Button but_open; - private System.Windows.Forms.Label label5; - private System.Windows.Forms.TextBox txt_stationNumber; private System.Windows.Forms.RadioButton rd_ulong; - private System.Windows.Forms.Label label8; private System.Windows.Forms.RadioButton rd_short; private System.Windows.Forms.RadioButton rd_bit; - private System.Windows.Forms.Label label2; private System.Windows.Forms.RadioButton rd_discrete; private System.Windows.Forms.Button button6; private System.Windows.Forms.RadioButton rd_float; private System.Windows.Forms.GroupBox groupBox2; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.TextBox txt_baudRate; private System.Windows.Forms.RadioButton rd_double; private System.Windows.Forms.RadioButton rd_ushort; private System.Windows.Forms.GroupBox groupBox3; @@ -540,5 +559,23 @@ private System.Windows.Forms.RadioButton rd_int; private System.Windows.Forms.RadioButton rd_uint; private System.Windows.Forms.TextBox txt_content; + private System.Windows.Forms.ComboBox cb_baudRate; + private System.Windows.Forms.ComboBox cb_parity; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.ComboBox cb_portNameSend_server; + private System.Windows.Forms.Label label8; + private System.Windows.Forms.Label label7; + private System.Windows.Forms.TextBox txt_stopBit; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.TextBox txt_dataBit; + private System.Windows.Forms.ComboBox cb_portNameSend; + private System.Windows.Forms.Button but_close; + private System.Windows.Forms.Button but_server_close; + private System.Windows.Forms.Button but_server_open; + private System.Windows.Forms.Button but_open; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.TextBox txt_stationNumber; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; } } diff --git a/IoTClient.Tool/Controls/ModBusAsciiControl.cs b/IoTClient.Tool/Controls/ModBusAsciiControl.cs index 0b6ec9f..cde6fad 100644 --- a/IoTClient.Tool/Controls/ModBusAsciiControl.cs +++ b/IoTClient.Tool/Controls/ModBusAsciiControl.cs @@ -55,6 +55,9 @@ namespace IoTClient.Tool.Controls UpdatePortNames(); cb_portNameSend.DropDownStyle = ComboBoxStyle.DropDownList; cb_portNameSend_server.DropDownStyle = ComboBoxStyle.DropDownList; + cb_parity.SelectedIndex = 0; + cb_parity.DropDownStyle = ComboBoxStyle.DropDownList; + cb_baudRate.SelectedIndex = 2; } /// @@ -76,11 +79,12 @@ namespace IoTClient.Tool.Controls try { var PortName = cb_portNameSend_server.Text.ToString(); - var BaudRate = int.Parse(txt_baudRate.Text.ToString()); + var BaudRate = int.Parse(cb_baudRate.Text.ToString()); var DataBits = int.Parse(txt_dataBit.Text.ToString()); var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString()); + var parity = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even); server?.Stop(); - server = new ModBusAsciiServer(PortName, BaudRate, DataBits, StopBits); + server = new ModBusAsciiServer(PortName, BaudRate, DataBits, StopBits, parity); server.Start(); AppendText("开启仿真服务"); but_server_open.Enabled = false; @@ -106,7 +110,7 @@ namespace IoTClient.Tool.Controls try { var PortName = cb_portNameSend.Text.ToString(); - var BaudRate = int.Parse(txt_baudRate.Text.ToString()); + var BaudRate = int.Parse(cb_baudRate.Text.ToString()); var DataBits = int.Parse(txt_dataBit.Text.ToString()); var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString()); client?.Close(); @@ -326,6 +330,16 @@ namespace IoTClient.Tool.Controls } } + private void but_close_Click(object sender, EventArgs e) + { + client?.Close(); + AppendText("关闭连接"); + but_open.Enabled = true; + but_close.Enabled = false; + cb_portNameSend.Enabled = true; + but_sendData.Enabled = false; + } + //private void AppendText(string content) //{ // txt_content.Invoke((Action)(() => diff --git a/IoTClient.Tool/Controls/ModBusRtuControl.Designer.cs b/IoTClient.Tool/Controls/ModBusRtuControl.Designer.cs index 02b45d5..86942db 100644 --- a/IoTClient.Tool/Controls/ModBusRtuControl.Designer.cs +++ b/IoTClient.Tool/Controls/ModBusRtuControl.Designer.cs @@ -52,9 +52,11 @@ this.rd_long = new System.Windows.Forms.RadioButton(); this.rd_int = new System.Windows.Forms.RadioButton(); this.rd_uint = new System.Windows.Forms.RadioButton(); - this.txt_baudRate = new System.Windows.Forms.TextBox(); this.txt_content = new System.Windows.Forms.TextBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.cb_baudRate = new System.Windows.Forms.ComboBox(); + this.cb_parity = new System.Windows.Forms.ComboBox(); + this.label3 = new System.Windows.Forms.Label(); this.cb_portNameSend_server = new System.Windows.Forms.ComboBox(); this.label8 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); @@ -77,7 +79,7 @@ // // but_read // - this.but_read.Location = new System.Drawing.Point(133, 18); + this.but_read.Location = new System.Drawing.Point(133, 16); this.but_read.Name = "but_read"; this.but_read.Size = new System.Drawing.Size(75, 23); this.but_read.TabIndex = 7; @@ -100,10 +102,11 @@ this.txt_address.Name = "txt_address"; this.txt_address.Size = new System.Drawing.Size(88, 21); this.txt_address.TabIndex = 9; + this.txt_address.Text = "1"; // // but_write // - this.but_write.Location = new System.Drawing.Point(337, 20); + this.but_write.Location = new System.Drawing.Point(337, 17); this.but_write.Name = "but_write"; this.but_write.Size = new System.Drawing.Size(75, 23); this.but_write.TabIndex = 10; @@ -113,10 +116,11 @@ // // txt_value // - this.txt_value.Location = new System.Drawing.Point(256, 20); + this.txt_value.Location = new System.Drawing.Point(256, 17); this.txt_value.Name = "txt_value"; this.txt_value.Size = new System.Drawing.Size(74, 21); this.txt_value.TabIndex = 12; + this.txt_value.Text = "3344"; // // groupBox3 // @@ -137,7 +141,7 @@ // // but_sendData // - this.but_sendData.Location = new System.Drawing.Point(616, 20); + this.but_sendData.Location = new System.Drawing.Point(616, 17); this.but_sendData.Name = "but_sendData"; this.but_sendData.Size = new System.Drawing.Size(75, 23); this.but_sendData.TabIndex = 16; @@ -147,7 +151,7 @@ // // txt_dataPackage // - this.txt_dataPackage.Location = new System.Drawing.Point(424, 21); + this.txt_dataPackage.Location = new System.Drawing.Point(424, 18); this.txt_dataPackage.Name = "txt_dataPackage"; this.txt_dataPackage.Size = new System.Drawing.Size(186, 21); this.txt_dataPackage.TabIndex = 17; @@ -302,14 +306,6 @@ this.rd_uint.Text = "uint"; this.rd_uint.UseVisualStyleBackColor = true; // - // txt_baudRate - // - this.txt_baudRate.Location = new System.Drawing.Point(127, 19); - this.txt_baudRate.Name = "txt_baudRate"; - this.txt_baudRate.Size = new System.Drawing.Size(42, 21); - this.txt_baudRate.TabIndex = 5; - this.txt_baudRate.Text = "9600"; - // // txt_content // this.txt_content.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); @@ -322,6 +318,9 @@ // // groupBox2 // + this.groupBox2.Controls.Add(this.cb_baudRate); + this.groupBox2.Controls.Add(this.cb_parity); + this.groupBox2.Controls.Add(this.label3); this.groupBox2.Controls.Add(this.cb_portNameSend_server); this.groupBox2.Controls.Add(this.label8); this.groupBox2.Controls.Add(this.label7); @@ -337,17 +336,50 @@ this.groupBox2.Controls.Add(this.txt_stationNumber); this.groupBox2.Controls.Add(this.label1); this.groupBox2.Controls.Add(this.label2); - this.groupBox2.Controls.Add(this.txt_baudRate); this.groupBox2.Location = new System.Drawing.Point(13, 11); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(855, 50); this.groupBox2.TabIndex = 33; this.groupBox2.TabStop = false; // + // cb_baudRate + // + this.cb_baudRate.FormattingEnabled = true; + this.cb_baudRate.Items.AddRange(new object[] { + "1200", + "4800", + "9600", + "14400"}); + this.cb_baudRate.Location = new System.Drawing.Point(121, 17); + this.cb_baudRate.Name = "cb_baudRate"; + this.cb_baudRate.Size = new System.Drawing.Size(47, 20); + this.cb_baudRate.TabIndex = 27; + // + // cb_parity + // + this.cb_parity.FormattingEnabled = true; + this.cb_parity.Items.AddRange(new object[] { + "无", + "奇", + "偶"}); + this.cb_parity.Location = new System.Drawing.Point(325, 17); + this.cb_parity.Name = "cb_parity"; + this.cb_parity.Size = new System.Drawing.Size(35, 20); + this.cb_parity.TabIndex = 26; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(297, 21); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(29, 12); + this.label3.TabIndex = 25; + this.label3.Text = "奇偶"; + // // cb_portNameSend_server // this.cb_portNameSend_server.FormattingEnabled = true; - this.cb_portNameSend_server.Location = new System.Drawing.Point(614, 21); + this.cb_portNameSend_server.Location = new System.Drawing.Point(615, 19); this.cb_portNameSend_server.Name = "cb_portNameSend_server"; this.cb_portNameSend_server.Size = new System.Drawing.Size(47, 20); this.cb_portNameSend_server.TabIndex = 24; @@ -355,7 +387,7 @@ // label8 // this.label8.AutoSize = true; - this.label8.Location = new System.Drawing.Point(587, 25); + this.label8.Location = new System.Drawing.Point(588, 23); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(29, 12); this.label8.TabIndex = 23; @@ -364,7 +396,7 @@ // label7 // this.label7.AutoSize = true; - this.label7.Location = new System.Drawing.Point(250, 23); + this.label7.Location = new System.Drawing.Point(233, 21); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(41, 12); this.label7.TabIndex = 21; @@ -372,16 +404,17 @@ // // txt_stopBit // - this.txt_stopBit.Location = new System.Drawing.Point(292, 19); + this.txt_stopBit.Location = new System.Drawing.Point(274, 17); this.txt_stopBit.Name = "txt_stopBit"; - this.txt_stopBit.Size = new System.Drawing.Size(24, 21); + this.txt_stopBit.Size = new System.Drawing.Size(21, 21); this.txt_stopBit.TabIndex = 22; this.txt_stopBit.Text = "1"; + this.txt_stopBit.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // label6 // this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(176, 24); + this.label6.Location = new System.Drawing.Point(168, 22); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(41, 12); this.label6.TabIndex = 19; @@ -389,23 +422,24 @@ // // txt_dataBit // - this.txt_dataBit.Location = new System.Drawing.Point(217, 20); + this.txt_dataBit.Location = new System.Drawing.Point(209, 18); this.txt_dataBit.Name = "txt_dataBit"; - this.txt_dataBit.Size = new System.Drawing.Size(24, 21); + this.txt_dataBit.Size = new System.Drawing.Size(21, 21); this.txt_dataBit.TabIndex = 20; this.txt_dataBit.Text = "8"; + this.txt_dataBit.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // cb_portNameSend // this.cb_portNameSend.FormattingEnabled = true; - this.cb_portNameSend.Location = new System.Drawing.Point(34, 20); + this.cb_portNameSend.Location = new System.Drawing.Point(34, 18); this.cb_portNameSend.Name = "cb_portNameSend"; this.cb_portNameSend.Size = new System.Drawing.Size(47, 20); this.cb_portNameSend.TabIndex = 18; // // but_close // - this.but_close.Location = new System.Drawing.Point(466, 19); + this.but_close.Location = new System.Drawing.Point(495, 16); this.but_close.Name = "but_close"; this.but_close.Size = new System.Drawing.Size(75, 23); this.but_close.TabIndex = 17; @@ -415,7 +449,7 @@ // // but_server_close // - this.but_server_close.Location = new System.Drawing.Point(768, 19); + this.but_server_close.Location = new System.Drawing.Point(768, 17); this.but_server_close.Name = "but_server_close"; this.but_server_close.Size = new System.Drawing.Size(75, 23); this.but_server_close.TabIndex = 16; @@ -425,7 +459,7 @@ // // but_server_open // - this.but_server_open.Location = new System.Drawing.Point(665, 19); + this.but_server_open.Location = new System.Drawing.Point(666, 17); this.but_server_open.Name = "but_server_open"; this.but_server_open.Size = new System.Drawing.Size(99, 23); this.but_server_open.TabIndex = 0; @@ -435,7 +469,7 @@ // // but_open // - this.but_open.Location = new System.Drawing.Point(385, 18); + this.but_open.Location = new System.Drawing.Point(416, 16); this.but_open.Name = "but_open"; this.but_open.Size = new System.Drawing.Size(75, 23); this.but_open.TabIndex = 1; @@ -446,7 +480,7 @@ // label5 // this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(324, 23); + this.label5.Location = new System.Drawing.Point(363, 21); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(29, 12); this.label5.TabIndex = 15; @@ -454,16 +488,17 @@ // // txt_stationNumber // - this.txt_stationNumber.Location = new System.Drawing.Point(353, 19); + this.txt_stationNumber.Location = new System.Drawing.Point(392, 17); this.txt_stationNumber.Name = "txt_stationNumber"; - this.txt_stationNumber.Size = new System.Drawing.Size(25, 21); + this.txt_stationNumber.Size = new System.Drawing.Size(21, 21); this.txt_stationNumber.TabIndex = 14; this.txt_stationNumber.Text = "1"; + this.txt_stationNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(7, 24); + this.label1.Location = new System.Drawing.Point(7, 22); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(29, 12); this.label1.TabIndex = 3; @@ -472,7 +507,7 @@ // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(86, 24); + this.label2.Location = new System.Drawing.Point(82, 22); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 4; @@ -521,7 +556,6 @@ private System.Windows.Forms.RadioButton rd_long; private System.Windows.Forms.RadioButton rd_int; private System.Windows.Forms.RadioButton rd_uint; - private System.Windows.Forms.TextBox txt_baudRate; private System.Windows.Forms.TextBox txt_content; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Button but_close; @@ -542,5 +576,8 @@ private System.Windows.Forms.Label label8; private System.Windows.Forms.Button but_sendData; private System.Windows.Forms.TextBox txt_dataPackage; + private System.Windows.Forms.ComboBox cb_parity; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.ComboBox cb_baudRate; } } diff --git a/IoTClient.Tool/Controls/ModBusRtuControl.cs b/IoTClient.Tool/Controls/ModBusRtuControl.cs index fe524b1..fa5760f 100644 --- a/IoTClient.Tool/Controls/ModBusRtuControl.cs +++ b/IoTClient.Tool/Controls/ModBusRtuControl.cs @@ -51,6 +51,9 @@ namespace IoTClient.Tool.Controls UpdatePortNames(); cb_portNameSend.DropDownStyle = ComboBoxStyle.DropDownList; cb_portNameSend_server.DropDownStyle = ComboBoxStyle.DropDownList; + cb_parity.SelectedIndex = 0; + cb_parity.DropDownStyle = ComboBoxStyle.DropDownList; + cb_baudRate.SelectedIndex = 2; } /// @@ -72,11 +75,12 @@ namespace IoTClient.Tool.Controls try { var PortName = cb_portNameSend.Text.ToString(); - var BaudRate = int.Parse(txt_baudRate.Text.ToString()); + var BaudRate = int.Parse(cb_baudRate.Text.ToString()); var DataBits = int.Parse(txt_dataBit.Text.ToString()); var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString()); + var parity = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even); client?.Close(); - client = new ModBusRtuClient(PortName, BaudRate, DataBits, StopBits); + client = new ModBusRtuClient(PortName, BaudRate, DataBits, StopBits, parity); var result = client.Open(); if (result.IsSucceed) { @@ -289,7 +293,7 @@ namespace IoTClient.Tool.Controls try { var PortName = cb_portNameSend_server.Text.ToString(); - var BaudRate = int.Parse(txt_baudRate.Text.ToString()); + var BaudRate = int.Parse(cb_baudRate.Text.ToString()); var DataBits = int.Parse(txt_dataBit.Text.ToString()); var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString()); server?.Stop(); diff --git a/IoTClient.Tool/Controls/ModBusTcpControl.Designer.cs b/IoTClient.Tool/Controls/ModBusTcpControl.Designer.cs index 71d8376..c9a60a6 100644 --- a/IoTClient.Tool/Controls/ModBusTcpControl.Designer.cs +++ b/IoTClient.Tool/Controls/ModBusTcpControl.Designer.cs @@ -97,10 +97,10 @@ this.groupBox2.TabIndex = 29; this.groupBox2.TabStop = false; // - // button5 + // but_close // this.but_close.Location = new System.Drawing.Point(318, 19); - this.but_close.Name = "button5"; + this.but_close.Name = "but_close"; this.but_close.Size = new System.Drawing.Size(75, 23); this.but_close.TabIndex = 17; this.but_close.Text = "断开"; @@ -378,6 +378,7 @@ this.txt_address.Name = "txt_address"; this.txt_address.Size = new System.Drawing.Size(88, 21); this.txt_address.TabIndex = 9; + this.txt_address.Text = "1"; // // but_write // @@ -395,6 +396,7 @@ this.txt_value.Name = "txt_value"; this.txt_value.Size = new System.Drawing.Size(74, 21); this.txt_value.TabIndex = 12; + this.txt_value.Text = "3344"; // // lab_value // diff --git a/IoTClient.Tool/Controls/PortsControl.Designer.cs b/IoTClient.Tool/Controls/PortsControl.Designer.cs index fdf91b8..4585171 100644 --- a/IoTClient.Tool/Controls/PortsControl.Designer.cs +++ b/IoTClient.Tool/Controls/PortsControl.Designer.cs @@ -29,7 +29,9 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.cb_checkBit = new System.Windows.Forms.ComboBox(); + this.cb_encoding = new System.Windows.Forms.ComboBox(); + this.cb_parity = new System.Windows.Forms.ComboBox(); + this.label6 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.cb_stopBit = new System.Windows.Forms.ComboBox(); this.label4 = new System.Windows.Forms.Label(); @@ -44,8 +46,6 @@ this.but_close = new System.Windows.Forms.Button(); this.txt_content = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); - this.cb_encoding = new System.Windows.Forms.ComboBox(); - this.label6 = new System.Windows.Forms.Label(); this.txt_msg = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); @@ -56,7 +56,7 @@ // groupBox1 // this.groupBox1.Controls.Add(this.cb_encoding); - this.groupBox1.Controls.Add(this.cb_checkBit); + this.groupBox1.Controls.Add(this.cb_parity); this.groupBox1.Controls.Add(this.label6); this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.cb_stopBit); @@ -74,13 +74,31 @@ this.groupBox1.TabStop = false; this.groupBox1.Text = "设置"; // - // cb_checkBit + // cb_encoding // - this.cb_checkBit.FormattingEnabled = true; - this.cb_checkBit.Location = new System.Drawing.Point(190, 46); - this.cb_checkBit.Name = "cb_checkBit"; - this.cb_checkBit.Size = new System.Drawing.Size(83, 20); - this.cb_checkBit.TabIndex = 10; + this.cb_encoding.FormattingEnabled = true; + this.cb_encoding.Location = new System.Drawing.Point(190, 72); + this.cb_encoding.Name = "cb_encoding"; + this.cb_encoding.Size = new System.Drawing.Size(83, 20); + this.cb_encoding.TabIndex = 12; + this.cb_encoding.SelectedValueChanged += new System.EventHandler(this.cb_encoding_SelectedValueChanged); + // + // cb_parity + // + this.cb_parity.FormattingEnabled = true; + this.cb_parity.Location = new System.Drawing.Point(190, 46); + this.cb_parity.Name = "cb_parity"; + this.cb_parity.Size = new System.Drawing.Size(83, 20); + this.cb_parity.TabIndex = 10; + // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(148, 76); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(41, 12); + this.label6.TabIndex = 11; + this.label6.Text = "编 码"; // // label5 // @@ -208,24 +226,6 @@ this.groupBox3.TabIndex = 14; this.groupBox3.TabStop = false; // - // cb_encoding - // - this.cb_encoding.FormattingEnabled = true; - this.cb_encoding.Location = new System.Drawing.Point(190, 72); - this.cb_encoding.Name = "cb_encoding"; - this.cb_encoding.Size = new System.Drawing.Size(83, 20); - this.cb_encoding.TabIndex = 12; - this.cb_encoding.SelectedValueChanged += new System.EventHandler(this.cb_encoding_SelectedValueChanged); - // - // label6 - // - this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(148, 76); - this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(41, 12); - this.label6.TabIndex = 11; - this.label6.Text = "编 码"; - // // txt_msg // this.txt_msg.Location = new System.Drawing.Point(30, 18); @@ -267,7 +267,7 @@ private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.ComboBox cb_portNameSend; - private System.Windows.Forms.ComboBox cb_checkBit; + private System.Windows.Forms.ComboBox cb_parity; private System.Windows.Forms.Label label5; private System.Windows.Forms.ComboBox cb_stopBit; private System.Windows.Forms.Label label4; diff --git a/IoTClient.Tool/Controls/PortsControl.cs b/IoTClient.Tool/Controls/PortsControl.cs index 9ba2faa..ac7552d 100644 --- a/IoTClient.Tool/Controls/PortsControl.cs +++ b/IoTClient.Tool/Controls/PortsControl.cs @@ -21,7 +21,7 @@ namespace IoTClient.Tool.Controls int[] DataBitArr = new int[] { 8, 7, 6, 5 }; int[] StopBitArr = new int[] { 1, 2, 3 }; string[] Encodings = new string[] { "ASCII", "UTF8", "UTF32", "UTF7", "Unicode" }; - object[] CheckBitArr = new object[] { "None" }; + object[] CheckBitArr = new object[] { "None", "Odd", "Even" }; Encoding encoding = Encoding.ASCII; public PortsControl() { @@ -32,13 +32,13 @@ namespace IoTClient.Tool.Controls cb_baudRate.DataSource = BaudRateArr; cb_dataBit.DataSource = DataBitArr; cb_stopBit.DataSource = StopBitArr; - cb_checkBit.DataSource = CheckBitArr; + cb_parity.DataSource = CheckBitArr; cb_encoding.DataSource = Encodings; cb_portNameSend.DropDownStyle = ComboBoxStyle.DropDownList; cb_encoding.DropDownStyle = ComboBoxStyle.DropDownList; cb_stopBit.DropDownStyle = ComboBoxStyle.DropDownList; - cb_checkBit.DropDownStyle = ComboBoxStyle.DropDownList; + cb_parity.DropDownStyle = ComboBoxStyle.DropDownList; cb_dataBit.DropDownStyle = ComboBoxStyle.DropDownList; SetEncoding(); @@ -58,10 +58,12 @@ namespace IoTClient.Tool.Controls { try { + var parity = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even); serialPort.PortName = cb_portNameSend.Text.ToString(); serialPort.BaudRate = int.Parse(cb_baudRate.Text.ToString()); serialPort.DataBits = int.Parse(cb_dataBit.Text.ToString()); serialPort.StopBits = (StopBits)int.Parse(cb_stopBit.Text.ToString()); + serialPort.Parity = parity; serialPort.ReadTimeout = 1000;//1秒 serialPort.Open(); but_close.Enabled = true; @@ -69,7 +71,7 @@ namespace IoTClient.Tool.Controls cb_baudRate.Enabled = false; cb_dataBit.Enabled = false; cb_stopBit.Enabled = false; - cb_checkBit.Enabled = false; + cb_parity.Enabled = false; cb_portNameSend.Enabled = false; UpdatePortNames(); AppendText("打开连接"); @@ -103,7 +105,7 @@ namespace IoTClient.Tool.Controls cb_baudRate.Enabled = true; cb_dataBit.Enabled = true; cb_stopBit.Enabled = true; - cb_checkBit.Enabled = true; + cb_parity.Enabled = true; cb_portNameSend.Enabled = true; but_close.Enabled = false; UpdatePortNames(); diff --git a/IoTClient.Tool/Controls/SiemensControl.Designer.cs b/IoTClient.Tool/Controls/SiemensControl.Designer.cs index ec302ac..ee9cce6 100644 --- a/IoTClient.Tool/Controls/SiemensControl.Designer.cs +++ b/IoTClient.Tool/Controls/SiemensControl.Designer.cs @@ -91,6 +91,7 @@ this.txt_address.Name = "txt_address"; this.txt_address.Size = new System.Drawing.Size(81, 21); this.txt_address.TabIndex = 9; + this.txt_address.Text = "V2634"; // // but_write // @@ -108,6 +109,7 @@ this.txt_value.Name = "txt_value"; this.txt_value.Size = new System.Drawing.Size(75, 21); this.txt_value.TabIndex = 12; + this.txt_value.Text = "18.88"; // // groupBox3 // diff --git a/IoTClient.Tool/Controls/SiemensS7_300Control.Designer.cs b/IoTClient.Tool/Controls/SiemensS7_300Control.Designer.cs index 44fb3f1..62c762f 100644 --- a/IoTClient.Tool/Controls/SiemensS7_300Control.Designer.cs +++ b/IoTClient.Tool/Controls/SiemensS7_300Control.Designer.cs @@ -74,6 +74,7 @@ this.button3.TabIndex = 24; this.button3.Text = "清空数据"; this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.button3_Click); // // but_close_server // @@ -83,6 +84,7 @@ this.but_close_server.TabIndex = 7; this.but_close_server.Text = "关闭服务"; this.but_close_server.UseVisualStyleBackColor = true; + this.but_close_server.Click += new System.EventHandler(this.but_close_server_Click); // // but_close // @@ -92,6 +94,7 @@ this.but_close.TabIndex = 6; this.but_close.Text = "关闭"; this.but_close.UseVisualStyleBackColor = true; + this.but_close.Click += new System.EventHandler(this.but_close_Click); // // groupBox2 // @@ -117,6 +120,7 @@ this.but_server.TabIndex = 0; this.but_server.Text = "本地模拟服务"; this.but_server.UseVisualStyleBackColor = true; + this.but_server.Click += new System.EventHandler(this.but_server_Click); // // but_open // @@ -126,6 +130,7 @@ this.but_open.TabIndex = 1; this.but_open.Text = "连接"; this.but_open.UseVisualStyleBackColor = true; + this.but_open.Click += new System.EventHandler(this.but_open_Click); // // txt_ip // @@ -296,6 +301,7 @@ this.but_sendData.TabIndex = 16; this.but_sendData.Text = "发送报文"; this.but_sendData.UseVisualStyleBackColor = true; + this.but_sendData.Click += new System.EventHandler(this.but_sendData_Click); // // txt_dataPackage // @@ -322,6 +328,7 @@ this.but_read.TabIndex = 7; this.but_read.Text = "读取"; this.but_read.UseVisualStyleBackColor = true; + this.but_read.Click += new System.EventHandler(this.but_read_Click); // // lab_address // @@ -338,6 +345,7 @@ this.txt_address.Name = "txt_address"; this.txt_address.Size = new System.Drawing.Size(81, 21); this.txt_address.TabIndex = 9; + this.txt_address.Text = "V2634"; // // but_write // @@ -347,6 +355,7 @@ this.but_write.TabIndex = 10; this.but_write.Text = "写入"; this.but_write.UseVisualStyleBackColor = true; + this.but_write.Click += new System.EventHandler(this.but_write_Click); // // txt_value // @@ -354,6 +363,7 @@ this.txt_value.Name = "txt_value"; this.txt_value.Size = new System.Drawing.Size(75, 21); this.txt_value.TabIndex = 12; + this.txt_value.Text = "18.88"; // // lab_value // diff --git a/IoTClient.Tool/Controls/SiemensS7_300Control.cs b/IoTClient.Tool/Controls/SiemensS7_300Control.cs index daae045..3fd4590 100644 --- a/IoTClient.Tool/Controls/SiemensS7_300Control.cs +++ b/IoTClient.Tool/Controls/SiemensS7_300Control.cs @@ -1,20 +1,311 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; +using IoTClient.Clients.PLC; +using IoTClient.Common.Enums; +using IoTClient.Common.Helpers; +using IoTServer.Common; +using IoTServer.Servers.PLC; +using System; using System.Data; +using System.Drawing; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; namespace IoTClient.Tool.Controls { public partial class SiemensS7_300Control : UserControl { + SiemensClient client; + SiemensServer server; public SiemensS7_300Control() { InitializeComponent(); + Size = new Size(880, 450); + groupBox2.Location = new Point(13, 5); + groupBox2.Size = new Size(855, 50); + groupBox1.Location = new Point(13, 55); + groupBox1.Size = new Size(855, 50); + groupBox3.Location = new Point(13, 105); + groupBox3.Size = new Size(855, 50); + txt_content.Location = new Point(13, 160); + + lab_address.Location = new Point(9, 22); + txt_address.Location = new Point(39, 18); + txt_address.Size = new Size(88, 21); + but_read.Location = new Point(132, 17); + + lab_value.Location = new Point(227, 22); + txt_value.Location = new Point(249, 18); + txt_value.Size = new Size(74, 21); + but_write.Location = new Point(328, 17); + + txt_dataPackage.Location = new Point(430, 18); + txt_dataPackage.Size = new Size(186, 21); + but_sendData.Location = new Point(620, 17); + + chb_show_package.Location = new Point(776, 19); + + but_read.Enabled = false; + but_write.Enabled = false; + but_close_server.Enabled = false; + but_close.Enabled = false; + but_sendData.Enabled = false; + } + + private void but_server_Click(object sender, EventArgs e) + { + try + { + server?.Stop(); + server = new SiemensServer(int.Parse(txt_port.Text.Trim())); + server.Start(); + but_server.Enabled = false; + but_close_server.Enabled = true; + but_sendData.Enabled = true; + AppendText($"开启仿真模拟服务"); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + + private void but_close_server_Click(object sender, EventArgs e) + { + server?.Stop(); + but_server.Enabled = true; + but_close_server.Enabled = false; + but_sendData.Enabled = false; + AppendText($"关闭仿真模拟服务"); + } + + private void but_open_Click(object sender, EventArgs e) + { + try + { + client?.Close(); + client = new SiemensClient(SiemensVersion.S7_300, txt_ip.Text?.Trim(), int.Parse(txt_port.Text.Trim())); + var result = client.Open(); + if (!result.IsSucceed) + MessageBox.Show($"连接失败:{result.Err}"); + else + { + but_read.Enabled = true; + but_write.Enabled = true; + but_open.Enabled = false; + but_close.Enabled = true; + AppendText($"连接成功"); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + + private void but_close_Click(object sender, EventArgs e) + { + client?.Close(); + but_open.Enabled = true; + but_close.Enabled = false; + AppendText($"连接关闭"); + } + + /// + /// 读取 + /// + /// + /// + private void but_read_Click(object sender, EventArgs e) + { + try + { + if (string.IsNullOrWhiteSpace(txt_address.Text)) + { + MessageBox.Show("请输入地址"); + return; + } + dynamic result = null; + if (rd_bit.Checked) + { + result = client.ReadBoolean(txt_address.Text); + } + else if (rd_short.Checked) + { + result = client.ReadInt16(txt_address.Text); + } + else if (rd_ushort.Checked) + { + result = client.ReadUInt16(txt_address.Text); + } + else if (rd_int.Checked) + { + result = client.ReadInt32(txt_address.Text); + } + else if (rd_uint.Checked) + { + result = client.ReadUInt32(txt_address.Text); + } + else if (rd_long.Checked) + { + result = client.ReadInt64(txt_address.Text); + } + else if (rd_ulong.Checked) + { + result = client.ReadUInt64(txt_address.Text); + } + else if (rd_float.Checked) + { + result = client.ReadFloat(txt_address.Text); + } + else if (rd_double.Checked) + { + result = client.ReadDouble(txt_address.Text); + } + + if (result.IsSucceed) + AppendText($"[读取 {txt_address.Text?.Trim()} 成功]:{result.Value}"); + else + AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}"); + if (chb_show_package.Checked) + { + AppendText($"[请求报文]{result.Requst}"); + AppendText($"[响应报文]{result.Response}\r\n"); + } + } + catch (Exception ex) + { + //client?.Close(); + MessageBox.Show(ex.Message); + } + } + + /// + /// 写入 + /// + /// + /// + private void but_write_Click(object sender, EventArgs e) + { + if (string.IsNullOrWhiteSpace(txt_address.Text)) + { + MessageBox.Show("请输入地址"); + return; + } + if (string.IsNullOrWhiteSpace(txt_value.Text)) + { + MessageBox.Show("请输入值"); + return; + } + + try + { + dynamic result = null; + if (rd_bit.Checked) + { + if (!bool.TryParse(txt_value.Text?.Trim(), out bool bit)) + { + if (txt_value.Text?.Trim() == "0") + bit = false; + else if (txt_value.Text?.Trim() == "1") + bit = true; + else + { + MessageBox.Show("请输入 True 或 False"); + return; + } + } + result = client.Write(txt_address.Text, bit); + } + else if (rd_short.Checked) + { + result = client.Write(txt_address.Text, short.Parse(txt_value.Text?.Trim())); + } + else if (rd_ushort.Checked) + { + result = client.Write(txt_address.Text, ushort.Parse(txt_value.Text?.Trim())); + } + else if (rd_int.Checked) + { + result = client.Write(txt_address.Text, int.Parse(txt_value.Text?.Trim())); + } + else if (rd_uint.Checked) + { + result = client.Write(txt_address.Text, uint.Parse(txt_value.Text?.Trim())); + } + else if (rd_long.Checked) + { + result = client.Write(txt_address.Text, long.Parse(txt_value.Text?.Trim())); + } + else if (rd_ulong.Checked) + { + result = client.Write(txt_address.Text, ulong.Parse(txt_value.Text?.Trim())); + } + else if (rd_float.Checked) + { + result = client.Write(txt_address.Text, float.Parse(txt_value.Text?.Trim())); + } + else if (rd_double.Checked) + { + result = client.Write(txt_address.Text, double.Parse(txt_value.Text?.Trim())); + } + + + if (result.IsSucceed) + AppendText($"[写入 {txt_address.Text?.Trim()} 成功]:{txt_value.Text?.Trim()} OK"); + else + AppendText($"[写入 {txt_address.Text?.Trim()} 失败]:{result.Err}"); + if (chb_show_package.Checked) + { + AppendText($"[请求报文]{result.Requst}"); + AppendText($"[响应报文]{result.Response}\r\n"); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + + private void button3_Click(object sender, EventArgs e) + { + DataPersist.Clear(); + AppendText($"数据清空成功\r\n"); + } + + private void but_sendData_Click(object sender, EventArgs e) + { + try + { + if (string.IsNullOrWhiteSpace(txt_dataPackage.Text)) + { + MessageBox.Show("请输入要发送的报文"); + return; + } + var dataPackageString = txt_dataPackage.Text.Replace(" ", ""); + if (dataPackageString.Length % 2 != 0) + { + MessageBox.Show("请输入正确的的报文"); + return; + } + + var dataPackage = DataConvert.StringToByteArray(txt_dataPackage.Text?.Trim(), false); + var msg = client.SendPackage(dataPackage); + AppendText($"[请求报文]{string.Join(" ", dataPackage.Select(t => t.ToString("X2")))}"); + AppendText($"[响应报文]{string.Join(" ", msg.Select(t => t.ToString("X2")))}\r\n"); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + client.Close(); + client.Open(); + } + } + + private void AppendText(string content) + { + txt_content.Invoke((Action)(() => + { + txt_content.AppendText($"[{DateTime.Now.ToLongTimeString()}]{content}\r\n"); + })); } } } diff --git a/IoTClient.Tool/IndexForm.Designer.cs b/IoTClient.Tool/IndexForm.Designer.cs index 103f325..e3c5dbd 100644 --- a/IoTClient.Tool/IndexForm.Designer.cs +++ b/IoTClient.Tool/IndexForm.Designer.cs @@ -31,6 +31,7 @@ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(IndexForm)); this.tabControl1 = new System.Windows.Forms.TabControl(); this.ModBusTcp = new System.Windows.Forms.TabPage(); + this.modBusTcpControl1 = new IoTClient.Tool.ModBusTcpControl(); this.ModBusRtu = new System.Windows.Forms.TabPage(); this.ModBusAscii = new System.Windows.Forms.TabPage(); this.Siemens = new System.Windows.Forms.TabPage(); @@ -46,7 +47,6 @@ this.toolStripMenuItemBlogPath = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); - this.modBusTcpControl1 = new IoTClient.Tool.ModBusTcpControl(); this.tabControl1.SuspendLayout(); this.ModBusTcp.SuspendLayout(); this.menuStrip1.SuspendLayout(); @@ -82,6 +82,14 @@ this.ModBusTcp.Text = "ModBusTcp"; this.ModBusTcp.UseVisualStyleBackColor = true; // + // modBusTcpControl1 + // + this.modBusTcpControl1.Dock = System.Windows.Forms.DockStyle.Fill; + this.modBusTcpControl1.Location = new System.Drawing.Point(3, 3); + this.modBusTcpControl1.Name = "modBusTcpControl1"; + this.modBusTcpControl1.Size = new System.Drawing.Size(880, 442); + this.modBusTcpControl1.TabIndex = 0; + // // ModBusRtu // this.ModBusRtu.Location = new System.Drawing.Point(4, 22); @@ -119,7 +127,7 @@ this.SiemensS7300.Padding = new System.Windows.Forms.Padding(3); this.SiemensS7300.Size = new System.Drawing.Size(886, 448); this.SiemensS7300.TabIndex = 8; - this.SiemensS7300.Text = "西门子S7-300"; + this.SiemensS7300.Text = "西门子S7-300 Beta"; this.SiemensS7300.UseVisualStyleBackColor = true; // // MitsubishiMC @@ -129,7 +137,7 @@ this.MitsubishiMC.Padding = new System.Windows.Forms.Padding(3); this.MitsubishiMC.Size = new System.Drawing.Size(886, 448); this.MitsubishiMC.TabIndex = 5; - this.MitsubishiMC.Text = " 三菱MC "; + this.MitsubishiMC.Text = " 三菱MC Beta"; this.MitsubishiMC.UseVisualStyleBackColor = true; // // OmronFinsTcp @@ -139,7 +147,7 @@ this.OmronFinsTcp.Padding = new System.Windows.Forms.Padding(3); this.OmronFinsTcp.Size = new System.Drawing.Size(886, 448); this.OmronFinsTcp.TabIndex = 6; - this.OmronFinsTcp.Text = "欧姆龙FinsTcp"; + this.OmronFinsTcp.Text = "欧姆龙FinsTcp Beta"; this.OmronFinsTcp.UseVisualStyleBackColor = true; // // BACnet @@ -223,17 +231,9 @@ this.toolStripMenuItem1.ForeColor = System.Drawing.Color.White; this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Size = new System.Drawing.Size(75, 21); - this.toolStripMenuItem1.Text = "版本 0.3.1"; + this.toolStripMenuItem1.Text = "版本 0.3.2"; this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click); // - // modBusTcpControl1 - // - this.modBusTcpControl1.Dock = System.Windows.Forms.DockStyle.Fill; - this.modBusTcpControl1.Location = new System.Drawing.Point(3, 3); - this.modBusTcpControl1.Name = "modBusTcpControl1"; - this.modBusTcpControl1.Size = new System.Drawing.Size(880, 442); - this.modBusTcpControl1.TabIndex = 0; - // // IndexForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); diff --git a/IoTClient.Tool/Model/VersionCheckInput.cs b/IoTClient.Tool/Model/VersionCheckInput.cs index fdc7f57..e0a7c39 100644 --- a/IoTClient.Tool/Model/VersionCheckInput.cs +++ b/IoTClient.Tool/Model/VersionCheckInput.cs @@ -5,7 +5,7 @@ /// /// 当前版本 /// - public float CurrentVersion { get; set; } = 0.31f; + public float CurrentVersion { get; set; } = 0.32f; /// /// 忽略版本 diff --git a/IoTClient/Clients/ModBus/ModBusRtuClient.cs b/IoTClient/Clients/ModBus/ModBusRtuClient.cs index 395859b..b73c953 100644 --- a/IoTClient/Clients/ModBus/ModBusRtuClient.cs +++ b/IoTClient/Clients/ModBus/ModBusRtuClient.cs @@ -29,7 +29,7 @@ namespace IoTClient.Clients.ModBus /// 波特率 /// 数据位 /// 停止位 - public ModBusRtuClient(string portName, int baudRate, int dataBits, StopBits stopBits) + public ModBusRtuClient(string portName, int baudRate, int dataBits, StopBits stopBits, Parity parity) { if (serialPort == null) serialPort = new SerialPort(); serialPort.PortName = portName; @@ -37,6 +37,7 @@ namespace IoTClient.Clients.ModBus serialPort.DataBits = dataBits; serialPort.StopBits = stopBits; serialPort.Encoding = Encoding.ASCII; + serialPort.Parity = parity; #if !DEBUG serialPort.ReadTimeout = 1000;//1秒 #endif @@ -160,7 +161,7 @@ namespace IoTClient.Clients.ModBus { result.IsSucceed = false; result.Err = "响应结果CRC16验证失败"; - return result; + //return result; } byte[] resultData = new byte[responsePackage.Length - 2]; @@ -437,7 +438,7 @@ namespace IoTClient.Clients.ModBus { result.IsSucceed = false; result.Err = "响应结果CRC16验证失败"; - return result; + //return result; } byte[] resultBuffer = new byte[responsePackage.Length - 2]; Buffer.BlockCopy(responsePackage, 0, resultBuffer, 0, resultBuffer.Length); @@ -474,13 +475,13 @@ namespace IoTClient.Clients.ModBus var command = GetWriteCommand(address, values, stationNumber, functionCode); var commandCRC16 = CRC16.GetCRC16(command); - result.Requst = string.Join(" ", commandCRC16.Select(t => t.ToString("X2"))); + result.Requst = string.Join(" ", commandCRC16.Select(t => t.ToString("X2"))); var responsePackage = SendPackage(commandCRC16); if (!CRC16.CheckCRC16(responsePackage)) { result.IsSucceed = false; result.Err = "响应结果CRC16验证失败"; - return result; + //return result; } byte[] resultBuffer = new byte[responsePackage.Length - 2]; Array.Copy(responsePackage, 0, resultBuffer, 0, resultBuffer.Length); diff --git a/IoTClient/Clients/PLC/SiemensClient.cs b/IoTClient/Clients/PLC/SiemensClient.cs index f387ce5..84fbc37 100644 --- a/IoTClient/Clients/PLC/SiemensClient.cs +++ b/IoTClient/Clients/PLC/SiemensClient.cs @@ -57,13 +57,25 @@ namespace IoTClient.Clients.PLC //连接 socket.Connect(ipAndPoint); + var Command1 = SiemensConstant.Command1; + var Command2 = SiemensConstant.Command2; + if (version == SiemensVersion.S7_200Smart) + { + Command1 = SiemensConstant.Command1_200Smart; + Command2 = SiemensConstant.Command2_200Smart; + } + else if (version == SiemensVersion.S7_300) + { + Command1 = SiemensConstant.Command1_300; + } + //第一次初始化指令交互 - socket.Send(SiemensConstant.Command1_200Smart); + socket.Send(Command1); var head1 = SocketRead(socket, SiemensConstant.InitHeadLength); SocketRead(socket, GetContentLength(head1)); //第二次初始化指令交互 - socket.Send(SiemensConstant.Command2_200Smart); + socket.Send(Command2); var head2 = SocketRead(socket, SiemensConstant.InitHeadLength); SocketRead(socket, GetContentLength(head2)); return result; @@ -153,7 +165,7 @@ namespace IoTClient.Clients.PLC if (ex.SocketErrorCode == SocketError.TimedOut) { result.Err = "连接超时"; - result.ErrList.Add("连接超时"); + result.ErrList.Add("连接超时"); } else { @@ -204,7 +216,7 @@ namespace IoTClient.Clients.PLC if (ex.SocketErrorCode == SocketError.TimedOut) { result.Err = "连接超时"; - result.ErrList.Add("连接超时"); + result.ErrList.Add("连接超时"); } else { @@ -478,8 +490,8 @@ namespace IoTClient.Clients.PLC //发送写入信息 var arg = ConvertArg(address); byte[] command = GetWriteByteCommand(arg.TypeCode, arg.BeginAddress, arg.DbBlock, value); - result.Requst = string.Join(" ", command.Select(t => t.ToString("X2"))); - var dataPackage = SendPackage(command); + result.Requst = string.Join(" ", command.Select(t => t.ToString("X2"))); + var dataPackage = SendPackage(command); result.Response = string.Join(" ", dataPackage.Select(t => t.ToString("X2"))); } catch (SocketException ex) @@ -523,7 +535,7 @@ namespace IoTClient.Clients.PLC //发送写入信息 var arg = ConvertArg(address); byte[] command = GetWriteCommand(arg.TypeCode, arg.BeginAddress, arg.DbBlock, data); - result.Requst = string.Join(" ", command.Select(t => t.ToString("X2"))); + result.Requst = string.Join(" ", command.Select(t => t.ToString("X2"))); var dataPackage = SendPackage(command); result.Response = string.Join(" ", dataPackage.Select(t => t.ToString("X2"))); } @@ -533,7 +545,7 @@ namespace IoTClient.Clients.PLC if (ex.SocketErrorCode == SocketError.TimedOut) { result.Err = "连接超时"; - result.ErrList.Add("连接超时"); + result.ErrList.Add("连接超时"); } else { diff --git a/IoTClient/Constants/SiemensConstant.cs b/IoTClient/Constants/SiemensConstant.cs index 54fc9a4..1404469 100644 --- a/IoTClient/Constants/SiemensConstant.cs +++ b/IoTClient/Constants/SiemensConstant.cs @@ -2,12 +2,46 @@ namespace IoTClient.Common.Constants { + /// + /// + /// public class SiemensConstant { /// /// Head头读取长度 /// public static readonly ushort InitHeadLength = 4; + + /// + /// 第一次初始化指令交互报文 + /// + public static readonly byte[] Command1 = new byte[22] + { + 0x03,0x00,0x00,0x16,0x11,0xE0,0x00,0x00, + 0x00,0x01,0x00,0xC0,0x01,0x0A,0xC1,0x02, + 0x01,0x02,0xC2,0x02,0x01,0x00 + }; + + /// + /// + /// + public static readonly byte[] Command1_300 = new byte[22] + { + 0x03,0x00,0x00,0x16,0x11,0xE0,0x00,0x00, + 0x00,0x01,0x00,0xC0,0x01,0x0A,0xC1,0x02, + 0x01,0x02,0xC2,0x02,0x01,0x02 + }; + + /// + /// 第二次初始化指令交互报文 + /// + public static readonly byte[] Command2 = new byte[25] + { + 0x03,0x00,0x00,0x19,0x02,0xF0,0x80,0x32, + 0x01,0x00,0x00,0x04,0x00,0x00,0x08,0x00, + 0x00,0xF0,0x00,0x00,0x01,0x00,0x01,0x01,0xE0 + }; + /// /// 第一次初始化指令交互报文 /// @@ -17,6 +51,7 @@ namespace IoTClient.Common.Constants 0x00,0x01,0x00,0xC1,0x02,0x10,0x00,0xC2, 0x02,0x03,0x00,0xC0,0x01,0x0A }; + /// /// 第二次初始化指令交互报文 /// @@ -25,6 +60,6 @@ namespace IoTClient.Common.Constants 0x03,0x00,0x00,0x19,0x02,0xF0,0x80,0x32, 0x01,0x00,0x00,0xCC,0xC1,0x00,0x08,0x00, 0x00,0xF0,0x00,0x00,0x01,0x00,0x01,0x03,0xC0 - }; + }; } } diff --git a/IoTClient/Enums/SiemensVersion.cs b/IoTClient/Enums/SiemensVersion.cs index 94c273c..6d59c2b 100644 --- a/IoTClient/Enums/SiemensVersion.cs +++ b/IoTClient/Enums/SiemensVersion.cs @@ -21,5 +21,10 @@ namespace IoTClient.Common.Enums /// [Description("西门子S7-200Smar")] S7_200Smart = 1, + /// + /// 西门子S7-300 + /// + [Description("西门子S7-300")] + S7_300 = 2, } } diff --git a/IoTClient/IoTClient/IoTClient.xml b/IoTClient/IoTClient/IoTClient.xml index ec93e9c..64011df 100644 --- a/IoTClient/IoTClient/IoTClient.xml +++ b/IoTClient/IoTClient/IoTClient.xml @@ -308,7 +308,7 @@ 是否自动打开关闭 - + 构造函数 @@ -1397,11 +1397,31 @@ + + + + + Head头读取长度 + + + 第一次初始化指令交互报文 + + + + + + + + + + 第二次初始化指令交互报文 + + 第一次初始化指令交互报文 @@ -1427,6 +1447,11 @@ 西门子S7-200Smar + + + 西门子S7-300 + + diff --git a/IoTServer/Servers/ModBus/ModBusAsciiServer.cs b/IoTServer/Servers/ModBus/ModBusAsciiServer.cs index 7bd2b64..93b9217 100644 --- a/IoTServer/Servers/ModBus/ModBusAsciiServer.cs +++ b/IoTServer/Servers/ModBus/ModBusAsciiServer.cs @@ -12,7 +12,7 @@ namespace IoTServer.Servers.ModBus { private SerialPort serialPort; DataPersist dataPersist; - public ModBusAsciiServer(string portName, int baudRate, int dataBits, StopBits stopBits) + public ModBusAsciiServer(string portName, int baudRate, int dataBits, StopBits stopBits, Parity parity) { if (serialPort == null) serialPort = new SerialPort(); serialPort.PortName = portName; @@ -20,6 +20,7 @@ namespace IoTServer.Servers.ModBus serialPort.DataBits = dataBits; serialPort.StopBits = stopBits; serialPort.Encoding = Encoding.ASCII; + serialPort.Parity = parity; #if !DEBUG serialPort.ReadTimeout = 1000;//1秒 #endif diff --git a/README.md b/README.md index 4e2693e..a539b50 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,48 @@ # IoTClient -这是一个物联网设备通讯协议实现客户端,将包括主流PLC通信读取、ModBus协议、Bacnet协议等常用工业通讯协议。本组件终身开源免费,采用最宽松MIT协议,您也可以随意修改和商业使用(商业使用请做好评估和测试)。 -QQ交流群:[995475200](https://jq.qq.com/?_wv=1027&k=5bz0ne5) +- 这是一个物联网设备通讯协议实现客户端,将包括主流PLC通信读取、ModBus协议、Bacnet协议等常用工业通讯协议。 +- 本组件基于.NET Standard 2.0,可用于.Net的跨平台开发,如Windows、Linux甚至可运行于树莓派上。 +- 本组件终身开源免费,采用最宽松MIT协议,您也可以随意修改和商业使用(商业使用请做好评估和测试)。 +- 开发工具:Visual Studio 2019 +- QQ交流群:[995475200](https://jq.qq.com/?_wv=1027&k=5bz0ne5) -# Demo效果图 +# 使用说明 +## 引用组件 +[Nuget安装](https://www.nuget.org/packages/IoTClient/) ```Install-Package IoTClient ``` +或图形化安装 +![](https://img2018.cnblogs.com/blog/208266/201911/208266-20191110094638423-1017899651.png) + +## ModBusTcp读写操作 + +``` +//1、实例化客户端 - 输入正确的IP和端口 +ModBusTcpClient client = new ModBusTcpClient("127.0.0.1", 502); + +//2、读操作 - 参数依次是:地址 、值 、站号 、功能码 +client.Write("4", (short)33, 2, 16); +client.Write("4", (short)3344, 2, 16); + +//3、写操作 - 参数依次是:地址 、站号 、功能码 +var value = client.ReadInt16("4", 2, 3).Value; +var value2 = client.ReadInt32("4", 2, 3).Value; + +//4、如果没有主动Open,则会每次读写操作的时候自动打开自动和关闭连接,这样会使读写效率大大减低。所以建议手动Open和Close。 +client.Open(); + +//5、读写操作都会返回操作结果对象Result +var result = client.ReadInt16("4", 2, 3); +//5.1 读取是否成功(true或false) +var isSucceed = result.IsSucceed; +//5.2 读取失败的异常信息 +var errMsg = result.Err; +//5.3 读取操作实际发送的请求报文 +var requst = result.Requst; +//5.4 读取操作服务端响应的报文 +var response = result.Response; +//5.5 读取到的值 +var value3 = result.Value; +``` + +# IoTClient Tool效果图 ![image](https://user-images.githubusercontent.com/5820324/68068792-1a50c980-fd94-11e9-9ad6-3c8cf683cf6d.png) ![image](https://user-images.githubusercontent.com/5820324/68068801-305e8a00-fd94-11e9-9745-98afa11c968e.png)