您现在的位置是:首页 >其他 >C#通过S7.NET库与西门子PLC数据交互网站首页其他

C#通过S7.NET库与西门子PLC数据交互

yongshao8 2023-06-10 08:00:03
简介C#通过S7.NET库与西门子PLC数据交互

C#通过S7.NET库实现与西门子PLC数据交互

在这里插入图片描述

PLC与C#数据类型对应

Bit  	=> 	bool
Byte 	=> 	byte
word 	=> 	ushort
DWord 	=> 	uint
Int 	=> 	short
DInt 	=> 	int
Real 	=> 	float
LReal 	=> 	double
String 	=> 	string

VS添加S7.NET库引用


1、右键项目名,选择图中【管理NuGet程序包】
在这里插入图片描述2、在搜索框输入【S7netplus】,我安装的是第一个,安装完成引用里面会自动添加【S7.NET】引用。

连接PLC

                this.myPlc.Open();
                if (this.myPlc.PlcObject.IsConnected)
                {
                    this.btnOpen.Enabled = false;
                    this.btnClose.Enabled = true;
                    this.tsslblIsConnected.BackColor = Color.Green;
                    this.tsslblIsConnected.Text = "PLC已连接";

                    this.tsslblIP.Text = this.myPlc.PlcObject.IP.ToString();

                    this.btnRead.Enabled = true;
                    this.btnWrite.Enabled = true;

                    switch (this.myPlc.PlcObject.CPU)
                    {
                        case CpuType.S7300:
                            this.tsslblCpuType.Text = "S7-300";
                            break;
                        case CpuType.S7400:
                            this.tsslblCpuType.Text = "S7-400";
                            break;
                        case CpuType.S71200:
                            this.tsslblCpuType.Text = "S7-1200";
                            break;
                        case CpuType.S71500:
                            this.tsslblCpuType.Text = "S7-1500";
                            break;
                    }
                }

对于S7-1200和S7-1500,默认为机架= 0,槽位= 0;对于S7-300和S7-400是默认的机架= 0,槽位= 2;端口号默认为102。

断开PLC

                this.myPlc.Close();
                if (!this.myPlc.PlcObject.IsConnected)
                {
                    this.btnOpen.Enabled = true;
                    this.btnClose.Enabled = false;
                    this.tsslblIsConnected.BackColor = Color.Red;
                    this.tsslblIsConnected.Text = "PLC未连接";

                    this.tsslblIP.Text = "***********";
                    this.tsslblCpuType.Text = "******";

                    this.btnRead.Enabled = false;
                    this.btnWrite.Enabled = false;
                }

读取单个数据

            if (this.tabControlRead.SelectedTab == this.tabControlRead.TabPages[0])  //读取单个数据
            {
                try
                {
                    this.txtReadResult.Text = DateTime.Now.ToString() + ":" + this.myPlc.Read(this.txtReadSingleAdr.Text.Trim().ToUpper()).ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("读取单个数据出现错误!" + ex.Message, "提示信息");
                }
            }

我这里把输入的地址的转换为大写字母,如果不转换,输入小写字母会出现报错。

读取数组数据

                    switch (this.cboReadVarType.Text.Trim())
                    {
                        case "Bit":
                            foreach (var item in (System.Collections.BitArray)this.myPlc.Read(dataType, db, startByteAdr, VarType.Bit, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "Byte":
                            foreach (var item in (byte[])this.myPlc.Read(dataType, db, startByteAdr, VarType.Byte, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "Word":
                            foreach (var item in (ushort[])this.myPlc.Read(dataType, db, startByteAdr, VarType.Word, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "DWord":
                            foreach (var item in (uint[])this.myPlc.Read(dataType, db, startByteAdr, VarType.DWord, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "Int":
                            foreach (var item in (short[])this.myPlc.Read(dataType, db, startByteAdr, VarType.Int, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "DInt":
                            foreach (var item in (int[])this.myPlc.Read(dataType, db, startByteAdr, VarType.DInt, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "Real":
                            foreach (var item in (float[])this.myPlc.Read(dataType, db, startByteAdr, VarType.Real, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "LReal":
                            foreach (var item in (double[])this.myPlc.Read(dataType, db, startByteAdr, VarType.LReal, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "String":
                            foreach (var item in (string)this.myPlc.Read(dataType, db, startByteAdr, VarType.String, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "S7String":
                            foreach (var item in (string[])this.myPlc.Read(dataType, db, startByteAdr, VarType.S7String, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "S7WString":
                            foreach (var item in (string[])this.myPlc.Read(dataType, db, startByteAdr, VarType.S7WString, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "DateTime":
                            foreach (var item in (DateTime[])this.myPlc.Read(dataType, db, startByteAdr, VarType.DateTime, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                        case "DateTimeLong":
                            foreach (var item in (DateTime[])this.myPlc.Read(dataType, db, startByteAdr, VarType.DateTimeLong, varCount, bitAdr))
                            {
                                this.txtReadResult.Text += item.ToString() + " ";
                            }
                            break;
                    }

读取BIT数组时我是使用【System.Collections.BitArray】强制转换

修改单个数据

            if (this.tabControlWrite.SelectedTab == this.tabControlWrite.TabPages[0])   //写入单个数据
            {
                try
                {                   
                    switch (this.cboWriteSingleVarType.Text.Trim())
                    {
                        case "Bit":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToBoolean(this.txtWriteData.Text.Trim()));
                            break;
                        case "Byte":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToByte(this.txtWriteData.Text.Trim()));
                            break;
                        case "Word":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToUInt16(this.txtWriteData.Text.Trim()));
                            break;
                        case "Int":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToInt16(this.txtWriteData.Text.Trim()));
                            break;
                        case "DWord":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToUInt32(this.txtWriteData.Text.Trim()));
                            break;
                        case "DInt":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToInt32(this.txtWriteData.Text.Trim()));
                            break;
                        case "Real":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToSingle(this.txtWriteData.Text.Trim()));
                            break;
                        case "LReal":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToDouble(this.txtWriteData.Text.Trim()));
                            break;
                        case "String":
                        case "S7String":
                        case "S7WString":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), this.txtWriteData.Text.Trim());                         
                            break;
                        case "DateTime":
                        case "DateTimeLong":
                            this.myPlc.Write(this.txtWriteSingleAdr.Text.Trim().ToUpper(), Convert.ToDateTime(this.txtWriteData.Text.Trim()));
                            break;
                    }                                           
                }
                catch (Exception ex)
                {
                    MessageBox.Show("写入单个数据出现错误!" + ex.Message, "提示信息");
                }
            }
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。