在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 采集PLC数据(.net core)

采集PLC数据(.net core)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:1.60M
  • 下载次数:132
  • 浏览次数:1096
  • 发布时间:2019-08-28
  • 实例类别:C#语言基础
  • 发 布 人:tqr123
  • 文件格式:.zip
  • 所需积分:20
 相关标签: PLC 数据 采集

实例介绍

【实例简介】可以用来与PLC通讯,采集PLC的数据

【实例截图】

from clipboard


from clipboard

【核心代码】


using System;
using System.Collections.Generic;
using System.Text;
using HslCommunication.Profinet.Siemens;
using HslCommunication;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace PLCRead
{

    class Storage
    {
        MySqlConnection conn;
        SiemensS7Net siemensTcpNet;
        #region
        bool bool_I0; // 读取I0.0的值
        bool bool_I1; // 读取I0.1的值
        bool bool_I2; // 读取I0.2的值
        bool bool_I3; // 读取I0.3的值
        bool bool_I4; // 读取I0.4的值
        bool bool_I5 ; // 读取I0.5的值
        bool bool_I6 ; // 读取I0.6的值
        bool bool_I7; // 读取I0.7的值
        bool bool_Q0; // 读取Q0.0的值
        bool bool_Q1; // 读取Q0.1的值
        bool bool_Q2; // 读取Q0.2的值
        bool bool_Q3; // 读取Q0.3的值
        bool bool_Q4; // 读取Q0.4的值
        bool bool_Q5 ; // 读取Q0.5的值
        bool bool_Q6 ; // 读取Q0.6的值
        bool bool_Q7 ; // 读取Q0.7的值

        #endregion

        #region  采集LED灯的状态
        int returnvalue_1;//读取returnvalue_1的值
        int returnvalue_2;//读取returnvalue_2的值
        int returnvalue_3;//读取returnvalue_3的值
        int returnvalue_4 ;//读取returnvalue_4的值
        int returnvalue_5;//读取returnvalue_5的值
        int returnvalue_6;//读取returnvalue_6的值
        #endregion
        OperateResult<string> read;

        #region 将采集的信息上传到Mysql
        //上传IO口的数据
        bool[] a;
        string[] c;
        //上传led的状态数据
        int[] b;
        string[]d;
        //上传PLC的基本信息
        string[] e;
        string[] f; 
        //上传各表的ID值
        int[] g;
        string[] h;
     

            #endregion
        public Storage(MySqlConnection connection, SiemensS7Net siemensPlc)
        {
            conn = connection;
            siemensTcpNet = siemensPlc;

            #region  采集IO口的数据

            bool_I0 = siemensTcpNet.ReadBool("I0.0").Content; // 读取I0.0的值
            bool_I1 = siemensTcpNet.ReadBool("I0.1").Content; // 读取I0.1的值
            bool_I2 = siemensTcpNet.ReadBool("I0.2").Content; // 读取I0.2的值
            bool_I3 = siemensTcpNet.ReadBool("I0.3").Content; // 读取I0.3的值
            bool_I4 = siemensTcpNet.ReadBool("I0.4").Content; // 读取I0.4的值
            bool_I5 = siemensTcpNet.ReadBool("I0.5").Content; // 读取I0.5的值
            bool_I6 = siemensTcpNet.ReadBool("I0.6").Content; // 读取I0.6的值
            bool_I7 = siemensTcpNet.ReadBool("I0.7").Content; // 读取I0.7的值
            bool_Q0 = siemensTcpNet.ReadBool("Q0.0").Content; // 读取Q0.0的值
            bool_Q1 = siemensTcpNet.ReadBool("Q0.1").Content; // 读取Q0.1的值
            bool_Q2 = siemensTcpNet.ReadBool("Q0.2").Content; // 读取Q0.2的值
            bool_Q3 = siemensTcpNet.ReadBool("Q0.3").Content; // 读取Q0.3的值
            bool_Q4 = siemensTcpNet.ReadBool("Q0.4").Content; // 读取Q0.4的值
            bool_Q5 = siemensTcpNet.ReadBool("Q0.5").Content; // 读取Q0.5的值
            bool_Q6 = siemensTcpNet.ReadBool("Q0.6").Content; // 读取Q0.6的值
            bool_Q7 = siemensTcpNet.ReadBool("Q0.7").Content; // 读取Q0.7的值

            #endregion

            #region  采集LED灯的状态
            returnvalue_1 = siemensTcpNet.ReadInt16("DB1.14").Content;//读取returnvalue_1的值
            returnvalue_2 = siemensTcpNet.ReadInt16("DB1.16").Content;//读取returnvalue_2的值
            returnvalue_3 = siemensTcpNet.ReadInt16("DB1.18").Content;//读取returnvalue_3的值
            returnvalue_4 = siemensTcpNet.ReadInt16("DB1.20").Content;//读取returnvalue_4的值
            returnvalue_5 = siemensTcpNet.ReadInt16("DB1.22").Content;//读取returnvalue_5的值
            returnvalue_6 = siemensTcpNet.ReadInt16("DB1.24").Content;//读取returnvalue_6的值

            #endregion

            #region 采集plc 的基本信息
            read = siemensTcpNet.ReadOrderNumber();

            #endregion

            #region 将采集的信息上传到Mysql
            //上传IO口的数据
            a = new bool[] { bool_I0, bool_I1, bool_I2, bool_I3, bool_I4, bool_I5, bool_I6, bool_I7, bool_Q0, bool_Q1, bool_Q2, bool_Q3, bool_Q4, bool_Q5, bool_Q6, bool_Q7 };
            c = new string[] { "I0_0", "I0_1", "I0_2", "I0_3", "I0_4", "I0_5", "I0_6", "I0_7", "Q0_0", "Q0_1", "Q0_2", "Q0_3", "Q0_4", "Q0_5", "Q0_6", "Q0_7", "time" };
            //上传led的状态数据
            b = new int[] { returnvalue_1, returnvalue_2, returnvalue_3, returnvalue_4, returnvalue_5, returnvalue_6 };
            d = new string[] { "return_led_value_1", "return_led_value_2", "return_led_value_3", "return_led_value_4", "return_led_value_5", "return_led_value_6", "time" };
            //上传PLC的基本信息
            e = new string[] { read.Content, "siemens", "S7-1200", "1", "tqr" };
            f = new string[] { "plc_num", "plc_brand", "plc_type", "embed_sys_id", "last_operate_name", "last_operate_time" };
            //上传各表的ID值
           
        }




        //增加时间戳
 



        public void Save()
        {
            DateTime[] date = { DateTime.Now };
            test2<bool, DateTime>(conn, "plc_io", c, a, date);
            int plc_io_id = Convert.ToInt32(GetID(conn));


            test2<int, DateTime>(conn, "plc_led_state", d, b, date);
            int plc_led_id = Convert.ToInt32(GetID(conn));

            test2<string, DateTime>(conn, "plc_info", f, e, date);
            int plc_info_id = Convert.ToInt32(GetID(conn));
           

            g = new int[] { plc_info_id, plc_io_id, plc_led_id };
            h = new string[] { "plc_id", "plc_io_id", "plc_led_state_id", "col_time" };
            test2<int, DateTime>(conn, "plc_data_bom", h, g, date);
        }
       
        




        //一种不同类型的参数存入Mysql
        public  void test1<T1>(MySqlConnection conn, string table, string[] name, T1[] data1)
        {

            string strname1 = name[0];
            string strname2 = "@"   name[0];
            for (int i = 1; i < name.Length; i  )
            {
                strname1  = ","   name[i];
                strname2  = ","   "@"   name[i];
            }

            string sql = " insert into"   "\t"   table   "("   strname1   ") values("   strname2   ")";
            try
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int count = 0;
                for (; count < data1.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data1[count]);
                }


                int result = cmd.ExecuteNonQuery();
                Console.WriteLine(table   "数据增加成功");
            }
            catch
            {
                Console.WriteLine(table   "数据增加失败");
            }
            return;
        }

        //两种不同类型的参数存入Mysql
        public  void test2<T1, T2>(MySqlConnection conn, string table, string[] name, T1[] data1, T2[] data2)
        {

            string strname1 = name[0];
            string strname2 = "@"   name[0];
            for (int i = 1; i < name.Length; i  )
            {
                strname1  = ","   name[i];
                strname2  = ","   "@"   name[i];
            }

            string sql = " insert into"   "\t"   table   "("   strname1   ") values("   strname2   ")";
            try
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int count = 0;
                for (; count < data1.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data1[count]);
                }
                for (; count < data1.Length   data2.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data2[count - data1.Length]);
                }

                int result = cmd.ExecuteNonQuery();
                Console.WriteLine(table   "数据增加成功");
            }
            catch
            {
                Console.WriteLine(table   "数据增加失败");
            }
            return;
        }

        //三种不同类型的参数存入Mysql
        public  void test3<T1, T2, T3>(MySqlConnection conn, string table, string[] name, T1[] data1, T2[] data2, T3[] data3)
        {

            string strname1 = name[0];
            string strname2 = "@"   name[0];
            for (int i = 1; i < name.Length; i  )
            {
                strname1  = ","   name[i];
                strname2  = ","   "@"   name[i];
            }

            string sql = " insert into"   "\t"   table   "("   strname1   ") values("   strname2   ")";
            try
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int count = 0;
                for (; count < data1.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data1[count]);
                }
                for (; count < data1.Length   data2.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data2[count - data1.Length]);
                }
                int result = cmd.ExecuteNonQuery();
                Console.WriteLine(table   "数据增加成功");
            }
            catch
            {
                Console.WriteLine(table   "数据增加失败");
            }
            return;



        }

        //四种不同类型的参数存入Mysql
        public  void test4<T1, T2, T3, T4>(MySqlConnection conn, string table, string[] name, T1[] data1, T2[] data2, T3[] data3, T4[] data4)
        {

            string strname1 = name[0];
            string strname2 = "@"   name[0];
            for (int i = 1; i < name.Length; i  )
            {
                strname1  = ","   name[i];
                strname2  = ","   "@"   name[i];
            }

            string sql = " insert into"   "\t"   table   "("   strname1   ") values("   strname2   ")";
            try
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int count = 0;
                for (; count < data1.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data1[count]);
                }
                for (; count < data1.Length   data2.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data2[count - data1.Length]);
                }
                for (; count < data1.Length   data2.Length   data3.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data3[count - data1.Length - data2.Length]);
                }
                for (; count < data1.Length   data2.Length   data3.Length   data4.Length; count  )
                {
                    cmd.Parameters.AddWithValue("@"   name[count], data4[count - data1.Length - data2.Length - data3.Length]);
                }


                int result = cmd.ExecuteNonQuery();
                Console.WriteLine(table   "数据增加成功");
            }
            catch
            {
                Console.WriteLine(table   "数据增加失败");
            }
            return;
        }

        //获取每个表的ID
        public  object GetID(MySqlConnection conn)
        {
            string sql = "select LAST_INSERT_ID()";
            try
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                object id = cmd.ExecuteScalar();
                Console.WriteLine("获取ID成功"   id);
                return id;
            }
            catch
            {
                Console.WriteLine("获取ID失败");
                return 0;
            }
        }
    }
}
#endregion


标签: PLC 数据 采集

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警