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.mod.server; 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) { LogExecute.logNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; LogExecute.PlclogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; LogExecute.ApilogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; Formrecover(); 线别.Text = "线别:" + Init.t_Line.linename; 段别.Text = "段别:" + Init.t_Line.linemode; 机台编号.Text = "机台编号:" + Init.t_Line.linecode; 操作人员ID.Text = "操作人员ID:" + Init.t_User.username; } public void build(t_plc Plc, CancellationToken token) { while (true) { try { if (token.IsCancellationRequested) { break; } if (Plc == null) { continue; } // test ApiClient apiClient = new ApiClient(); otherdata otherdata = new otherdata(); otherdata.JS_SN = "12345"; plcdata PlcData = new plcdata(); var res = apiClient.CallFunctionByName("SN_CheckIN", PlcData, otherdata); SiemensHelp siemensHelp = Plc.plchelp; var openresult = siemensHelp.OpenConn(); if (!openresult.Success) { PLC链接状态.Text = "PLC链接状态:NG"; PLC链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"{Plc.plcip}建立S7连接失败"); continue; } List plcaddress = Init.t_Plcaddresses.FindAll(a => a.plcid == Plc.id); var resdresult = siemensHelp.ReadAddress(plcaddress, PlcData); if (!resdresult.Success) { PLC链接状态.Text = "PLC链接状态:NG"; PLC链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"{Plc.plcip}读取全部地址处理错误:处理停止"); continue; } //写入心跳 PlcData.Write_Heartbeat = PlcData.Write_Heartbeat == 0 ? (short)1 : (short)0; var haertresult = siemensHelp.WriteAddress(plcaddress, PlcData); if (!haertresult.Success) { PLC链接状态.Text = "PLC链接状态:NG"; PLC链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"{Plc.plcip}写入心跳错误:处理停止"); continue; } DateTime Plc_Time = DateTime.Now; PLC链接状态.Text = "PLC链接状态:OK"; PLC链接状态.BackColor = SystemColors.Control; PLC通讯时间.Text = "PLC通讯时间:" + Plc_Time.ToString("MM-dd HH:mm:ss"); if (PlcData.Read_Trigger == 0 && PlcData.Write_Status == 0) { continue; } else if (PlcData.Read_Trigger != 0 && PlcData.Write_Status != 0) { continue; } else if (PlcData.Read_Trigger == 0 && PlcData.Write_Status != 0) { PlcData.Write_Status = 0; var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData); if (!writeresult.Success) { PLC链接状态.Text = "PLC链接状态:NG"; PLC链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止"); } continue; } //状态+报警 if (Init.Plcstatus != PlcData.Read_Status || (Plc_Time - Init.Plcstatus_LastTime).TotalSeconds > 15) { //反馈状态 var statusresult = apiClient.CallFunctionByName("Status", PlcData, otherdata); if (statusresult.Success) { MES链接状态.Text = "MES链接状态:OK"; MES链接状态.BackColor = SystemColors.Control; MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss"); MES_MsgfORM("Status", statusresult.Mesresult, statusresult.Requesttime); } else { MES链接状态.Text = "MES链接状态:NG"; MES链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"MES通讯异常!"); } } if (Init.Plcstatus == (short)plcstatus.运行 && PlcData.Read_Status == (short)plcstatus.停机) { 机台状态.BackColor = Color.Red; //反馈报警 var statusresult = apiClient.CallFunctionByName("Alarm", PlcData, otherdata); if (statusresult.Success) { MES链接状态.Text = "MES链接状态:OK"; MES链接状态.BackColor = SystemColors.Control; MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss"); MES_MsgfORM("Alarm", statusresult.Mesresult, statusresult.Requesttime); } else { MES链接状态.Text = "MES链接状态:NG"; MES链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"MES通讯异常!"); } continue; } else { 机台状态.BackColor = SystemColors.Control; } Init.Plcstatus = PlcData.Read_Status; Init.Plcstatus_LastTime = DateTime.Now; 机台状态.Text = Enum.GetName(typeof(plcstatus), Init.Plcstatus); var nowflow = Init.t_Flows.FirstOrDefault(a => a.hopestatus == PlcData.Read_Trigger); if (nowflow == null) { LogExecute.logNet.WriteError($"未找到当前触发字{PlcData.Read_Trigger},对应的流程!"); continue; } var nowinterface = Init.t_Interfaces.FirstOrDefault(a => a.id == nowflow.hopeinterfaceid); if (nowinterface == null) { LogExecute.logNet.WriteError($"未找到当前期望接口ID {nowflow.hopeinterfaceid},对应的接口!"); continue; } var result = apiClient.CallFunctionByName(nowinterface.code,PlcData, otherdata); if (result.Success) { var writeresult = siemensHelp.WriteAddress(Init.t_Plcaddresses.FindAll(a => a.plcid == Plc.id),PlcData); if (!writeresult.Success) { PLC链接状态.Text = "PLC链接状态:NG"; PLC链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止"); continue; } else { MES链接状态.Text = "MES链接状态:OK"; MES链接状态.BackColor = SystemColors.Control; MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss"); } } else { MES链接状态.Text = "MES链接状态:NG"; MES链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"MES通讯异常!"); } } 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; 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; Formrecover(); 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"); } } } private void timer1_Tick(object sender, EventArgs e) { 当前时间.Text = "当前时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss"); } #endregion public void Formrecover() { MES链接状态.Text = "MES链接状态:None"; MES链接状态.BackColor = SystemColors.Control; MES通讯时间.Text = "MES通讯时间:None"; MES通讯时间.BackColor = SystemColors.Control; PLC链接状态.Text = "PLC链接状态:None"; PLC链接状态.BackColor = SystemColors.Control; PLC通讯时间.Text = "PLC通讯时间:None"; PLC通讯时间.BackColor = SystemColors.Control; 机台状态.Text = "机台状态:None"; 机台状态.BackColor = SystemColors.Control; 请扫描LOT号.Enabled = false; LOT号.Visible = false; Tray盘二维码.Visible = false; 事件1.Visible = false; 事件1结果.Visible = false; 事件1时间.Visible = false; 事件2.Visible = false; 事件2结果.Visible = false; 事件2时间.Visible = false; 事件3.Visible = false; 事件3结果.Visible = false; 事件3时间.Visible = false; 事件4.Visible = false; 事件4结果.Visible = false; 事件4时间.Visible = false; 底部注释1.Visible = false; 底部注释2.Visible = false; 底部注释3.Visible = false; 底部注释4.Visible = false; 底部注释5.Visible = false; } public void MES_MsgfORM(string name,mesresult result,DateTime dateTime) { if (事件1.Text.Contains("事件")) { 事件1.Visible = true; 事件1结果.Visible = true; 事件1时间.Visible = true; 事件1.Text = name; 事件1时间.Text = dateTime.ToString("MM-dd HH:mm:ss"); 事件1结果.Text = result.ToString(); } else if (事件2.Text.Contains("事件")) { 事件2.Visible = true; 事件2结果.Visible = true; 事件2时间.Visible = true; 事件2.Text = name; 事件2时间.Text = dateTime.ToString("MM-dd HH:mm:ss"); 事件2结果.Text = result.ToString(); } else if (事件3.Text.Contains("事件")) { 事件3.Visible = true; 事件3结果.Visible = true; 事件3时间.Visible = true; 事件3.Text = name; 事件3时间.Text = dateTime.ToString("MM-dd HH:mm:ss"); 事件3结果.Text = result.ToString(); } else if (事件4.Text.Contains("事件")) { 事件4.Visible = true; 事件4结果.Visible = true; 事件4时间.Visible = true; 事件4.Text = name; 事件4时间.Text = dateTime.ToString("MM-dd HH:mm:ss"); 事件4结果.Text = result.ToString(); } else { 事件1.Visible = true; 事件1结果.Visible = true; 事件1时间.Visible = true; 事件1.Text = name; 事件1时间.Text = dateTime.ToString("MM-dd HH:mm:ss"); 事件1结果.Text = result.ToString(); 事件2.Visible = false; 事件2结果.Visible = false; 事件2时间.Visible = false; 事件2.Text = "事件2"; 事件2时间.Text = "事件2时间"; 事件2结果.Text = "OK"; 事件3.Visible = false; 事件3结果.Visible = false; 事件3时间.Visible = false; 事件3.Text = "事件3"; 事件3时间.Text = "事件3时间"; 事件3结果.Text = "OK"; 事件4.Visible = false; 事件4结果.Visible = false; 事件4时间.Visible = false; 事件4.Text = "事件4"; 事件4时间.Text = "事件4时间"; 事件4结果.Text = "OK"; } } } }