C#采集三菱QPLC数据,写入MYSQL数据库

本文档演示了一个C#应用程序,用于从三菱QPLC设备读取数据(如D50-D62寄存器),并将这些数据实时写入MYSQL数据库。程序通过HslCommunication库实现PLC通讯,使用线程进行后台数据读取,同时UI界面显示读取结果,并定时更新数据库记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication;//引用采集模块
using HslCommunication.Profinet.Melsec;
using System.Threading;
using MySql.Data.MySqlClient;//引用 MYSQL
using System.Data.SqlClient;

namespace KCKJMES
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            thread = new Thread(new ThreadStart(ReadMethod));//线程实例化
            thread.IsBackground = true;
            thread.Start();//线程开始
            string ConnectString = "Server=localhost;User ID=root;Password=B7812078899;Database=kckjmes;";//MYsql数据库连接
            MySqlConnection conn = new MySqlConnection(ConnectString);
            conn.Open();//增删改查
        }
        double JB120,JB123A,JB123B,JB124A,JB124B,FS408A,FS408B,JB414A,JB414B,JB414C,JB415A,JB415B,CD603A,CD603B;
        int ID = 0;
        private void timer2_Tick(object sender, EventArgs e)
        {
            string ConnectString = "Server=localhost;User ID=root;Password=B7812078899;Database=kckjmes;";
            MySqlConnection connection = new MySqlConnection(ConnectString);
            connection.Open();
            try
            {
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT * FROM czdb";
                MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Clone();
                }
            }
        }

                private void button4_Click(object sender, EventArgs e)
        {
            timer2.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Start();

        }

        private Thread thread;
        private bool readEnable = false;
        private void ReadMethod()
        {
            MelsecMcNet melsec_net = new MelsecMcNet("192.168.0.7", 6000);
            melsec_net.SetPersistentConnection();         // 设置长连接
            while (true)
            {
                Thread.Sleep(1000);  // 决定了一秒读20次

                if (readEnable)
                {
                    // 启动了读操作
                    OperateResult<short> read120 = melsec_net.ReadInt16("D50");
                    // 跨UI更新界面
                    Invoke(new Action(() =>
                    {
                        if (read120.IsSuccess)
                        {
                            JB120 = double.Parse(read120.Content.ToString())*1.5;
                            textBox4.Text = JB120.ToString();
                         }
                        else
                        {
                            textBox4.Text = "读取失败";
                        }
                    }));
                }
                if (readEnable)
                {
                    OperateResult<short> read123a = melsec_net.ReadInt16("D51");
                    // 跨UI更新界面
                    Invoke(n

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值