添加项目文件。

This commit is contained in:
bnala committed 2025-02-04 17:29:49 +08:00
1 parent d68cdea124
commit faaf9136d6
51 files changed
+11674

No files matched your search

+365
View File
@@ -0,0 +1,365 @@
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;
}
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);
//测试用
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 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<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 = true;
label6.Visible = true;
label7.Visible = true;
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
}
}