2025-02-18 14:05:27 +08:00

393 lines
15 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.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)
{
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 openresult = siemensHelp.OpenConn();
if (!openresult.Success)
{
label8.Text = $"PLC:{Plc.id}:{Plc.plcip}建立S7连接失败";
//LogExecute.logNet.WriteError($"{Plc.plcip}建立S7连接失败");
continue;
}
List<t_plcaddress> plcaddress = Init.t_Plcaddresses.FindAll(a => a.plcid == Plc.id);
plcdata PlcData = new plcdata();
var resdresult = siemensHelp.ReadAddress(plcaddress, PlcData);
if (!resdresult.Success)
{
label8.Text = $"PLC:{Plc.id}:{Plc.plcip}读取全部地址处理错误";
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)
{
label8.Text = $"PLC:{Plc.id}:{Plc.plcip}写入心跳错误";
LogExecute.logNet.WriteError($"{Plc.plcip}写入心跳错误:处理停止");
continue;
}
Plc.dateTime = DateTime.Now;
label8.Text = Plc.dateTime.ToString();
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)
{
label8.Text = $"PLC:{Plc.id}:{Plc.plcip}写入地址处理错误";
LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止");
}
continue;
}
//状态+报警
var nowflow = Init.t_Flows.FirstOrDefault(a => a.hopestatus == PlcData.Read_Trigger);
if (nowflow == null)
{
label8.Text = $"PLC:{Plc.id}:未找到当前触发字{PlcData.Read_Trigger},对应的流程";
LogExecute.logNet.WriteError($"未找到当前触发字{PlcData.Read_Trigger},对应的流程!");
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;
}
otherdata otherdata = new otherdata();
otherdata.JS_SN = "12345";
ApiClient apiClient = new ApiClient();
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)
{
label8.Text = $"PLC:{Plc.id}:{Plc.plcip}写入地址处理错误";
LogExecute.logNet.WriteError($"{Plc.plcip}写入地址处理错误:处理停止");
continue;
}
}
else
{
label8.Text = $"MES通讯异常!";
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<t_plc> sQLiteHelper_plc = new db.SQLiteHelper<t_plc>();
BllResult<t_plc> plc_result = sQLiteHelper_plc.QuerySingle(a => a.id == Init.t_Line.plcid1);
BllResult<t_plc> plc_result2 = new BllResult<t_plc>(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<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_interface> sQLiteHelper_interface = new db.SQLiteHelper<t_interface>();
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
}
}