C# Snap7 实现对西门子PLC的读写(本篇主要对系统诊断缓存区内容进行读取)

                        Snap 7 使用C# 实现对西门子PLC的读写

          摘要: 本篇主要针对PLC的错误日志进行操作,此目的是方便在没有电气工程师在现场的情况下 快速定位错误位置,帮助节约ShoutDown 时间 提升企业产量。使用系统环境Windows 操作系统,.Net 4.5以上版本 ,VS2015以上。

1.Snap 7 基本知识

Snap 7 是一款基于以太网传输的开源平台和接口并且专门针对西门子S7系列PLC。最新款的S7-1200/1500,SIMAICS 驱动,老款的S7-200,和小型的LOGO 产品 0BA7/0BA8也部分支。

Snap 7 弥补了OPC的一些不足,在传输大量数据且实时性要求高的场合Snap 7 具有很强的优势。并且Snap 7可以跨平台(OPC UA也可) 

主要特征如下:

        1.原生多架构设计(32/64位)

        2.独立于平台,目前支持Windows(从NT 4.0到Windows 8),Linux,BSD,Oracle Solaris 11和Mac OSX。

        3.多CPU支持:Intel和AMD i386 / x86_64,ARM,Sun Sparc,Mips。

        4.不依赖于任何第三方库,无需安装,零配置。

        5.用于性能优化的三种不同的本机线程模型:Win32线程/ Posix线程/ Solaris 11线程。

        6.两种数据传输模型:经典同步和异步。

        7.两种数据流模型:轮询和未经请求(PLC在需要时传输数据)

        8.两个专用端口:Settimino和Moka7允许您使用Arduino或Android手机与S7 PLC通信。

其次Snap 7 还有如下优点:

        1.接口易于使用

        2.可以被多种语言调用(C/C++,.Net/Monon,Pascal,LabView,Python,Node.js)

2.Snap 7 基本命令

    1.Snap 7 命令可分为一下几种

 Data Read/Write

 Cyclic Data Read/Write

 Directory info

 System Info

 Blocks move

 PLC Control

 Date and Time

 Security

 Programming

 2. S7协议功能目录

  Data I/O (also via multivariable read/write) Read/Write DB, Mk, IPI, IPQ, Timers, and Counters.

Directory List Blocks, List Blocks of Type, Block info.

Control  Run/Stop, Compress and Copy Ram to Rom.

Date and Time Get/Set PLC Date and Time.

System Info Read SZL 

Security Get/Set session password .

3.SSL 基本信息

1.SSL(系统状态列表) 概念

    所有的诊断事件按它们出现的先后次序登陆在诊断缓冲区中。可以通过模块信息和诊断缓冲区等查看系统状态列表的信息。为了快速定位故障位置,帮助企业减少宕机时间提升产量,对系统诊断缓冲区的监视是十分有必要的。传统的OPC AE 配置步骤繁琐,具有限制性。使用Snap 7调用接口即可获取SSL内容。

SSL-IDW#16#xyA0 部分列表的表头结构如下:

内容 含义
SZL-ID

W#16#xyA0:可在当前处于激活状态的操作模式下提供的目录;

INDEX 仅对于SZL-IDW#16#xy01A0 最新项目数
LENTHDR W#16#0014 一个数据记录长度为10个字(20个 字节)
N_DR 数据记录数目

诊断信息的数据具有一下结构:

名称 长度 含义
ID 1个字 事件ID
info 5个字 有关该事件及其后果的信息
time 4个字 事件的时间戳

2.接口函数

Cli_ReadSZL Reads a partial list of given ID and Index.

Cli_ReadSZLList Reads the list of partial lists available in the CPU.

3.C# 关键代码

 private void ReadSzl_bt_Click(object sender, EventArgs e)
        {
            ReadSZL();
        }



        void ReadSZL()
        {
            S7Client.S7SZL SZL = new S7Client.S7SZL();
            Int32 Size = 16380; // Declare our Buffer Size  
            SZL.Data = new byte[Size];  //长度参考 S7SZL struct 定义的长度 

            label19.Text = "0";
            TxtDump.Text = "";

            int Result = Client.ReadSZL(Convert.ToInt32(TxtSZLID.Text), Convert.ToInt32(TxtSZLIndex.Text), ref SZL, ref Size);

            ShowResult(Result);

            if (Result == 0)
            {
                label19.Text = Size.ToString();
                // HexDump(TxtDump, SZL.Data, Size);
                // HexDump(SZL.Data, Size);
                Dump(TxtDump, SZL.Data, Size);
                Dump(SZL.Data, Size);
                ShowSSLID();
            }

        }
  private void ShowResult(int Result)
        {
            // This function returns a textual explaination of the error code
            TextError.Text = Client.ErrorText(Result) + " (" + Client.ExecTime().ToString() + " ms)";
        }
    private void Dump(TextBox Box, byte[] Buffer, int Size)
        {
            // Declaration separated from the code for readability
            int y;
            Box.Text = "";
            y = 0;
            for (int c = 0; c < Size; c++)
            {
                String S = Convert.ToString(Buffer[c], 16);
                if (S.Length == 1) S = "0" + S;
                Box.Text = Box.Text + S + " ";
                y++;
                if (y == 20)
                {
                    y = 0;
                    Box.Text = Box.Text + (char)13 + (char)10;
                }
            }
        }
   public void ShowSSLID()
        {
            string x5;
            string x6;
            if (SZL_ReadString.Count > 0)
            {
                for (int i = 0; i < SZL_ReadString.Count; i++)
                {
                    x5 = SZL_IDText.SZL_IDString(DataTrasfer(SZL_ReadString[i].SZLIDHi, SZL_ReadString[i].SZLIDLo));
                    x6 = SZL_ReadString[i].SZLYear + "年" + SZL_ReadString[i].SZLMonth + "月" + SZL_ReadString[i].SZLDay + "日" + SZL_ReadString[i].SZLHour
                        + "时" + SZL_ReadString[i].SZLMindute + "分" + SZL_ReadString[i].SZLSecound + "秒" + SZL_ReadString[i].SZLMSecoundHi + SZL_ReadString[i].SZLMSecondLo;
                    SZL_tXT.Text = SZL_tXT.Text + SZL_ReadString[i].SZLIDHi.ToString() +
                        SZL_ReadString[i].SZLIDLo.ToString() + " " + x5 + " " + x6 + "\r\n";

                }
            }

        }

 4.总结

虽然 SIMATIC NET OPC AE 也可以读出SSL列表内容,但是受到OPC本身的限制并且SIMATIC NET OPC 配置比较复杂如果在客户现场非常步方便,我相信Snap 7 会是最佳的方式。

5.参考资料

 1.用于%20S7300_400系统和%20标准功能的系统软件%20卷1_2.pdf

2.《西门子工业通信网络组态编程与故障诊断》

猜你喜欢

转载自blog.csdn.net/Paul22101574/article/details/81583198