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