Files
LL-28/MES对接/华擎对接资料/c#_demo/C#/MEStest/Form1_1.cs
2025-11-19 15:38:36 +08:00

338 lines
15 KiB
C#
Raw Permalink 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.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Newtonsoft;//处理从mes获取的json字符串
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MEStest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int MESBackFunc(StringBuilder data);
/// <summary>
/// mes函数执行成功
/// </summary>
public const int MesBackOK = 0;
/// <summary>
/// MES的句柄
/// </summary>
public static int hMes = 0;
private const string DLLPATH = "HQMES.dll";
[DllImport(DLLPATH)]
public static extern int MesInit(MESBackFunc func, ref int hMes, StringBuilder sInfo, ref int InfoLen);
[DllImport(DLLPATH)]
public static extern int MesStart(int hMes, string SN, string ActionName, string Tools, StringBuilder sInfo, ref int InfoLen);
[DllImport(DLLPATH)]
public static extern int MesStart2(int hMes, string SN, string SNType, string ActionName, string Tools, StringBuilder sInfo, ref int InfoLen);
[DllImport(DLLPATH)]
public static extern int MesEnd(int hMes, string SN, string ActionName, string Tools, string ErrorCode, StringBuilder sInfo, ref int InfoLen);
[DllImport(DLLPATH)]
public static extern int MesEnd2(int hMes, string SN, string SNType, string ActionName, string Tools, string ErrorCode, string AllData, StringBuilder sInfo, ref int InfoLen);
[DllImport(DLLPATH)]
private static extern int MesSaveAndGetExtraInfo(int hMes, string G_TYPE, string G_POSITION, string G_KEY, string G_VALUE, string G_EXTINFO, StringBuilder sInfo, ref int InfoLen);
[DllImport(DLLPATH)]
public static extern int MesUnInit(int hMes);
/// <summary>
/// 回调函数
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static int WriteLogFile(StringBuilder data)
{
//MessageBox.Show(data.ToString());
return 0;
}
static int hmes = 0;
private static MESBackFunc tempFunc;// 必须要加一个变量,这样不会被回收
private void btMesInit_Click(object sender, EventArgs e)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
if (hMes == 0)
{
tempFunc = WriteLogFile; // 必须要加一个变量,这样不会被回收 这个回调函数赋值只能放在这个位置防止当多次点击init之后可能会出现回调函数被回收的现象
if (0 == MesInit(tempFunc, ref hMes, strdata, ref len))
{
JObject jt = (JObject)JsonConvert.DeserializeObject(strdata.ToString());
string version = "";
version = jt.GetValue("H_MSG") == null ? "" : jt.GetValue("H_MSG").ToString();//获取meshelepr的版本
MessageBox.Show("初始化链接MES 成功 MEShelper版本" + version);//strdata会返回一个meshelper的版本号表示初始化成功
}
else
{
hMes = 0;
MessageBox.Show("初始化链接MES 失败 ");
}
;
}
else
{
MessageBox.Show("请勿重复初始化MES链接");
}
}
private void btMesStart_Click(object sender, EventArgs e)//messtart函数用来执行一般过站操作只能使用sn过站如需使用别的类型过站请使用MesStart2函数
{
if (hMes != 0)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
//StringBuilder SN = new StringBuilder("12345678901002");
//StringBuilder ActionName = new StringBuilder("BT");
//StringBuilder toolName = new StringBuilder("PRESSTOOL");
string SN = tBSN.Text;
string ActionName = tBStationHand.Text;
string toolName = tBTool.Text;
string data = "";
string msg = "";
string retdata = "";
string needload = "";
if (0 == MesStart(hMes, SN, ActionName, toolName, strdata, ref len))
{
MessageBox.Show("链接MES 成功 /r/n" + strdata.ToString());
//start参数的解析具体字段定义请参考《华勤MESHelper接口工具使用说明》
JObject jo = (JObject)JsonConvert.DeserializeObject(strdata.ToString());
data = jo.GetValue("DATA") == null ? "" : jo.GetValue("DATA").ToString();
//data是一个json字符串其中包含mes返回的主要字段按照这个参考继续解析出来 例如获取CSN
JObject jb = (JObject)JsonConvert.DeserializeObject(data);
string CSN = jb.GetValue("CSN") ==null?"":jb.GetValue("CSN").ToString();
//
msg = jo.GetValue("H_MSG") == null ? "" : jo.GetValue("H_MSG").ToString();
retdata = jo.GetValue("G_RET_DATA") == null ? "" : jo.GetValue("G_RET_DATA").ToString();//标记工具需要回传给MES的字段是json格式将其解析出来可以得到字段工具必须将该字段赋值并回传给MES在endmes函数回传
needload = jo.GetValue("NeedLoad") == null ? "" : jo.GetValue("NeedLoad").ToString();//标记工具是否需要重启
//解析结束
}
else
{
JObject jo = (JObject)JsonConvert.DeserializeObject(strdata.ToString());
msg = jo.GetValue("H_MSG") == null ? "" : jo.GetValue("H_MSG").ToString();//mes返回的错误信息最好将其在工具界面展示出来给操作人员查看
MessageBox.Show("链接MES 失败 /r/n" + strdata.ToString());
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
private void btMesGet_Click(object sender, EventArgs e)
{
if (hMes != 0)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
string SN = tBSN.Text.ToString();
if (0 == MesSaveAndGetExtraInfo(hMes, "2", "BATROLL", SN, null, null, strdata, ref len))
{
MessageBox.Show("链接MES 成功 /r/n" + strdata.ToString());
}
else
{
MessageBox.Show("链接MES 失败 /r/n" + strdata.ToString());
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
private void btMesSet_Click(object sender, EventArgs e)
{
if (hMes != 0)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
string SN = tBSN.Text.ToString();
string RollData = "Max:5.100kg;Min:4.800kg;RollNum:3;Time:10s";
if (0 == MesSaveAndGetExtraInfo(hMes, "1", "BATROLL", SN, RollData, null, strdata, ref len))
{
MessageBox.Show("保存数据到MES 成功 /r/n" + strdata.ToString());
}
else
{
MessageBox.Show("保存数据到MES 失败 /r/n" + strdata.ToString());
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
private void btMesEnd_Click(object sender, EventArgs e)//mesend函数仅仅向mes回传工具运行情况的错误码和错误描述如需向mes回传特定字段请使用MesEnd2函数
{
if (hMes != 0)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
strdata.Append(textBox3.Text);//错误描述
string SN = tBSN.Text.ToString();
string ActionName = tBStationHand.Text;//工具动作
string toolName = tBTool.Text;//工具版本
string ErrorCode = textBox1.Text;//错误码 0表示测试成功其它表示失败
string nextws = "";//表示下一个工位
string msg = "";//过站的错误信息
if (0 == MesEnd(hMes, SN, ActionName, toolName, ErrorCode, strdata, ref len))
{
JObject jo = (JObject)JsonConvert.DeserializeObject(strdata.ToString());
msg = jo.GetValue("H_MSG") == null ? "" : jo.GetValue("H_MSG").ToString();
nextws = jo.GetValue("G_NEXTWS") == null ? "" : jo.GetValue("G_NEXTWS").ToString();
MessageBox.Show("保存数据到MES 成功 /r/n" + strdata.ToString());
}
else
{
JObject jo = (JObject)JsonConvert.DeserializeObject(strdata.ToString());
msg = jo.GetValue("H_MSG") == null ? "" : jo.GetValue("H_MSG").ToString();//表示过站的错误信息,最好展示在工具的界面方便操作人员查看
MessageBox.Show("保存数据到MES 失败 /r/n" + strdata.ToString());
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
private void btMesUnInit_Click(object sender, EventArgs e)
{
if (hMes != 0)
{
if (0 == MesUnInit(hMes))
{
MessageBox.Show("释放MES链接 成功 ");
}
else
{
MessageBox.Show("释放MES链接 失败 ");
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (hMes != 0)
{
//StringBuilder SN = new StringBuilder("12345678901002");
//StringBuilder ActionName = new StringBuilder("BT");
//StringBuilder toolName = new StringBuilder("PRESSTOOL");
string SN = tBSN.Text;
string ActionName = tBStationHand.Text;
string toolName = tBTool.Text;
for (int i = 0; i < 16; i++)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
if (0 == MesStart(hMes, SN, ActionName, toolName, strdata, ref len))
{
//MessageBox.Show("链接MES 成功 /r/n" + strdata.ToString());
}
else
{
//MessageBox.Show("链接MES 失败 /r/n" + strdata.ToString());
}
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
if (hMes != 0)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
strdata.Append(textBox1.Text);
string SN = tBSN.Text;
string ActionName = tBStationHand.Text;
string toolName = tBTool.Text;
string ErrorCode = textBox3.Text;
string AllData = textBox2.Text;//表示需要回传给mes的字段json字符串形式传递
string SNType = "1";//
//start
//假设需要回传的字段为a值为1,将字段a上抛
//JObject data = new JObject();
//data.Add("a", "1");
//AllData = data.ToString();
//当遇到数据较多的时候可以使用Dictionary类型存储数据然后序列化
//end
if (0 == MesEnd2(hMes, SN, "1", ActionName, toolName, ErrorCode, AllData, strdata, ref len))
{
//这个函数返回的值跟MesEnd一样请参考MesEnd的解析参数的操作
MessageBox.Show("保存数据到MES 成功 /r/n" + strdata.ToString());
}
else
{
MessageBox.Show("保存数据到MES 失败 /r/n" + strdata.ToString());
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (hMes != 0)
{
int len = 102400;
StringBuilder strdata = new StringBuilder(len);
//StringBuilder SN = new StringBuilder("12345678901002");
//StringBuilder ActionName = new StringBuilder("BT");
//StringBuilder toolName = new StringBuilder("PRESSTOOL");
string SN = tBSN.Text;
string ActionName = tBStationHand.Text;
string toolName = tBTool.Text;
string ErrorCode = textBox1.Text;
string ErrorDes = textBox3.Text;
string AllData = textBox2.Text;
string SNType = "1";//表示流程检查的输入数据类型1 SN 2 IMEI 3 CSN 4 蓝牙 5 WiFi 6 入网证
if (0 == MesStart2(hMes, SN,SNType, ActionName, toolName, strdata, ref len))
{
//这个函数返回的值跟MesStart一样请参考MesStart的解析参数的操作
MessageBox.Show("链接MES 成功 /r/n" + strdata.ToString());
}
else
{
MessageBox.Show("链接MES 失败 /r/n" + strdata.ToString());
}
}
else
{
MessageBox.Show("请初始化MES链接");
}
}
}
}