上面四篇我们进行封装连接PLC以及读写PLC内存地址表
C#使用TCP-S7协议读写西门子PLC(一)-CSDN博客
C#使用TCP-S7协议读写西门子PLC(二)-CSDN博客
C#使用TCP-S7协议读写西门子PLC(三)-CSDN博客
C#使用TCP-S7协议读写西门子PLC(四)-CSDN博客
这里我们进行测试操作
西门子PLC-S7协议测试读写基础数据类型、字符串、Unicode字符串、连续数组等
窗体应用程序PlcSiemesS7Demo应用程序文件清单如下图所示
窗体FormS7Protocol设计器如下:
窗体设计器代码如下:
文件FormS7Protocol.Designer.cs
namespace PlcSiemesS7Demo
{partial class FormS7Protocol{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.panel1 = new System.Windows.Forms.Panel();this.cboSiemensPLCCategory = new System.Windows.Forms.ComboBox();this.label2 = new System.Windows.Forms.Label();this.btnDisconnect = new System.Windows.Forms.Button();this.btnConnect = new System.Windows.Forms.Button();this.txbPort = new System.Windows.Forms.TextBox();this.label3 = new System.Windows.Forms.Label();this.txbIP = new System.Windows.Forms.TextBox();this.label1 = new System.Windows.Forms.Label();this.rtxbDisplay = new System.Windows.Forms.RichTextBox();this.groupBox1 = new System.Windows.Forms.GroupBox();this.btnReadWString = new System.Windows.Forms.Button();this.label12 = new System.Windows.Forms.Label();this.txtReadValWString = new System.Windows.Forms.TextBox();this.label13 = new System.Windows.Forms.Label();this.txtReadWStringLength = new System.Windows.Forms.TextBox();this.rtxtReadValArray = new System.Windows.Forms.RichTextBox();this.btnReadArray = new System.Windows.Forms.Button();this.label9 = new System.Windows.Forms.Label();this.label8 = new System.Windows.Forms.Label();this.txtReadArrayLength = new System.Windows.Forms.TextBox();this.btnReadString = new System.Windows.Forms.Button();this.label7 = new System.Windows.Forms.Label();this.txtReadValString = new System.Windows.Forms.TextBox();this.label4 = new System.Windows.Forms.Label();this.txtReadStringLength = new System.Windows.Forms.TextBox();this.btnReadBasic = new System.Windows.Forms.Button();this.label5 = new System.Windows.Forms.Label();this.txtReadValBasic = new System.Windows.Forms.TextBox();this.cboReadDataType = new System.Windows.Forms.ComboBox();this.label6 = new System.Windows.Forms.Label();this.label10 = new System.Windows.Forms.Label();this.label11 = new System.Windows.Forms.Label();this.txtOffsetAddress = new System.Windows.Forms.TextBox();this.cboRegisterCategory = new System.Windows.Forms.ComboBox();this.groupBox2 = new System.Windows.Forms.GroupBox();this.btnWriteWString = new System.Windows.Forms.Button();this.label14 = new System.Windows.Forms.Label();this.txtWriteValWString = new System.Windows.Forms.TextBox();this.label15 = new System.Windows.Forms.Label();this.txtWriteWStringLength = new System.Windows.Forms.TextBox();this.rtxtWriteValArray = new System.Windows.Forms.RichTextBox();this.btnWriteArray = new System.Windows.Forms.Button();this.label16 = new System.Windows.Forms.Label();this.btnWriteString = new System.Windows.Forms.Button();this.label18 = new System.Windows.Forms.Label();this.txtWriteValString = new System.Windows.Forms.TextBox();this.label19 = new System.Windows.Forms.Label();this.txtWriteStringLength = new System.Windows.Forms.TextBox();this.btnWriteBasic = new System.Windows.Forms.Button();this.label20 = new System.Windows.Forms.Label();this.txtWriteValBasic = new System.Windows.Forms.TextBox();this.cboWriteDataType = new System.Windows.Forms.ComboBox();this.label21 = new System.Windows.Forms.Label();this.label22 = new System.Windows.Forms.Label();this.txtDbNumber = new System.Windows.Forms.TextBox();this.panel1.SuspendLayout();this.groupBox1.SuspendLayout();this.groupBox2.SuspendLayout();this.SuspendLayout();// // panel1// this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.panel1.Controls.Add(this.cboSiemensPLCCategory);this.panel1.Controls.Add(this.label2);this.panel1.Controls.Add(this.btnDisconnect);this.panel1.Controls.Add(this.btnConnect);this.panel1.Controls.Add(this.txbPort);this.panel1.Controls.Add(this.label3);this.panel1.Controls.Add(this.txbIP);this.panel1.Controls.Add(this.label1);this.panel1.Location = new System.Drawing.Point(4, 4);this.panel1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);this.panel1.Name = "panel1";this.panel1.Size = new System.Drawing.Size(611, 87);this.panel1.TabIndex = 2;// // cboSiemensPLCCategory// this.cboSiemensPLCCategory.FormattingEnabled = true;this.cboSiemensPLCCategory.Location = new System.Drawing.Point(122, 52);this.cboSiemensPLCCategory.Name = "cboSiemensPLCCategory";this.cboSiemensPLCCategory.Size = new System.Drawing.Size(206, 20);this.cboSiemensPLCCategory.TabIndex = 14;// // label2// this.label2.AutoSize = true;this.label2.Location = new System.Drawing.Point(8, 52);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(95, 12);this.label2.TabIndex = 13;this.label2.Text = "西门子PLC型号:";// // btnDisconnect// this.btnDisconnect.Enabled = false;this.btnDisconnect.Location = new System.Drawing.Point(494, 14);this.btnDisconnect.Name = "btnDisconnect";this.btnDisconnect.Size = new System.Drawing.Size(91, 28);this.btnDisconnect.TabIndex = 5;this.btnDisconnect.Text = "断开连接";this.btnDisconnect.UseVisualStyleBackColor = true;this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click);// // btnConnect// this.btnConnect.Location = new System.Drawing.Point(371, 14);this.btnConnect.Name = "btnConnect";this.btnConnect.Size = new System.Drawing.Size(91, 28);this.btnConnect.TabIndex = 4;this.btnConnect.Text = "连接";this.btnConnect.UseVisualStyleBackColor = true;this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);// // txbPort// this.txbPort.Location = new System.Drawing.Point(238, 14);this.txbPort.Name = "txbPort";this.txbPort.Size = new System.Drawing.Size(90, 21);this.txbPort.TabIndex = 3;this.txbPort.Text = "102";// // label3// this.label3.AutoSize = true;this.label3.Location = new System.Drawing.Point(184, 17);this.label3.Name = "label3";this.label3.Size = new System.Drawing.Size(53, 12);this.label3.TabIndex = 2;this.label3.Text = "端口号:";// // txbIP// this.txbIP.Location = new System.Drawing.Point(62, 14);this.txbIP.Name = "txbIP";this.txbIP.Size = new System.Drawing.Size(106, 21);this.txbIP.TabIndex = 1;this.txbIP.Text = "192.168.1.10";// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(8, 17);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(53, 12);this.label1.TabIndex = 0;this.label1.Text = "IP地址:";// // rtxbDisplay// this.rtxbDisplay.Location = new System.Drawing.Point(4, 622);this.rtxbDisplay.Name = "rtxbDisplay";this.rtxbDisplay.ReadOnly = true;this.rtxbDisplay.Size = new System.Drawing.Size(943, 232);this.rtxbDisplay.TabIndex = 3;this.rtxbDisplay.Text = "";// // groupBox1// this.groupBox1.Controls.Add(this.btnReadWString);this.groupBox1.Controls.Add(this.label12);this.groupBox1.Controls.Add(this.txtReadValWString);this.groupBox1.Controls.Add(this.label13);this.groupBox1.Controls.Add(this.txtReadWStringLength);this.groupBox1.Controls.Add(this.rtxtReadValArray);this.groupBox1.Controls.Add(this.btnReadArray);this.groupBox1.Controls.Add(this.label9);this.groupBox1.Controls.Add(this.label8);this.groupBox1.Controls.Add(this.txtReadArrayLength);this.groupBox1