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
// 跨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
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read123a.IsSuccess)
{
JB123A = double.Parse(read123a.Content.ToString()) * 3;
textBox5.Text = JB123A.ToString();
}
else
{
textBox5.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read123b.IsSuccess)
{
JB123B = double.Parse(read123b.Content.ToString()) * 3;
textBox6.Text = JB123B.ToString();
}
else
{
textBox6.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read124a.IsSuccess)
{
JB124A = double.Parse(read124a.Content.ToString()) * 1.5;
textBox7.Text = JB124A.ToString();
}
else
{
textBox7.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read124b.IsSuccess)
{
JB124B = double.Parse(read124b.Content.ToString()) *1.5;
textBox8.Text = JB124B.ToString();
}
else
{
textBox8.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read408a.IsSuccess)
{
FS408A = double.Parse(read408a.Content.ToString()) * 1.25;
textBox9.Text =FS408A.ToString();
}
else
{
textBox9.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read408b.IsSuccess)
{
FS408B = double.Parse(read408b.Content.ToString()) * 1.25;
textBox10.Text = FS408B.ToString();
}
else
{
textBox10.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read414a.IsSuccess)
{
JB414A = double.Parse(read414a.Content.ToString()) * 3;
textBox11.Text = JB414A.ToString();
}
else
{
textBox11.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read414b.IsSuccess)
{
JB414B = double.Parse(read414b.Content.ToString()) * 3;
textBox12.Text = JB414B.ToString();
}
else
{
textBox12.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read414c.IsSuccess)
{
JB414C = double.Parse(read414c.Content.ToString()) * 3;
textBox13.Text = JB414C.ToString();
}
else
{
textBox13.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read415a.IsSuccess)
{
JB415A = double.Parse(read415a.Content.ToString()) * 2;
textBox14.Text = JB415A.ToString();
}
else
{
textBox14.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read415b.IsSuccess)
{
JB415B = double.Parse(read415b.Content.ToString()) * 2.15;
textBox15.Text = JB415B.ToString();
}
else
{
textBox15.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read603a.IsSuccess)
{
CD603A = double.Parse(read603a.Content.ToString()) * 0.4;
textBox16.Text = CD603A.ToString();
}
else
{
textBox16.Text = "读取失败";
}
}));
}
if (readEnable)
{
OperateResult
// 跨UI更新界面
Invoke(new Action(() =>
{
if (read603b.IsSuccess)
{
CD603B = double.Parse(read603b.Content.ToString()) * 0.4;
textBox17.Text = CD603B.ToString();
}
else
{
textBox17.Text = "读取失败";
}
}));
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Button1_Click(object sender, EventArgs e)
{
readEnable = true;
}
private void Button2_Click(object sender, EventArgs e)
{
readEnable = false;
timer1.Stop();
}
private void Timer1_Tick(object sender, EventArgs e)
{
label21.Text = System.DateTime.Now.ToLocalTime().ToString();
string ConnectString = "Server=localhost;User ID=root;Password=B7812078899;Database=kckjmes;";
MySqlConnection conn = new MySqlConnection(ConnectString);
conn.Open();//增删改查
MySqlCommand command = conn.CreateCommand();
command.CommandText = "INSERT INTO kckjmes.czdb(DATETIME,JB120,JB123A,JB123B,JB124A,JB124B,FS408A,FS408B,JB414A,JB414B,JB414C,JB415A,JB415B,CJ603A,CJ603B)VALUES(@DATETIME,@JB120T,@JB123AT,@JB123BT,@JB124AT,@JB124BT,@FS408AT,@FS408BT,@JB414AT,@JB414BT,@JB414CT,@JB415AT,@JB415BT,@CJ603AT,@CJ603BT)";
command.Parameters.AddWithValue("DATETIME", DateTime.Now);
command.Parameters.AddWithValue("JB120T", (int)double.Parse(textBox4.Text));
command.Parameters.AddWithValue("JB123AT", (int)double.Parse(textBox5.Text));
command.Parameters.AddWithValue("JB123BT", (int)double.Parse(textBox6.Text));
command.Parameters.AddWithValue("JB124AT", (int)double.Parse(textBox7.Text));
command.Parameters.AddWithValue("JB124BT", (int)double.Parse(textBox8.Text));
command.Parameters.AddWithValue("FS408AT", (int)double.Parse(textBox9.Text));
command.Parameters.AddWithValue("FS408BT", (int)double.Parse(textBox10.Text));
command.Parameters.AddWithValue("JB414AT", (int)double.Parse(textBox11.Text));
command.Parameters.AddWithValue("JB414BT", (int)double.Parse(textBox12.Text));
command.Parameters.AddWithValue("JB414CT", (int)double.Parse(textBox13.Text));
command.Parameters.AddWithValue("JB415AT", (int)double.Parse(textBox14.Text));
command.Parameters.AddWithValue("JB415BT", (int)double.Parse(textBox15.Text));
command.Parameters.AddWithValue("CJ603AT", (int)double.Parse(textBox16.Text));
command.Parameters.AddWithValue("CJ603BT", (int)double.Parse(textBox17.Text));
command.ExecuteNonQuery();
conn.Close();
conn.Dispose();
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();
}
}
}
}
}