jingshengzhi 发表于 2023-5-25 10:14:37

用C#开发软件监测并记录西门子1500plc的I/O点状态

用C#开发软件监测并记录西门子1500plc的I/O点状态

chatGPT_小智 发表于 2023-5-25 10:14:57

好的,这个任务需要用到C#编写程序,并使用西门子提供的通信库来与PLC进行通信。以下是参考代码:

using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;

namespace PLCMonitoring
{
    class Program
    {
      static void Main(string[] args)
      {
            //创建PLC连接实例
            Plc plc = new Plc(CpuType.S71200, "192.168.0.1", 0, 1);

            //打开PLC连接
            plc.Open();

            //检查连接状态
            if (!plc.IsConnected)
            {
                Console.WriteLine("无法连接到PLC");
                return;
            }

            //循环监测和记录I/O点状态
            while(true)
            {
                //读取I/O点状态
                bool input0 = plc.Read("I0.0");
                bool input1 = plc.Read("I0.1");
                bool output0 = plc.Read("Q0.0");
                bool output1 = plc.Read("Q0.1");

                //记录I/O点状态
                Console.WriteLine("Input0: {0}, Input1: {1}, Output0: {2}, Output1: {3}", input0, input1, output0, output1);

                //延时1秒
                System.Threading.Thread.Sleep(1000);
            }

            //关闭PLC连接
            plc.Close();
      }
    }
}

以上代码通过创建Plc类实例,使用plc.Open()方法打开PLC连接,并使用plc.IsConnected属性检查连接状态。然后在循环中使用plc.Read()方法读取I/O点状态,并通过Console.WriteLine()方法将状态记录到控制台中。最后使用plc.Close()方法关闭PLC连接。在实际情况下,可以将I/O点状态记录到文件、数据库或云平台中进行保存和分析。

jingshengzhi 发表于 2023-5-25 10:17:24


用C#开发软件监测并记录西门子1500plc的I/O点状态,并用画面显示出来

chatGPT_小智 发表于 2023-5-25 10:17:54

jingshengzhi 发表于 2023-05-25 10:17
用C#开发软件监测并记录西门子1500plc的I/O点状态,并 ...

好的,这个任务需要用到C#编写程序,并使用西门子提供的通信库来与PLC进行通信,同时也需要使用相关的画面显示控件来实现界面显示。以下是参考代码:

using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;
using System.Windows.Forms;

namespace PLCMonitoring
{
    class Program
    {
      static Plc plc;
      static Label input0Label, input1Label, output0Label, output1Label;

      
      static void Main(string[] args)
      {
            //创建PLC连接实例
            plc = new Plc(CpuType.S71200, "192.168.0.1", 0, 1);
            //打开PLC连接
            plc.Open();
            //检查连接状态
            if (!plc.IsConnected)
            {
                Console.WriteLine("无法连接到PLC");
                return;
            }

            //创建主窗体
            Form mainForm = new Form();
            mainForm.Text = "PLC Monitoring";

            //添加控件
            input0Label = new Label();
            input0Label.Text = "Input0: ";
            input0Label.Location = new System.Drawing.Point(10, 10);
            mainForm.Controls.Add(input0Label);

            input1Label = new Label();
            input1Label.Text = "Input1: ";
            input1Label.Location = new System.Drawing.Point(10, 40);
            mainForm.Controls.Add(input1Label);

            output0Label = new Label();
            output0Label.Text = "Output0: ";
            output0Label.Location = new System.Drawing.Point(10, 70);
            mainForm.Controls.Add(output0Label);

            output1Label = new Label();
            output1Label.Text = "Output1: ";
            output1Label.Location = new System.Drawing.Point(10, 100);
            mainForm.Controls.Add(output1Label);

            //创建定时器
            Timer timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

            //显示窗体
            Application.Run(mainForm);

            //关闭PLC连接
            plc.Close();
      }

      static void timer_Tick(object sender, EventArgs e)
      {
            //循环监测和记录I/O点状态
            bool input0 = plc.Read("I0.0");
            bool input1 = plc.Read("I0.1");
            bool output0 = plc.Read("Q0.0");
            bool output1 = plc.Read("Q0.1");

            //更新控件显示
            input0Label.Text = "Input0: " + input0.ToString();
            input1Label.Text = "Input1: " + input1.ToString();
            output0Label.Text = "Output0: " + output0.ToString();
            output1Label.Text = "Output1: " + output1.ToString();
      }
    }
}

以上代码通过使用Windows.Forms命名空间提供的控件,在主窗体中创建了四个Label控件来分别显示4个I/O点的状态。同时,使用定时器每隔1秒钟读取一次PLC的I/O点状态,并更新控件的显示。通过应用程序的运行和窗口的显示,可以实现I/O点状态的监测和画面显示。

jingshengzhi 发表于 2023-5-25 10:21:27

wincc记录plc所有I/O点的动作,然后行成表格

chatGPT_小智 发表于 2023-5-25 10:21:41

jingshengzhi 发表于 2023-05-25 10:21
wincc记录plc所有I/O点的动作,然后行成表格 ...

好的,这个任务可以通过WinCC的Historian功能来实现。下面是具体的实现步骤:

1. 在WinCC中创建一个Historian Tag Group,并将要记录的PLC I/O点全部添加到Tag Group中。

2. 在WinCC Runtime Manager中启用Historian功能,在Historian Manager中配置Historian Server和Tag Group。

3. 在WinCC Graphics Designer中创建一个表格控件,并绑定Historian Tag Group中的数据点。

4. 在页面加载时,将表格控件绑定到Historian数据源,并指定查询条件,使得表格中显示的数据只包含需要记录的PLC I/O点。

5. 当PLC I/O点状态有变化时,Historian会自动记录该数据点的数值变化。在图形页面上,表格控件会自动刷新并显示最新的Historian记录。

以上是基本的实现步骤,具体的实现过程可能会因具体环境而异。请参考WinCC的相关手册和文档,详细了解如何使用Historian功能来记录PLC数据。
页: [1]
查看完整版本: 用C#开发软件监测并记录西门子1500plc的I/O点状态