C#利用HslCommunication读取和写入汇川H3U

您所在的位置:网站首页 hslcommunication收费价格 C#利用HslCommunication读取和写入汇川H3U

C#利用HslCommunication读取和写入汇川H3U

2024-07-16 04:20| 来源: 网络整理| 查看: 265

一,window界面代码

namespace HslCommunicationForInovance {     partial class HslCommunicationForInovanceFrom     {         ///         /// 必需的设计器变量。         ///         private System.ComponentModel.IContainer components = null;

        ///         /// 清理所有正在使用的资源。         ///         /// 如果应释放托管资源,为 true;否则为 false。         protected override void Dispose(bool disposing)         {             if (disposing && (components != null))             {                 components.Dispose();             }             base.Dispose(disposing);         }

        #region Windows 窗体设计器生成的代码

        ///         /// 设计器支持所需的方法 - 不要修改         /// 使用代码编辑器修改此方法的内容。         ///         private void InitializeComponent()         {             this.label1 = new System.Windows.Forms.Label();             this.txtAddress = new System.Windows.Forms.TextBox();             this.label2 = new System.Windows.Forms.Label();             this.txtPiont = new System.Windows.Forms.TextBox();             this.label3 = new System.Windows.Forms.Label();             this.txtLogicalBit = new System.Windows.Forms.TextBox();             this.label4 = new System.Windows.Forms.Label();             this.txtStartAddress = new System.Windows.Forms.TextBox();             this.txtqty = new System.Windows.Forms.TextBox();             this.label5 = new System.Windows.Forms.Label();             this.richCurrent = new System.Windows.Forms.RichTextBox();             this.btnRead = new System.Windows.Forms.Button();             this.btnWrite = new System.Windows.Forms.Button();             this.cmbNumType = new System.Windows.Forms.ComboBox();             this.label7 = new System.Windows.Forms.Label();             this.richHistory = new System.Windows.Forms.RichTextBox();             this.checkBox1 = new System.Windows.Forms.CheckBox();             this.cmbCPU = new System.Windows.Forms.ComboBox();             this.label8 = new System.Windows.Forms.Label();             this.ComboBox1 = new System.Windows.Forms.ComboBox();             this.checkBox3 = new System.Windows.Forms.CheckBox();             this.label6 = new System.Windows.Forms.Label();             this.btnClear = new System.Windows.Forms.Button();             this.SuspendLayout();             //              // label1             //              this.label1.AutoSize = true;             this.label1.Location = new System.Drawing.Point(70, 26);             this.label1.Name = "label1";             this.label1.Size = new System.Drawing.Size(41, 12);             this.label1.TabIndex = 0;             this.label1.Text = "IP地址";             //              // txtAddress             //              this.txtAddress.Location = new System.Drawing.Point(149, 23);             this.txtAddress.Name = "txtAddress";             this.txtAddress.Size = new System.Drawing.Size(102, 21);             this.txtAddress.TabIndex = 1;             this.txtAddress.Text = "192.168.40.60";             //              // label2             //              this.label2.AutoSize = true;             this.label2.Location = new System.Drawing.Point(70, 85);             this.label2.Name = "label2";             this.label2.Size = new System.Drawing.Size(29, 12);             this.label2.TabIndex = 2;             this.label2.Text = "端口";             //              // txtPiont             //              this.txtPiont.Location = new System.Drawing.Point(149, 82);             this.txtPiont.Name = "txtPiont";             this.txtPiont.Size = new System.Drawing.Size(83, 21);             this.txtPiont.TabIndex = 3;             this.txtPiont.Text = "502";             //              // label3             //              this.label3.AutoSize = true;             this.label3.Location = new System.Drawing.Point(257, 85);             this.label3.Name = "label3";             this.label3.Size = new System.Drawing.Size(41, 12);             this.label3.TabIndex = 4;             this.label3.Text = "逻辑位";             //              // txtLogicalBit             //              this.txtLogicalBit.Location = new System.Drawing.Point(317, 82);             this.txtLogicalBit.Name = "txtLogicalBit";             this.txtLogicalBit.Size = new System.Drawing.Size(83, 21);             this.txtLogicalBit.TabIndex = 5;             this.txtLogicalBit.Text = "0";             //              // label4             //              this.label4.AutoSize = true;             this.label4.Location = new System.Drawing.Point(70, 119);             this.label4.Name = "label4";             this.label4.Size = new System.Drawing.Size(53, 12);             this.label4.TabIndex = 6;             this.label4.Text = "起始地址";             //              // txtStartAddress             //              this.txtStartAddress.Location = new System.Drawing.Point(149, 116);             this.txtStartAddress.Name = "txtStartAddress";             this.txtStartAddress.Size = new System.Drawing.Size(83, 21);             this.txtStartAddress.TabIndex = 7;             this.txtStartAddress.Text = "D50";             //              // txtqty             //              this.txtqty.Location = new System.Drawing.Point(317, 115);             this.txtqty.Name = "txtqty";             this.txtqty.Size = new System.Drawing.Size(83, 21);             this.txtqty.TabIndex = 8;             this.txtqty.Text = "15";             //              // label5             //              this.label5.AutoSize = true;             this.label5.Location = new System.Drawing.Point(267, 120);             this.label5.Name = "label5";             this.label5.Size = new System.Drawing.Size(29, 12);             this.label5.TabIndex = 9;             this.label5.Text = "个数";             //              // richCurrent             //              this.richCurrent.Location = new System.Drawing.Point(441, 31);             this.richCurrent.Name = "richCurrent";             this.richCurrent.Size = new System.Drawing.Size(318, 207);             this.richCurrent.TabIndex = 11;             this.richCurrent.Text = "";             //              // btnRead             //              this.btnRead.Location = new System.Drawing.Point(72, 206);             this.btnRead.Name = "btnRead";             this.btnRead.Size = new System.Drawing.Size(88, 32);             this.btnRead.TabIndex = 12;             this.btnRead.Text = "读取数据";             this.btnRead.UseVisualStyleBackColor = true;             this.btnRead.Click += new System.EventHandler(this.btnRead_Click);             //              // btnWrite             //              this.btnWrite.Location = new System.Drawing.Point(178, 206);             this.btnWrite.Name = "btnWrite";             this.btnWrite.Size = new System.Drawing.Size(84, 32);             this.btnWrite.TabIndex = 13;             this.btnWrite.Text = "写入数据";             this.btnWrite.UseVisualStyleBackColor = true;             this.btnWrite.Click += new System.EventHandler(this.btnWrite_Click);             //              // cmbNumType             //              this.cmbNumType.FormattingEnabled = true;             this.cmbNumType.Items.AddRange(new object[] {             "16整数",             "32整数",             "FLOAT",             "字符串",             "布尔"});             this.cmbNumType.Location = new System.Drawing.Point(316, 160);             this.cmbNumType.Name = "cmbNumType";             this.cmbNumType.Size = new System.Drawing.Size(83, 20);             this.cmbNumType.TabIndex = 16;             //              // label7             //              this.label7.AutoSize = true;             this.label7.Location = new System.Drawing.Point(248, 165);             this.label7.Name = "label7";             this.label7.Size = new System.Drawing.Size(53, 12);             this.label7.TabIndex = 17;             this.label7.Text = "数据类型";             //              // richHistory             //              this.richHistory.Location = new System.Drawing.Point(72, 269);             this.richHistory.Name = "richHistory";             this.richHistory.Size = new System.Drawing.Size(687, 169);             this.richHistory.TabIndex = 18;             this.richHistory.Text = "";             //              // checkBox1             //              this.checkBox1.AutoSize = true;             this.checkBox1.Checked = true;             this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;             this.checkBox1.Location = new System.Drawing.Point(262, 25);             this.checkBox1.Name = "checkBox1";             this.checkBox1.Size = new System.Drawing.Size(102, 16);             this.checkBox1.TabIndex = 19;             this.checkBox1.Text = "首地址从0开始";             this.checkBox1.UseVisualStyleBackColor = true;             //              // cmbCPU             //              this.cmbCPU.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;             this.cmbCPU.FormattingEnabled = true;             this.cmbCPU.Items.AddRange(new object[] {             "AM",             "H3U",             "H5U"});             this.cmbCPU.Location = new System.Drawing.Point(149, 160);             this.cmbCPU.Name = "cmbCPU";             this.cmbCPU.Size = new System.Drawing.Size(65, 20);             this.cmbCPU.TabIndex = 42;             //              // label8             //              this.label8.AutoSize = true;             this.label8.Location = new System.Drawing.Point(70, 55);             this.label8.Name = "label8";             this.label8.Size = new System.Drawing.Size(53, 12);             this.label8.TabIndex = 41;             this.label8.Text = "解析格式";             //              // ComboBox1             //              this.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;             this.ComboBox1.FormattingEnabled = true;             this.ComboBox1.Items.AddRange(new object[] {             "ABCD",             "BADC",             "CDAB",             "DCBA"});             this.ComboBox1.Location = new System.Drawing.Point(149, 53);             this.ComboBox1.Name = "ComboBox1";             this.ComboBox1.Size = new System.Drawing.Size(89, 20);             this.ComboBox1.TabIndex = 44;             //              // checkBox3             //              this.checkBox3.AutoSize = true;             this.checkBox3.Checked = true;             this.checkBox3.CheckState = System.Windows.Forms.CheckState.Checked;             this.checkBox3.Location = new System.Drawing.Point(262, 55);             this.checkBox3.Name = "checkBox3";             this.checkBox3.Size = new System.Drawing.Size(84, 16);             this.checkBox3.TabIndex = 43;             this.checkBox3.Text = "字符串颠倒";             this.checkBox3.UseVisualStyleBackColor = true;             //              // label6             //              this.label6.AutoSize = true;             this.label6.Location = new System.Drawing.Point(70, 168);             this.label6.Name = "label6";             this.label6.Size = new System.Drawing.Size(47, 12);             this.label6.TabIndex = 45;             this.label6.Text = "CPU型号";             //              // btnClear             //              this.btnClear.Location = new System.Drawing.Point(298, 206);             this.btnClear.Name = "btnClear";             this.btnClear.Size = new System.Drawing.Size(80, 32);             this.btnClear.TabIndex = 46;             this.btnClear.Text = "清空记录";             this.btnClear.UseVisualStyleBackColor = true;             this.btnClear.Click += new System.EventHandler(this.btnClear_Click);             //              // HslCommunicationForInovanceFrom             //              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;             this.ClientSize = new System.Drawing.Size(800, 463);             this.Controls.Add(this.btnClear);             this.Controls.Add(this.label6);             this.Controls.Add(this.ComboBox1);             this.Controls.Add(this.checkBox3);             this.Controls.Add(this.cmbCPU);             this.Controls.Add(this.label8);             this.Controls.Add(this.checkBox1);             this.Controls.Add(this.richHistory);             this.Controls.Add(this.label7);             this.Controls.Add(this.cmbNumType);             this.Controls.Add(this.btnWrite);             this.Controls.Add(this.btnRead);             this.Controls.Add(this.richCurrent);             this.Controls.Add(this.label5);             this.Controls.Add(this.txtqty);             this.Controls.Add(this.txtStartAddress);             this.Controls.Add(this.label4);             this.Controls.Add(this.txtLogicalBit);             this.Controls.Add(this.label3);             this.Controls.Add(this.txtPiont);             this.Controls.Add(this.label2);             this.Controls.Add(this.txtAddress);             this.Controls.Add(this.label1);             this.Name = "HslCommunicationForInovanceFrom";             this.Text = "HslCommunicationForInovance";             this.ResumeLayout(false);             this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;         private System.Windows.Forms.TextBox txtAddress;         private System.Windows.Forms.Label label2;         private System.Windows.Forms.TextBox txtPiont;         private System.Windows.Forms.Label label3;         private System.Windows.Forms.TextBox txtLogicalBit;         private System.Windows.Forms.Label label4;         private System.Windows.Forms.TextBox txtStartAddress;         private System.Windows.Forms.TextBox txtqty;         private System.Windows.Forms.Label label5;         private System.Windows.Forms.RichTextBox richCurrent;         private System.Windows.Forms.Button btnRead;         private System.Windows.Forms.Button btnWrite;         private System.Windows.Forms.ComboBox cmbNumType;         private System.Windows.Forms.Label label7;         private System.Windows.Forms.RichTextBox richHistory;         private System.Windows.Forms.CheckBox checkBox1;         private System.Windows.Forms.ComboBox cmbCPU;         private System.Windows.Forms.Label label8;         private System.Windows.Forms.ComboBox ComboBox1;         private System.Windows.Forms.CheckBox checkBox3;         private System.Windows.Forms.Label label6;         private System.Windows.Forms.Button btnClear;     } }

二,window界面后台代码

using HslCommunication.LogNet; using HslCommunication.Profinet.Inovance; using System; using System.Text; using System.Windows.Forms;

namespace HslCommunicationForInovance {     public partial class HslCommunicationForInovanceFrom : Form     {         private InovanceTcpNet inovanceH3UTcp = null;         private readonly ILogNet LogNet;

        public HslCommunicationForInovanceFrom()         {             InitializeComponent();         }

        private void btnRead_Click(object sender, EventArgs e)         {             if (!int.TryParse(this.txtPiont.Text, out int port))             {                 MessageBox.Show("Port input is wrong!");                 return;             }

            if (!byte.TryParse(this.txtLogicalBit.Text, out byte station))             {                 MessageBox.Show("Station input is wrong!");                 return;             }

            inovanceH3UTcp?.ConnectClose();             inovanceH3UTcp = new InovanceTcpNet(this.txtAddress.Text.Trim(), port, station);             inovanceH3UTcp.AddressStartWithZero = checkBox1.Checked;             inovanceH3UTcp.Series = (InovanceSeries)cmbCPU.SelectedIndex;             comboBox1_SelectedIndexChanged(null, new EventArgs());  // 设置数据服务             inovanceH3UTcp.IsStringReverse = checkBox3.Checked; //读取字符串是否倒序             inovanceH3UTcp.LogNet = LogNet;             var operateResult = inovanceH3UTcp.ConnectServer();             if (operateResult.IsSuccess)             {                 this.richHistory.Text = this.richHistory.Text + "\r\n" + "连接成功";             }             else             {                 this.richHistory.Text = this.richHistory.Text + "\r\n" + "连接失败";                 return;             }             try             {                 if (this.cmbNumType.Text == "字符串")                 {                     var operateResult1 = inovanceH3UTcp.ReadString(this.txtStartAddress.Text, ushort.Parse(this.txtqty.Text));

                    if (operateResult1.IsSuccess)                      { 

                        this.richCurrent.Text = "读取结果为:"+operateResult1.Content.ToString();

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                    }                     else                     {                         this.richCurrent.Text = "读取失败!";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }                 else if (this.cmbNumType.Text == "16整数")                 {                     var operateResult1 = inovanceH3UTcp.ReadUInt16(this.txtStartAddress.Text, ushort.Parse(this.txtqty.Text));

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = "读取结果为:" + operateResult1.Content[0].ToString();

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = "读取失败!";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }                 else if (this.cmbNumType.Text == "32整数")                  {                     var operateResult1 = inovanceH3UTcp.ReadUInt32(this.txtStartAddress.Text, ushort.Parse(this.txtqty.Text));

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = "读取结果为:" + operateResult1.Content[0].ToString();

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = "读取失败!";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }                 else                 {                     var operateResult1 = inovanceH3UTcp.ReadBool(this.txtStartAddress.Text, ushort.Parse(this.txtqty.Text));

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = "读取结果为:" + operateResult1.Content[0].ToString();

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = "读取失败!";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }             }             catch (Exception ex )             {                 Console.WriteLine(ex.Message);             }             finally             {                 inovanceH3UTcp.ConnectClose();             }         }

        private void btnWrite_Click(object sender, EventArgs e)         {             if (!int.TryParse(this.txtPiont.Text, out int port))             {                 MessageBox.Show("Port input is wrong!");                 return;             }

            if (!byte.TryParse(this.txtLogicalBit.Text, out byte station))             {                 MessageBox.Show("Station input is wrong!");                 return;             }

            inovanceH3UTcp?.ConnectClose();             inovanceH3UTcp = new InovanceTcpNet(this.txtAddress.Text.Trim(), port, station);             inovanceH3UTcp.AddressStartWithZero = checkBox1.Checked;             inovanceH3UTcp.Series = (InovanceSeries)cmbCPU.SelectedIndex;             comboBox1_SelectedIndexChanged(null, new EventArgs());  // 设置解析方式             inovanceH3UTcp.IsStringReverse = checkBox3.Checked; //读取字符串是否倒序(汇川必须是反的)             inovanceH3UTcp.LogNet = LogNet;             var operateResult = inovanceH3UTcp.ConnectServer();             if (operateResult.IsSuccess)             {

                this.richHistory.Text = this.richHistory.Text + "\r\n" + "连接成功";             }             else             {                 this.richHistory.Text = this.richHistory.Text + "\r\n" + "连接失败";                 return;             }             try             {                 if (this.cmbNumType.Text == "字符串")                 {                     var operateResult1 = inovanceH3UTcp.Write(this.txtStartAddress.Text, this.richCurrent.Text.Trim());

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}成功";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}失败";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }                 else if (this.cmbNumType.Text == "16整数")                 {                     var operateResult1 = inovanceH3UTcp.Write(this.txtStartAddress.Text, UInt16.Parse(this.richCurrent.Text.Trim()));

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}成功";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}失败";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }                 else if (this.cmbNumType.Text == "32整数")                 {                     var operateResult1 = inovanceH3UTcp.Write(this.txtStartAddress.Text, UInt32.Parse(this.richCurrent.Text.Trim()));

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}成功";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}失败";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }                 else if (this.cmbNumType.Text == "FLOAT")                 {                     var operateResult1 = inovanceH3UTcp.Write(this.txtStartAddress.Text, float.Parse(this.richCurrent.Text.Trim()));

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}成功";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}失败";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }                 else                 {                     var operateResult1 = inovanceH3UTcp.Write(this.txtStartAddress.Text, bool.Parse(this.richCurrent.Text.Trim()));

                    if (operateResult1.IsSuccess)                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}成功";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;                     }                     else                     {                         this.richCurrent.Text = $"写入{this.richCurrent.Text}到{this.txtStartAddress.Text}失败";

                        this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;

                        return;                     }                 }             }             catch (Exception ex)             {                 this.richCurrent.Text = ex.Message;                 this.richHistory.Text = this.richHistory.Text + "\r\n" + this.richCurrent.Text;             }             finally             {                 inovanceH3UTcp.ConnectClose();             }         }         ///         /// 设置解析格式         ///         ///         ///         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)         {             if (inovanceH3UTcp != null)             {                 switch (ComboBox1.SelectedIndex)                 {                     case 0: inovanceH3UTcp.DataFormat = HslCommunication.Core.DataFormat.ABCD; break;                     case 1: inovanceH3UTcp.DataFormat = HslCommunication.Core.DataFormat.BADC; break;                     case 2: inovanceH3UTcp.DataFormat = HslCommunication.Core.DataFormat.CDAB; break;//汇川的默认解析方式                     case 3: inovanceH3UTcp.DataFormat = HslCommunication.Core.DataFormat.DCBA; break;                     default: break;                 }             }         }

        private void btnClear_Click(object sender, EventArgs e)         {             this.richHistory.Text = "";         }     } }

三、界面参考

四、完毕,谢谢。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3