引用一下西门子plc、sql server、线程用到的命名空间:S7.Net/Data.SqlClient/Threading。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Amazon.Runtime.Internal.Util;
using S7.Net;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using MessageBox = System.Windows.Forms.MessageBox;
namespace 基于上位机的测温系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
bool status = false; //定义PLC连接状态字
//bool bOK = true; //写数据地址只读一次标志位
//Thread thRead = null; //定义读写线程名
//int readByteSum; //定义读字节的总数变量名
//int readByteStart; //定义读字节的起始字节变量名
//IniFile Initools = new IniFile();//定义并实例化INI文件读写类
//定义PLC及SQL Server的连接变量
private Plc plcSm1200 = new Plc(CpuType.S71200, "192.168.2.10", 0, 1);
private SqlConnection sqlcon = new SqlConnection("Server=SAMS;User Id=su;Pwd=112233;DataBase=SAM");
//窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“sAMDataSet.Jinlv2”中。您可以根据需要移动或删除它。
this.jinlv2TableAdapter.Fill(this.sAMDataSet.Jinlv2);
lb3.Visible = false;
lb4.Visible = false;
tBx1.ReadOnly = true;
tBx2.ReadOnly = true;
timer1.Enabled = true;
lb1.Show();
sqlcon.Open();//打开SQL Server数据库
dataGridView1.Rows[0].Selected = fals