2025-03-25 00:01:26 +08:00

1051 lines
44 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
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.database;
using WindowsFormsApp1.mod.server;
using WindowsFormsApp1.server;
using static SQLite.SQLite3;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
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();
linename.Text = Init.t_Line.linename;
linecode.Text = Init.t_Line.linecode;
linetype.Text = Init.t_Line.linetype;
linemode.Text = Init.t_Line.linemode;
lineremark1.Text = Init.t_Line.lineremark1;
lineremark2.Text = Init.t_Line.lineremark2;
lineremark3.Text = Init.t_Line.lineremark3;
lineremark4.Text = Init.t_Line.lineremark4;
lineremark5.Text = Init.t_Line.lineremark5;
lineremark6.Text = Init.t_Line.lineremark6;
lineremark7.Text = Init.t_Line.lineremark7;
操作人员ID.Text = Init.t_User.username;
}
public void build(t_plc Plc, CancellationToken token)
{
while (true)
{
try
{
if (token.IsCancellationRequested)
{
break;
}
ChangeBarcode();
if (Plc == null)
{
continue;
}
#region PLC链接及读取
SiemensHelp siemensHelp = Plc.plchelp;
var openresult = siemensHelp.OpenConn();
if (!openresult.Success)
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}建立S7连接失败");
continue;
}
List<t_plcaddress> plcaddress = Init.t_Plcaddresses.FindAll(a => a.plcid == Plc.id);
List<t_angleaddress> angleaddress = Init.t_Angleaddresses.FindAll(a => a.plcid == Plc.id);
List<t_torqueaddress> torqueaddress = Init.t_Torqueaddresses.FindAll(a => a.plcid == Plc.id);
List<t_pressurecurve> pressurecurveaddress = Init.t_Pressurecurves.FindAll(a => a.plcid == Plc.id);
plcdata PlcData = new plcdata();
var resdresult = siemensHelp.ReadAddress(plcaddress, PlcData);
if (!resdresult.Success)
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
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;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
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");
PLC_Form(Plc, plcstate.OK, Plc_Time);
#endregion
ApiClient apiClient = new ApiClient();
otherdata otherdata = new otherdata();
#region 机台状态及报警
//机台状态
//机台状态.Text = "机台状态:" + Enum.GetName(typeof(plcstatus), Init.Plcstatus);
int oldstatus = Init.Plcstatus;
if (PlcData.Read_Status != 0)
{
DateTime oldPlcstatus_LastTime = Init.Plcstatus_LastTime;
//状态+报警
if (oldstatus != PlcData.Read_Status || (Plc_Time - oldPlcstatus_LastTime).TotalSeconds > 5)
{
//反馈状态
Init.Plcstatus = PlcData.Read_Status;
Init.Plcstatus_LastTime = DateTime.Now;
otherdata.Plc_Status = PlcData.Read_Status;
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);
if (statusresult.Mesresult != mesresult.PASS)
{
PlcData.Write_Status = 1001;
siemensHelp.WriteAddress(plcaddress, PlcData);
continue;
}
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
var errresult = siemensHelp.WriteAddress(plcaddress, PlcData);
continue;
}
}
}
//机台报警
if (PlcData.Read_Status == (short)plcstatus.停机)
{
//机台状态.BackColor = Color.Red;
if (oldstatus != (short)plcstatus.停机)
{
//反馈报警
List<string> list = new List<string>();
///////////////////////////////测试用
list.Add("M03CVSO-01");
otherdata.Plc_ErrorMsgs = list;
Init.Plcstatus = PlcData.Read_Status;
Init.Plcstatus_LastTime = DateTime.Now;
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);
if (statusresult.Mesresult != mesresult.PASS)
{
PlcData.Write_Status = 1001;
siemensHelp.WriteAddress(plcaddress, PlcData);
continue;
}
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
siemensHelp.WriteAddress(plcaddress, PlcData);
}
}
continue;
}
else
{
//机台状态.BackColor = SystemColors.Control;
}
#endregion
#region 判断及查找流程
if (PlcData.Read_Trigger == 0 && PlcData.Write_Status == 0)
{
continue;
}
else if (PlcData.Read_Trigger == PlcData.Write_Status)
{
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;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止");
}
continue;
}
LogExecute.logNet.WriteDebug($"读取状态字{PlcData.Read_Trigger}");
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;
}
#endregion
#region 根据流程,判断是否需要扫码、气密、扭力等
if (nowflow.scan)
{
//扫码
ScanForm scanForm = new ScanForm();
DialogResult scan_result = scanForm.ShowDialog();
if (scan_result == DialogResult.OK)
{
string returnValue = scanForm.ReturnValue;
otherdata.JS_SN = returnValue;
Init.ScanString = returnValue;
}
else
{
continue;
}
}
if (nowflow.dzscan)
{
//扫码
ScanForm_DZ scanForm = new ScanForm_DZ();
DialogResult scan_result = scanForm.ShowDialog();
var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData);
if (!writeresult.Success)
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
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");
}
continue;
//if (scan_result == DialogResult.OK)
//{
// string returnValue = scanForm.ReturnValue;
// otherdata.JS_SN = returnValue;
// Init.ScanString = returnValue;
//}
//else
//{
// continue;
//}
}
if (nowflow.finalpressurecurve)
{
//压力曲线
var pressurecurveaddress_result = siemensHelp.ReadAddress(pressurecurveaddress);
if (!pressurecurveaddress_result.Success)
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}压力曲线数据地址处理错误:处理停止");
continue;
}
otherdata.FinalPressureCurve = pressurecurveaddress_result.Data;
}
//if (nowflow.finaltorquecurve)
//{
// //扭力曲线(这里不处理,后端API获取)
//}
if (nowflow.finaltorquevalue)
{
//螺丝枪扭力数据
var finaltorquevalue_result = siemensHelp.ReadAddress(torqueaddress);
if (!finaltorquevalue_result.Success)
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}螺丝枪扭力数据地址处理错误:处理停止");
continue;
}
otherdata.FinalTorqueValue = finaltorquevalue_result.Data;
}
if (nowflow.airtight)
{
//气密参数
otherdata.FinalAirtightValue = Init.airtight;
}
if (nowflow.finalanglevaluue)
{
//螺丝枪角度
var finalanglevaluue_result = siemensHelp.ReadAddress(angleaddress);
if (!finalanglevaluue_result.Success)
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
LogExecute.logNet.WriteError($"{Plc.plcip}螺丝枪角度数据地址处理错误:处理停止");
continue;
}
otherdata.FinalAngleValue = finalanglevaluue_result.Data;
}
if (nowflow.fileupload)
{
//文件上传
}
#endregion
#region 调用后端接口
var result = apiClient.CallFunctionByName(nowinterface.code, PlcData, otherdata);
if (result.IsShow)
{
MES提示信息.Visible = true;
MES提示信息.Text = DateTime.Now.ToString("HH:mm:ss") + ":" + result.Msg;
}
if (result.Success)
{
var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData);
if (!writeresult.Success)
{
//PLC链接状态.Text = "PLC链接状态:NG";
//PLC链接状态.BackColor = Color.Red;
PLC_Form(Plc, plcstate.NG, DateTime.Now);
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");
if (result.Mesresult != mesresult.PASS)
{
PlcData.Write_Status = 1001;
siemensHelp.WriteAddress(plcaddress, PlcData);
continue;
}
}
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
siemensHelp.WriteAddress(plcaddress, PlcData);
}
#endregion
}
catch (Exception e)
{
LogExecute.logNet.WriteError(e.Message);
}
finally
{
Thread.Sleep(1000);
}
}
}
#region 开始及结束处理按钮
private void 开始处理_Click(object sender, EventArgs e)
{
if (开始处理.Text == "开始处理")
{
SQLiteHelper<t_plc> sQLiteHelper_plc = new db.SQLiteHelper<t_plc>();
var plc_sqlresult = sQLiteHelper_plc.QuerySingle(a=>a.id == Init.t_Line.plcid);
if (!plc_sqlresult.Success)
{
MessageBox.Show("无法初始化PLC信息,无法开始处理!");
return;
}
SQLiteHelper<t_keyvalue> sQLiteHelper_key = new db.SQLiteHelper<t_keyvalue>();
var key_sqlresult = sQLiteHelper_key.QueryAll();
if (!key_sqlresult.Success)
{
MessageBox.Show("无法初始化信息信息,无法开始处理!");
return;
}
SQLiteHelper<t_flow> sQLiteHelper_flow = new db.SQLiteHelper<t_flow>();
var flow_result = sQLiteHelper_flow.Query(a => a.flowid == Init.t_Line.flowid);
if (!flow_result.Success)
{
MessageBox.Show("无法初始化流程信息,无法开始处理!");
return;
}
SQLiteHelper<t_plcaddress> sQLiteHelper_address = new db.SQLiteHelper<t_plcaddress>();
var address_result = sQLiteHelper_address.QueryAll();
if (!address_result.Success)
{
MessageBox.Show("无法初始化PLC地址信息,无法开始处理!");
return;
}
SQLiteHelper<t_torqueaddress> sQLiteHelper_torqueaddress = new db.SQLiteHelper<t_torqueaddress>();
var torqueaddress_result = sQLiteHelper_torqueaddress.QueryAll();
if (!torqueaddress_result.Success)
{
MessageBox.Show("无法初始化螺丝枪扭力信息,无法开始处理!");
return;
}
SQLiteHelper<t_angleaddress> sQLiteHelper_angleaddress = new db.SQLiteHelper<t_angleaddress>();
var angleaddress_result = sQLiteHelper_angleaddress.QueryAll();
if (!angleaddress_result.Success)
{
MessageBox.Show("无法初始化螺丝枪角度信息,无法开始处理!");
return;
}
SQLiteHelper<t_pressurecurve> sQLiteHelper_pressurecurve = new db.SQLiteHelper<t_pressurecurve>();
var pressurecurve_result = sQLiteHelper_pressurecurve.QueryAll();
if (!pressurecurve_result.Success)
{
MessageBox.Show("无法初始化压力曲线信息,无法开始处理!");
return;
}
SQLiteHelper<t_interface> sQLiteHelper_interface = new db.SQLiteHelper<t_interface>();
var interface_result = sQLiteHelper_interface.QueryAll();
if (!interface_result.Success)
{
MessageBox.Show("无法初始化接口地址信息,无法开始处理!");
return;
}
SQLiteHelper<t_serialport> sQLiteHelper_serialport = new db.SQLiteHelper<t_serialport>();
var serialport_result = sQLiteHelper_serialport.QueryAll();
if (!serialport_result.Success)
{
MessageBox.Show("无法初始化COM口信息,无法开始处理!");
return;
}
#region 初始化全局变量
Init.t_Flows = flow_result.Data;
Init.t_Plcaddresses = address_result.Data;
Init.t_Angleaddresses = angleaddress_result.Data;
Init.t_Torqueaddresses = torqueaddress_result.Data;
Init.t_Pressurecurves = pressurecurve_result.Data;
Init.t_Interfaces = interface_result.Data;
Init.t_Keyvalues = key_sqlresult.Data;
#endregion
#region 初始化COM口服务
try
{
var scanport = serialport_result.Data.FirstOrDefault(a => a.name == "Scan");
if (scanport != null)
{
if (scanport.enable)
{
Enum.TryParse(scanport.parity, out Parity parity);
Init.ScanPort = new SerialPort(scanport.portName, scanport.baudRate, parity, scanport.dataBits);
if (!Init.ScanPort.IsOpen)
{
Init.ScanPort?.Open();
底部注释1.Text = "扫码枪COM-Ready";
底部注释1.Visible = true;
// 订阅 DataReceived 事件
胶水上下线.Enabled = true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("无法开启扫码枪COM,无法开始处理!" + ex.Message);
return;
}
try
{
var scanport = serialport_result.Data.FirstOrDefault(a => a.name == "Airtight");
if (scanport != null)
{
if (scanport.enable)
{
Enum.TryParse(scanport.parity, out Parity parity);
Init.AirtightPort = new SerialPort(scanport.portName, scanport.baudRate, parity, scanport.dataBits);
if (!Init.AirtightPort.IsOpen)
{
Init.AirtightPort?.Open();
Init.AirtightPort.DataReceived += AirtightPort_DataReceived;
底部注释2.Text = "气密仪COM-Ready";
底部注释2.Visible = true;
}
胶水上下线.Enabled = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show("无法开启气密仪COM,无法开始处理!" + ex.Message);
return;
}
#endregion
Init.threadHelps = new List<ThreadHelp>();
var plc = plc_sqlresult.Data;
plc.plchelp = new SiemensHelp(plc.plcip, plc.timeout);
ThreadHelp threadHelp = new ThreadHelp();
if (!threadHelp.isrunning1)
{
threadHelp.cancellationTokenSource1 = new CancellationTokenSource();
threadHelp.Thread = new Thread(() => build(plc, threadHelp.cancellationTokenSource1.Token));
threadHelp.Thread.Start();
threadHelp.isrunning1 = true;
}
Init.threadHelps.Add(threadHelp);
开始处理.Text = "停止处理";
开始处理.BackColor = Color.Red;
LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},开始处理!");
}
else
{
foreach (var thread in Init.threadHelps)
{
if (thread.Thread.IsAlive)
{
// 请求取消线程
thread.cancellationTokenSource1?.Cancel();
thread.Thread.Join(); // 等待线程完成
}
thread.isrunning1 = false;
}
if (Init.ScanPort != null && Init.ScanPort.IsOpen)
{
Init.ScanPort?.Close();
}
if (Init.AirtightPort != null && Init.AirtightPort.IsOpen)
{
Init.AirtightPort?.Close();
}
//Thread.Sleep(500);
FormRecover();
LogExecute.logNet.WriteInfo($"用户{Init.t_User.username},停止处理!");
}
}
#endregion
#region 页面按钮-跳转
private void 扭力角度扭矩设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
Angleaddress_Manage angleaddress_Manage = new Angleaddress_Manage();
angleaddress_Manage.ShowDialog();
}
private void 螺丝枪扭力设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
Torqueaddress_Manage torqueaddress_Manage = new Torqueaddress_Manage();
torqueaddress_Manage.ShowDialog();
}
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 系统参数配置ToolStripMenuItem_Click(object sender, EventArgs e)
{
KeyValue keyValue = new KeyValue();
keyValue.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)
{
MESUrl mES_URL = new MESUrl();
mES_URL.ShowDialog();
}
private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
{
UserChangeP uSER_ChangeP = new UserChangeP();
uSER_ChangeP.ShowDialog();
}
private void 用户管理ToolStripMenuItem1_Click(object sender, EventArgs e)
{
User_Manage uSER_Manage = new User_Manage();
uSER_Manage.ShowDialog();
}
private void 扫码枪配置ToolStripMenuItem1_Click(object sender, EventArgs e)
{
SerialPort_Manage serialPort_Manage = new SerialPort_Manage();
serialPort_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 >= 200)
{
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");
}
public void FormRecover()
{
开始处理.Text = "开始处理";
开始处理.BackColor = Color.Green;
MES链接状态.Text = "MES链接状态:None";
MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:None";
MES通讯时间.BackColor = SystemColors.Control;
PLC1名称.Visible = false;
PLC1状态.Visible = false;
PLC1机台状态.Visible = false;
PLC1时间.Visible = false;
//胶水上下线.Enabled = false;
MES提示信息.Visible = 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;
剩余寿命.Visible = false;
barcode1.Visible = false;
barcode2.Visible = false;
barcode3.Visible = false;
barcode4.Visible = false;
barcode5.Visible = false;
barcode6.Visible = false;
barcode7.Visible = false;
barcode8.Visible = false;
barcode9.Visible = false;
}
public void ChangeBarcode()
{
剩余寿命.Visible = true;
barcode1.Visible = true;
barcode2.Visible = true;
barcode3.Visible = true;
barcode4.Visible = true;
barcode5.Visible = true;
barcode6.Visible = true;
barcode7.Visible = true;
barcode8.Visible = true;
barcode9.Visible = true;
var power = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Power");
var i_barcode1 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode1");
var i_barcode2 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode2");
var i_barcode3 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode3");
var i_barcode4 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode4");
var i_barcode5 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode5");
var i_barcode6 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode6");
var i_barcode7 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode7");
var i_barcode8 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode8");
var i_barcode9 = Init.t_Keyvalues.FirstOrDefault(a => a.key == "Barcode9");
剩余寿命.Text = power == null ? "": power.value;
barcode1.Text = i_barcode1 == null ? "":i_barcode1.value;
barcode2.Text = i_barcode2 == null ? "":i_barcode2.value;
barcode3.Text = i_barcode3 == null ? "":i_barcode3.value;
barcode4.Text = i_barcode4 == null ? "":i_barcode4.value;
barcode5.Text = i_barcode5 == null ? "":i_barcode5.value;
barcode6.Text = i_barcode6 == null ? "":i_barcode6.value;
barcode7.Text = i_barcode7 == null ? "":i_barcode7.value;
barcode8.Text = i_barcode8 == null ? "":i_barcode8.value;
barcode9.Text = i_barcode9 == null ? "":i_barcode9.value;
}
public void PLC_Form(t_plc plc, plcstate state, DateTime time)
{
string plcname = plc.plcname + ":" + plc.plcip;
string datetime = time.ToString("HH:mm:ss");
PLC1名称.Visible = true;
PLC1状态.Visible = true;
PLC1机台状态.Visible = true;
PLC1时间.Visible = true;
PLC1名称.Text = plcname;
PLC1状态.Text = state.ToString();
PLC1机台状态.Text = Enum.GetName(typeof(plcstatus), Init.Plcstatus);
PLC1时间.Text = datetime;
if (PLC1状态.Text == plcstate.NG.ToString())
{
PLC1状态.BackColor = Color.Red;
}
else
{
PLC1状态.BackColor = SystemColors.Control;
}
if (PLC1机台状态.Text == plcstatus.停机.ToString())
{
PLC1机台状态.BackColor = Color.Red;
}
else
{
PLC1机台状态.BackColor = SystemColors.Control;
}
}
public void MES_MsgfORM(string name, mesresult result, DateTime dateTime)
{
if (事件1.Text == name)
{
事件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 == name)
{
事件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 == name)
{
事件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 == name)
{
事件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
{
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";
}
}
}
#endregion
#region 其他业务处理
private void 胶水上下线_Click(object sender, EventArgs e)
{
ScanForm_SSQ scanForm = new ScanForm_SSQ();
DialogResult result = scanForm.ShowDialog();
}
public void AirtightPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (!Init.AirtightPort.IsOpen)
{
Init.AirtightPort?.Open();
}
// 读取串口接收到的数据
// 读取串口接收到的数据
var dataBuilder = new StringBuilder();
while (Init.AirtightPort.BytesToRead > 0)
{
System.Threading.Thread.Sleep(100);
dataBuilder.Append(Init.AirtightPort.ReadExisting());
}
var ReturnValue = dataBuilder.ToString();
LogExecute.logNet.WriteDebug($"通过气密仪获取到 {ReturnValue}");
int index1 = 1;
int index2 = 1;
string press = string.Empty;
string leakage = string.Empty;
//pressure选择为yes// export选择为yes(非常重要)
var dataArray = ReturnValue.Trim('\r', '\n').Split('\t');
float[] sealFloat = new float[2];
//充气压力
if (dataArray[3] == "")
{
sealFloat[0] = 0;
}
else
{
if (dataArray[3].Contains("-"))
{
press = dataArray[3].Remove(0, 1);
index1 = -1;
}
else
{
press = dataArray[3];
index1 = 1;
}
sealFloat[0] = float.Parse(press);
}
//测试泄露结果
if (dataArray[5] == "")
sealFloat[1] = 0;
else
{
if (dataArray[5].Contains("-"))
{
leakage = dataArray[5].Remove(0, 1);
index2 = -1;
}
else
{
leakage = dataArray[5];
index2 = 1;
}
sealFloat[1] = float.Parse(leakage);
}
int[] sealInt = new int[2];
sealInt[0] = Convert.ToInt32(sealFloat[1] * 1000) * (index2);
sealInt[1] = Convert.ToInt32(sealFloat[0] * 1000) * (index1);
Init.airtight.Airtight_Value = sealInt[0].ToString();
Init.airtight.Leak_Value = sealInt[1].ToString();
Init.airtight.LastTime = DateTime.Now;
LogExecute.logNet.WriteDebug($"通过气密仪获取到 压力值:{Init.airtight.Airtight_Value},泄露值:{Init.airtight.Leak_Value}");
}
catch (Exception ex)
{
LogExecute.logNet.WriteError($"通过气密仪获取到压力参数失败,错误原因{ex.Message}");
}
}
#endregion
}
}