【C#使用S7.NET库读取和写入西门子PLC变量】

C#使用S7.NET库读取和写入西门子PLC变量

前言

本来想用Wincc的接口给读和写Wincc,但是速度实在太感人了,所以不如直接读和写PLC的变量,这种方式速度瞬间快了不知道多少倍(经测试4000个变量几乎瞬间完成,应该1s左右,人感知不出来)。

网上找了好多资料,都写得比较分散。特别是关于字符串的读取和写入,写了好久才读写正常,把两种方式的代码分享出来。

使用S7.NET库读取

csharp 复制代码
        //使用S7.NET库___读取PLC变量
        public static string[] ReadPLC(int DbAddress,int StartByteAdr,int ByteCount,int Type)
        {
            int i, j, k;
            int types = 2;
            int count;
            byte[] bytes=new byte[1000];
            //返回值当前设置最大1000,可自由设置
            string[] result=new string[1000];

            #region plc系列号+机架号+插槽号
            Plc MyPLC = new Plc(CpuType.S71500, "10.168.4.2", 0, 1);
            MyPLC.Open();
            #endregion

            //读取的是int
            if (Type == 1)
            {
                types = 2;
            }
            //读取的是real
            else if (Type == 2)
            {
                types = 4;
            }
            //读取的是String
            else if (Type == 3)
            {
                types = 256;
            }
            //读取的是bool
            else if(Type == 0)
            {
                types = 1;
            }

            try
            {
                j = 0;
                if (types == 1)
                {
                    //读取BOOL
                    for (i = 0; i < Convert.ToInt16(ByteCount / 8 + 1); i++)
                    {
                        for (k = 0; k <= 7; k++)
                        {
                            result[j + k] = MyPLC.Read("DB" + DbAddress.ToString() + "." + "DBX" + (StartByteAdr + i).ToString() + "." + k.ToString()).ToString();
                            if (result[j + k] == "True")
                            {
                                result[j + k] = "1";
                            }
                            else
                            {
                                result[j + k] = "0";
                            }
                        }
                        j = j + 8;
                    }
                }
                else if (types == 2 || types == 4)
                {
                    //读取字节
                    bytes = MyPLC.ReadBytes(DataType.DataBlock, DbAddress, StartByteAdr, ByteCount * types);
                    for (i = 0; i <= (ByteCount * types - types); i = i + types)
                    {
                        if (types == 2)
                        {
                            result[j] = S7.Net.Types.Int.FromByteArray(bytes.Skip(i).Take(types).ToArray()).ToString();
                        }
                        if (types == 4)
                        {
                            result[j] = S7.Net.Types.Real.FromByteArray(bytes.Skip(i).Take(types).ToArray()).ToString();
                        }
                        j++;
                    }
                }
                else if (types == 256)
                {
                    //读取字符串
                    //读取字符串的长度
                    for (i=0; i< ByteCount; i++)
                    {
                        count = (byte)MyPLC.Read(DataType.DataBlock, DbAddress, StartByteAdr+i*22 + 1, VarType.Byte, 1);
                        bytes = MyPLC.ReadBytes(DataType.DataBlock, DbAddress, StartByteAdr+i*22 + 2, count);
                        result[j] = Encoding.Default.GetString(bytes);
                        j++;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("错误:读取PLC变量错误!");
            }

            MyPLC.Close();
            return result;
        }

使用S7.NET库写入

csharp 复制代码
        //使用S7.NET库___写入PLC变量
        public static void WritePLC(int DbAddress, int StartByteAdr, int ByteCount, int Type, string[] strings)
        {
            int i,j;
            short ints;
            float floats;
            int types = 2;
            //最多1000个字节写入,可自由设置
            byte[] bytes = new byte[1000];

            #region plc系列号+机架号+插槽号
            Plc MyPLC = new Plc(CpuType.S71500, "10.168.4.2", 0, 1);
            MyPLC.Open();
            #endregion

            //写入的是int
            if (Type == 1)
            {
                types = 2;
            }
            //写入的是real
            else if (Type == 2)
            {
                types = 4;
            }
            //写入的字符串
            else if (Type == 3)
            {
                types = 256;
            }
            //读取的是bool
            else if (Type == 0)
            {
                types = 1;
            }

            try
            {
                if (types == 2)
                {
                    for (i = 0; i <= ByteCount-1; i++)
                    {
                        ints = Convert.ToInt16(strings[i]);
                        MyPLC.Write(DataType.DataBlock, DbAddress, StartByteAdr + i * types, ints);            
                    }
                }
                else if (types == 4)
                {
                    for (i = 0; i <= ByteCount-1; i++)
                    {
                        floats = Convert.ToSingle(strings[i]);
                        MyPLC.Write(DataType.DataBlock, DbAddress, StartByteAdr + i * types, floats);
                    }
                }
                else if (types == 256)
                {
                    for (i = 0; i <= ByteCount - 1; i++)
                    {
                        if (strings[i]!=null)
                        {
                            //字符串转换成编码再转成字节
                            bytes = Encoding.Default.GetBytes(strings[i]);
                            //写入字节长度,用字节的数据类型
                            MyPLC.Write(DataType.DataBlock, DbAddress, StartByteAdr + i * 22 + 1, (byte)bytes.Length);
                            //依次写入
                            MyPLC.Write(DataType.DataBlock, DbAddress, StartByteAdr + i * 22 + 2, bytes);
                        }
                        else
                        {
                            //字符串转换成编码再转成字节
                            bytes = Encoding.Default.GetBytes("");
                            //写入字节长度,用字节的数据类型
                            MyPLC.Write(DataType.DataBlock, DbAddress, StartByteAdr + i * 22 + 1, (byte)bytes.Length);
                            //依次写入
                            MyPLC.Write(DataType.DataBlock, DbAddress, StartByteAdr + i * 22 + 2, bytes);
                        }
                    }
                }
                else if (types == 1)
                {
                    for (i=0; i <= ByteCount - 1; i=i+8)
                    {
                        for (j=0;j<=7 && i+j<= ByteCount - 1; j++)
                        {
                            if (strings[i + j]=="1")
                            {
                                MyPLC.WriteBit(DataType.DataBlock, DbAddress, StartByteAdr+i/8, j,true);
                            }
                            else
                            {
                                MyPLC.WriteBit(DataType.DataBlock, DbAddress, StartByteAdr+i/8, j, false);
                            }                            
                        }                       
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("写入PLC数据错误");
            }
            
            MyPLC.Close();
        }
相关推荐
爱喝一杯白开水8 分钟前
SpringMVC从入门到上手-全面讲解SpringMVC的使用.
java·spring·springmvc
王景程18 分钟前
如何测试短信接口
java·服务器·前端
zhang23839061541 小时前
IDEA add gitlab account 提示
java·gitlab·intellij-idea·idea
牛马baby1 小时前
Java高频面试之并发编程-07
java·开发语言·面试
卓怡学长2 小时前
w304基于HTML5的民谣网站的设计与实现
java·前端·数据库·spring boot·spring·html5
YONG823_API2 小时前
深度探究获取淘宝商品数据的途径|API接口|批量自动化采集商品数据
java·前端·自动化
界面开发小八哥2 小时前
界面开发框架DevExpress XAF实践:如何在Blazor项目中集成.NET Aspire?(二)
.net·界面控件·devexpress·ui开发·xaf
yzhSWJ2 小时前
Spring Boot中自定义404异常处理问题学习笔记
java·javascript
盖世英雄酱581362 小时前
分布式ID所有生成方案
java·后端
敖云岚3 小时前
【AI】SpringAI 第五弹:接入千帆大模型
java·大数据·人工智能·spring boot·后端