using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; using System.Runtime.Remoting.Contexts; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; using HslCommunication.LogNet; using WindowsFormsApp1.db; using WindowsFormsApp1.form; using WindowsFormsApp1.mod; using WindowsFormsApp1.server; using static SQLite.SQLite3; namespace WindowsFormsApp1 { public partial class MainForm : Form { private Thread thread_plc1; private Thread thread_plc2; public MainForm() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void Form1_Load(object sender, EventArgs e) { label1.Visible = false; label2.Visible = false; label3.Visible = false; label4.Visible = false; label5.Visible = false; label6.Visible = false; label7.Visible = false; label8.Visible = false; LogExecute.logNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; LogExecute.PlclogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; LogExecute.ApilogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; } public void build(t_plc Plc, CancellationToken token) { while (true) { try { if (token.IsCancellationRequested) { break; } if (Plc == null) { continue; } SiemensHelp siemensHelp = Plc.plchelp; var readlist = Init.t_Plcaddresses.FindAll(a=>a.plcid == Plc.id && a.type == "read"); var writelist = Init.t_Plcaddresses.FindAll(a => a.plcid == Plc.id && a.type == "write"); var openresult = siemensHelp.OpenConn(); if (!openresult.Success) { label8.Text = $"PLC:{Plc.id}:{Plc.plcip}建立S7连接失败"; //LogExecute.logNet.WriteError($"{Plc.plcip}建立S7连接失败"); continue; } var resdresult = siemensHelp.ReadAddress(readlist); if (!resdresult.Success) { label8.Text = $"PLC:{Plc.id}:{Plc.plcip}读取地址处理错误"; //LogExecute.logNet.WriteError($"{Plc.plcip}读取地址处理错误:处理停止"); continue; } //写入心跳 var heartaddress = writelist.FirstOrDefault(a=>a.remark == "心跳"); var haert = int.Parse(heartaddress.value); if (haert == 0) { haert = 1; } else { haert = 0; } heartaddress.value = haert.ToString(); var haertresult = siemensHelp.WriteHaert(writelist); if (!haertresult.Success) { label8.Text = $"PLC:{Plc.id}:{Plc.plcip}写入心跳错误"; //LogExecute.logNet.WriteError($"{Plc.plcip}读取地址处理错误:处理停止"); continue; } Plc.dateTime = DateTime.Now; //判断触发字 var status = readlist.FirstOrDefault(a=>a.remark == "触发字"); if (status == null) { label8.Text = $"PLC:{Plc.id}:未找到触发字对应的字段"; //LogExecute.logNet.WriteError($"未找到触发字对应的字段!"); continue; } var nowstatus = int.Parse(status.value); label8.Text = Plc.dateTime.ToString(); //测试用 //var writestatus = writelist.FirstOrDefault(a => a.remark == "状态字"); //writestatus.value = nowstatus.ToString(); //var write1 = writelist.FirstOrDefault(a => a.remark == "反馈工艺参数10"); //write1.value = readlist.FirstOrDefault(a => a.remark == "写入工艺参数10").value; //var write2 = writelist.FirstOrDefault(a => a.remark == "反馈SN/二维码"); //write2.value = readlist.FirstOrDefault(a => a.remark == "产品SN").value; //var write3 = writelist.FirstOrDefault(a => a.remark == "触发备用5"); //write3.value = readlist.FirstOrDefault(a => a.remark == "触发备用5").value; //var writeresult = siemensHelp.WriteAddress(writelist); //if (!writeresult.Success) //{ // label8.Text = $"PLC:{Plc.id}:{Plc.plcip}写入地址处理错误"; // //LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止"); // continue; //} //continue; //nowstatus = 3; if (nowstatus == 0) { continue; } var nowflow = Init.t_Flows.FirstOrDefault(a => a.hopestatus == nowstatus); if (nowflow == null) { label8.Text = $"PLC:{Plc.id}:未找到当前触发字{nowstatus},对应的流程"; //LogExecute.logNet.WriteError($"未找到当前触发字{nowstatus},对应的流程!"); continue; } var nowinterface = Init.t_Interfaces.FirstOrDefault(a => a.id == nowflow.hopeinterfaceid); if (nowinterface == null) { label8.Text = $"PLC:{Plc.id}:未找到当前期望接口ID {nowflow.hopeinterfaceid},对应的接口"; //LogExecute.logNet.WriteError($"未找到当前期望接口ID {nowflow.hopeinterfaceid},对应的接口!"); continue; } ApiClient apiClient = new ApiClient(); var result = apiClient.CallFunctionByName(nowinterface.code); if (result.Success) { var writestatus = writelist.FirstOrDefault(a => a.remark == "状态字"); writestatus.value = nowstatus.ToString(); var writeresult = siemensHelp.WriteAddress(writelist); if (!writeresult.Success) { label8.Text = $"PLC:{Plc.id}:{Plc.plcip}写入地址处理错误"; //LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止"); continue; } } else { } } catch (Exception e) { LogExecute.logNet.WriteError(e.Message); } finally { Thread.Sleep(1000); } } } #region 开始及结束处理按钮 private void 开始处理_Click(object sender, EventArgs e) { if (开始处理.Text == "开始处理") { SQLiteHelper sQLiteHelper_plc = new db.SQLiteHelper(); BllResult plc_result = sQLiteHelper_plc.QuerySingle(a => a.id == Init.t_Line.plcid1); BllResult plc_result2 = new BllResult(false, "", null); if (!plc_result.Success) { MessageBox.Show("无法初始化PLC1信息,无法开始处理!"); return; } if (Init.t_Line.plcid2 != 0) { plc_result2 = sQLiteHelper_plc.QuerySingle(a => a.id == Init.t_Line.plcid2); if (!plc_result2.Success) { MessageBox.Show("无法初始化PLC2信息,无法开始处理!"); return; } } SQLiteHelper sQLiteHelper_flow = new db.SQLiteHelper(); var flow_result = sQLiteHelper_flow.Query(a => a.flowid == Init.t_Line.flowid); if (!flow_result.Success) { MessageBox.Show("无法初始化流程信息,无法开始处理!"); return; } SQLiteHelper sQLiteHelper_address = new db.SQLiteHelper(); var address_result = sQLiteHelper_address.QueryAll(); if (!address_result.Success) { MessageBox.Show("无法初始化PLC地址信息,无法开始处理!"); return; } SQLiteHelper sQLiteHelper_interface = new db.SQLiteHelper(); var interface_result = sQLiteHelper_interface.QueryAll(); if (!interface_result.Success) { MessageBox.Show("无法初始化接口地址信息,无法开始处理!"); return; } Init.t_Plc1 = plc_result.Data; Init.t_Plc1.plchelp = new SiemensHelp(Init.t_Plc1.plcip, Init.t_Plc1.timeout); Init.t_Plc2 = plc_result2.Data; if (Init.t_Plc2 != null) { Init.t_Plc2.plchelp = new SiemensHelp(Init.t_Plc2.plcip, Init.t_Plc2.timeout); } Init.t_Flows = flow_result.Data; Init.t_Plcaddresses = address_result.Data; Init.t_Interfaces = interface_result.Data; if (!Init.isrunning1) { Init.cancellationTokenSource1 = new CancellationTokenSource(); thread_plc1 = new Thread(() => build(Init.t_Plc1, Init.cancellationTokenSource1.Token)); thread_plc1.Start(); Init.isrunning1 = true; } //if (!Init.isrunning2) //{ // Init.cancellationTokenSource2 = new CancellationTokenSource(); // thread_plc2 = new Thread(() => build(Init.t_Plc2, Init.cancellationTokenSource2.Token)); // thread_plc2.Start(); // Init.isrunning2 = true; //} 开始处理.Text = "停止处理"; 开始处理.BackColor = Color.Red; label1.Text = "MES站名:" + Init.t_Line.linename; label2.Text = "MES站名:" + Init.t_Line.linecode; label3.Text = "类型:" + Init.t_Line.linetype; label4.Text = "模组:" + Init.t_Line.linemode; label1.Visible = true; label2.Visible = true; label3.Visible = true; label4.Visible = true; label5.Visible = false; label6.Visible = false; label7.Visible = false; label8.Visible = true; LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},开始处理!"); } else { if (thread_plc1 != null && thread_plc1.IsAlive) { // 请求取消线程 Init.cancellationTokenSource1?.Cancel(); thread_plc1.Join(); // 等待线程完成 } Init.isrunning1 = false; if (thread_plc2 != null && thread_plc2.IsAlive) { // 请求取消线程 Init.cancellationTokenSource2?.Cancel(); thread_plc2.Join(); // 等待线程完成 } Init.isrunning2 = false; Thread.Sleep(500); 开始处理.Text = "开始处理"; 开始处理.BackColor = Color.Green; label1.Visible = false; label2.Visible = false; label3.Visible = false; label4.Visible = false; LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},停止处理!"); } } #endregion #region 页面按钮-跳转 private void 流程管理ToolStripMenuItem_Click(object sender, EventArgs e) { FLOW_Manage fLOW_Mange = new FLOW_Manage(); fLOW_Mange.ShowDialog(); } private void 产线管理ToolStripMenuItem_Click(object sender, EventArgs e) { LINE_Manage lINE_Manage = new LINE_Manage(); lINE_Manage.ShowDialog(); } private void pLC通讯设置ToolStripMenuItem_Click(object sender, EventArgs e) { PLC_Manage pLC_Manage = new PLC_Manage(); pLC_Manage.ShowDialog(); } private void pLC地址设置ToolStripMenuItem_Click(object sender, EventArgs e) { PLCADDRESS_Manage pLCADDRESS_Manage = new PLCADDRESS_Manage(); pLCADDRESS_Manage.ShowDialog(); } private void 切换用户ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Restart(); } private void mES接口配置ToolStripMenuItem_Click(object sender, EventArgs e) { MES_URL mES_URL = new MES_URL(); mES_URL.ShowDialog(); } private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e) { USER_ChangeP uSER_ChangeP = new USER_ChangeP(); uSER_ChangeP.ShowDialog(); } private void 用户管理ToolStripMenuItem1_Click(object sender, EventArgs e) { USER_Manage uSER_Manage = new USER_Manage(); uSER_Manage.ShowDialog(); } #endregion #region 页面-布局、日志等 private void LogNet_BeforeSaveToFile(object sender, HslEventArgs e) { lock (this) { DateTime time = e.HslMessage.Time;//获取时间 string text = e.HslMessage.Text;//日志文本 int textLength = richTextBox1.TextLength; int lineCount = richTextBox1.GetLineFromCharIndex(textLength); if (lineCount >= 20) { richTextBox1.Clear(); } if (e.HslMessage.Degree == HslMessageDegree.ERROR) { richTextBox1.SelectionColor = Color.Red; richTextBox1.AppendText("[错误] " + time.ToString() + " | " + text + "\n"); } else if (e.HslMessage.Degree == HslMessageDegree.DEBUG) { richTextBox1.SelectionColor = Color.Green; richTextBox1.AppendText("[日志] " + time.ToString() + " | " + text + "\n"); } else if (e.HslMessage.Degree == HslMessageDegree.WARN) { richTextBox1.SelectionColor = Color.Orange; richTextBox1.AppendText("[警告] " + time.ToString() + " | " + text + "\n"); } else if (e.HslMessage.Degree == HslMessageDegree.INFO) { richTextBox1.SelectionColor = Color.Orange; richTextBox1.AppendText("[提示] " + time.ToString() + " | " + text + "\n"); } else { richTextBox1.SelectionColor = Color.Red; richTextBox1.AppendText("[未知] " + time.ToString() + " | " + text + "\n"); } } } #endregion } }