重新修改

This commit is contained in:
YZJ 2024-07-15 17:15:47 +08:00
parent feeda6a550
commit 791bfb85d2
2 changed files with 741 additions and 765 deletions

File diff suppressed because it is too large Load Diff

View File

@ -283,6 +283,65 @@ namespace HisenceYoloDetection
return content; return content;
} }
public static string PostData(string url, string postData)
{
byte[] data = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
static string FindStringAfterColon(string response, string targetString)
{
int targetIndex = response.IndexOf(targetString);
if (targetIndex == -1)
{
Console.WriteLine($"未找到字符串: {targetString}");
return null;
}
// 查找目标字符串后的冒号和引号
int colonIndex = response.IndexOf(':', targetIndex);
if (colonIndex == -1 || colonIndex <= targetIndex)
{
Console.WriteLine("在目标字符串后未找到冒号");
return null;
}
// 查找引号开始的位置
int startQuoteIndex = response.IndexOf('"', colonIndex + 1);
if (startQuoteIndex == -1)
{
Console.WriteLine("未找到引号");
return null;
}
// 查找闭合引号的位置
int endQuoteIndex = response.IndexOf('"', startQuoteIndex + 1);
if (endQuoteIndex == -1)
{
Console.WriteLine("未找到闭合的引号");
return null;
}
// 提取并返回引号内的字符串
return response.Substring(startQuoteIndex + 1, endQuoteIndex - startQuoteIndex - 1);
}
#region Post请求 #region Post请求
/// <summary> /// <summary>
/// http Post请求 /// http Post请求
@ -347,59 +406,46 @@ namespace HisenceYoloDetection
public void GetTypeFromPost() public void GetTypeFromPost()
{ {
string url = "http://172.16.139.146/FCCBOMWebService/FCCBomService.asmx/GetFCCBomInfoPDJson"; string url = "http://172.16.139.146/FCCBOMWebService/FCCBomService.asmx/GetFCCBomInfoPDJson";
string jsonData = @"
DateTime dt = DateTime.Now;
string today = dt.ToString("yyyy-MM-dd");
string startTime = today;
string endTime = today;
string lineBody = "BZ-PC";
string materialDescript = "皂盒";
JObject patientInfo = new JObject
{ {
["StartTime"] = startTime, ""StartTime"": ""2024-07-15"",
["EndTime"] = endTime, ""EndTime"": ""2024-07-15"",
["LineBody"] = lineBody, ""LineBody"": ""BZ-PC""
["MaterialDescripts"] = new JArray { materialDescript } }";
};
string sendData = patientInfo.ToString();
string resultData = Post(sendData, url, "application/x-www-form-urlencoded", "application/json");
if (resultData != null) string postData = "inputvalue=" + Uri.EscapeDataString(jsonData);
try
{ {
JObject responseJson = JObject.Parse(resultData); string response = PostData(url, postData);
bool success = responseJson["success"] != null && (bool)responseJson["success"];
if (success)
// 解析并处理响应
string finalDefIdToFind = m_sKEYENCEBar.Substring(0, m_sKEYENCEBar.Length - 7); // 目标 FinalDefID
string productType = FindStringAfterColon(response, finalDefIdToFind);
if (productType != null)
{ {
Console.WriteLine($"{finalDefIdToFind}: {productType}");
// string FinalDefIDToFind = m_sKEYENCEBar.Substring(1, 18);// 目标 FinalDefID // 将结果写入文件
JArray dataArray = responseJson["data"] as JArray;
if (dataArray != null)
{
foreach (JObject dataItem in dataArray)
{
string finalDefID = dataItem["FinalDefID"] != null ? dataItem["FinalDefID"].ToString() : "N/A";
string productType = dataItem["ProductType"] != null ? dataItem["ProductType"].ToString() : "N/A";
DayTypeDics.Add(finalDefID, productType);
}
}
} }
else else
{ {
string returnMsg = responseJson["ReturnMsg"] != null ? responseJson["ReturnMsg"].ToString() : "Unknown error"; Console.WriteLine($"FinalDefID {finalDefIdToFind} not found in response.");
Console.WriteLine("请求失败:" + returnMsg);
} }
} }
else catch (Exception ex)
{ {
Console.WriteLine("请求失败:未返回数据"); Console.WriteLine("Error occurred:");
Console.WriteLine(ex.Message);
} }
} }
/// <summary> /// <summary>
/// 主窗口 /// 主窗口
@ -411,59 +457,49 @@ namespace HisenceYoloDetection
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
sw.Start(); sw.Start();
string url = "http://172.16.139.146/FCCBOMWebService/FCCBomService.asmx/GetFCCBomInfoPDJson"; // string url = "http://172.16.139.146/FCCBOMWebService/FCCBomService.asmx/GetFCCBomInfoPDJson";
DateTime dt = DateTime.Now; // string jsonData = @"
string today = dt.ToString("yyyy-MM-dd"); //{
// ""StartTime"": ""2024-07-15"",
// ""EndTime"": ""2024-07-15"",
// ""LineBody"": ""BZ-PC""
// }";
string startTime = today;
string endTime = today;
string lineBody = "BZ-PC";
string materialDescript = "皂盒";
JObject patientInfo = new JObject // string postData = "inputvalue=" + Uri.EscapeDataString(jsonData);
{
["StartTime"] = startTime, // try
["EndTime"] = endTime, // {
["LineBody"] = lineBody, // string response = PostData(url, postData);
["MaterialDescripts"] = new JArray { materialDescript }
};
// // 解析并处理响应
// string finalDefIdToFind = m_sKEYENCEBar.Substring(0, m_sKEYENCEBar.Length - 7);
// string productType = FindStringAfterColon(response, finalDefIdToFind);
// if (productType != null)
// {
// Console.WriteLine($"{finalDefIdToFind}: {productType}");
// // 将结果写入文件
// }
// else
// {
// Console.WriteLine($"FinalDefID {finalDefIdToFind} not found in response.");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine("Error occurred:");
// Console.WriteLine(ex.Message);
// }
string sendData = patientInfo.ToString();
string resultData = Post(sendData, url, "application/x-www-form-urlencoded", "application/json");
if (resultData != null)
{
JObject responseJson = JObject.Parse(resultData);
bool success = responseJson["success"] != null && (bool)responseJson["success"];
if (success)
{
string FinalDefIDToFind = m_sKEYENCEBar.Substring(1, 18);// 目标 FinalDefID
JArray dataArray = responseJson["data"] as JArray;
if (dataArray != null)
{
foreach (JObject dataItem in dataArray)
{
if (dataItem["FinalDefID"] != null && dataItem["FinalDefID"].ToString() == FinalDefIDToFind)
{
string productType = dataItem["ProductType"] != null ? dataItem["ProductType"].ToString() : "N/A";
Console.WriteLine($"FinalDefID: {FinalDefIDToFind}, ProductType: {productType}");
break;
}
}
}
}
else
{
string returnMsg = responseJson["ReturnMsg"] != null ? responseJson["ReturnMsg"].ToString() : "Unknown error";
Console.WriteLine("请求失败:" + returnMsg);
}
}
else
{
Console.WriteLine("请求失败:未返回数据");
}
sw.Stop(); sw.Stop();
Console.WriteLine("时间请求i" + sw.ElapsedMilliseconds); Console.WriteLine("时间请求i" + sw.ElapsedMilliseconds);
@ -558,14 +594,10 @@ namespace HisenceYoloDetection
OnSpeedShow();//线程开启将速度显示在界面上 OnSpeedShow();//线程开启将速度显示在界面上
OnPostionShow();//线程开启将位置显示在界面上 OnPostionShow();//线程开启将位置显示在界面上
<<<<<<< HEAD
melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 1);//黄灯 melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 1);//黄灯
melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//绿灯 melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//绿灯
=======
melsecPLCTCPDriver.WriteInt("554", 1);//黄灯亮
melsecPLCTCPDriver.WriteInt("552", 0);//其他灯处于灭状态
melsecPLCTCPDriver.WriteInt("556", 0);
>>>>>>> 756d23229c59669d628dd94efb8876679bfe21f7
//t = new System.Timers.Timer(10000);//实例化Timer类设置间隔时间为10000毫秒 //t = new System.Timers.Timer(10000);//实例化Timer类设置间隔时间为10000毫秒
//t.Elapsed += new System.Timers.ElapsedEventHandler(Execute);//到达时间的时候执行事件; //t.Elapsed += new System.Timers.ElapsedEventHandler(Execute);//到达时间的时候执行事件;
//t.AutoReset = true;//设置是执行一次false还是一直执行(true) //t.AutoReset = true;//设置是执行一次false还是一直执行(true)
@ -1964,31 +1996,23 @@ namespace HisenceYoloDetection
} }
else else
{ {
<<<<<<< HEAD
melsecPLCTCPDriver.WriteInt(RedLightingAdress, 1);//红灯 melsecPLCTCPDriver.WriteInt(RedLightingAdress, 1);//红灯
melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//黄灯 melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//黄灯
melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//绿灯 melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//绿灯
melsecPLCTCPDriver.WriteInt(WaringAdress, 1);//报警 melsecPLCTCPDriver.WriteInt(WaringAdress, 1);//报警
=======
melsecPLCTCPDriver.WriteInt("552", 1);//红灯亮
melsecPLCTCPDriver.WriteInt("554", 0);//其他灯灭
melsecPLCTCPDriver.WriteInt("556", 0);
melsecPLCTCPDriver.WriteInt("558", 1);//报警
//加上人为判断是否是NG洗衣机 //加上人为判断是否是NG洗衣机
DialogResult dr = MessageBox.Show("是否误检?", "是否误检", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); DialogResult dr = MessageBox.Show("是否误检?", "是否误检", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK) if (dr == DialogResult.OK)
{ {
<<<<<<< HEAD
melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯 melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯
melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 1);//黄灯 melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 1);//黄灯
melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警 melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警
=======
melsecPLCTCPDriver.WriteInt("552", 0);//红灯灭
melsecPLCTCPDriver.WriteInt("554", 1);//黄灯亮
melsecPLCTCPDriver.WriteInt("558", 0);//报警
>>>>>>> 756d23229c59669d628dd94efb8876679bfe21f7
OKDsums++; OKDsums++;
WUsums++; WUsums++;
this.Invoke(new Action(() => this.Invoke(new Action(() =>
@ -2003,15 +2027,11 @@ namespace HisenceYoloDetection
else else
{ {
NGDsums++; NGDsums++;
<<<<<<< HEAD
melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警 melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警
melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯 melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯
melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 1);//黄灯 melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 1);//黄灯
=======
melsecPLCTCPDriver.WriteInt("558", 0);//报警
melsecPLCTCPDriver.WriteInt("552", 0);//红灯灭
melsecPLCTCPDriver.WriteInt("554", 1);//黄灯亮
>>>>>>> 756d23229c59669d628dd94efb8876679bfe21f7
this.Invoke(new Action(() => this.Invoke(new Action(() =>
{ {
@ -2321,18 +2341,14 @@ namespace HisenceYoloDetection
Rect rect2 = new Rect(0, 0, 0, 0); Rect rect2 = new Rect(0, 0, 0, 0);
CheckDiffSciHelper1.CheckDiffSci(path2, pathmat, rect2, rect2, false, "D://Hisence//Test1"); CheckDiffSciHelper1.CheckDiffSci(path2, pathmat, rect2, rect2, false, "D://Hisence//Test1");
<<<<<<< HEAD
*/ */
// OnGetBar(); // OnGetBar();
melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//绿灯 melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//绿灯
melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//黄灯 melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//黄灯
=======
*/
// OnGetBar();
melsecPLCTCPDriver.WriteInt("556", 1);//绿灯亮
melsecPLCTCPDriver.WriteInt("554", 0);//黄灯灭
TriggerCameral2();//光电触发拍照 根据SN获取型号 从数据库中得到块值 TriggerCameral2();//光电触发拍照 根据SN获取型号 从数据库中得到块值
ReadyDetect();//相机拍照后检测线程 ReadyDetect();//相机拍照后检测线程
ThreadPost();//相机拍照后获取当日的型号 ThreadPost();//相机拍照后获取当日的型号
@ -2686,56 +2702,43 @@ namespace HisenceYoloDetection
string productType = "N/A"; string productType = "N/A";
string url = "http://172.16.139.146/FCCBOMWebService/FCCBomService.asmx/GetFCCBomInfoPDJson"; string url = "http://172.16.139.146/FCCBOMWebService/FCCBomService.asmx/GetFCCBomInfoPDJson";
DateTime dt = DateTime.Now; string jsonData = @"
string today = dt.ToString("yyyy-MM-dd"); {
""StartTime"": ""2024-07-15"",
""EndTime"": ""2024-07-15"",
""LineBody"": ""BZ-PC""
}";
string startTime = today;
string endTime = today;
string lineBody = "BZ-PC";
string materialDescript = "皂盒";
JObject patientInfo = new JObject string postData = "inputvalue=" + Uri.EscapeDataString(jsonData);
{
["StartTime"] = startTime,
["EndTime"] = endTime,
["LineBody"] = lineBody,
["MaterialDescripts"] = new JArray { materialDescript }
};
string sendData = patientInfo.ToString(); try
string resultData = Post(sendData, url, "application/x-www-form-urlencoded", "application/json"); {
string response = PostData(url, postData);
if (resultData != null)
{
JObject responseJson = JObject.Parse(resultData);
bool success = responseJson["success"] != null && (bool)responseJson["success"];
if (success)
{
string FinalDefIDToFind = SN.Substring(1, 18);// 目标 FinalDefID
JArray dataArray = responseJson["data"] as JArray; // 解析并处理响应
if (dataArray != null) string finalDefIdToFind = m_sKEYENCEBar.Substring(0, m_sKEYENCEBar.Length - 7); // 目标 FinalDefID
productType = FindStringAfterColon(response, finalDefIdToFind);
if (productType != null)
{ {
foreach (JObject dataItem in dataArray) Console.WriteLine($"{finalDefIdToFind}: {productType}");
{
if (dataItem["FinalDefID"] != null && dataItem["FinalDefID"].ToString() == FinalDefIDToFind) // 将结果写入文件
{
productType = dataItem["ProductType"] != null ? dataItem["ProductType"].ToString() : "N/A";
Console.WriteLine($"FinalDefID: {FinalDefIDToFind}, ProductType: {productType}");
break;
}
}
}
} }
else else
{ {
string returnMsg = responseJson["ReturnMsg"] != null ? responseJson["ReturnMsg"].ToString() : "Unknown error"; Console.WriteLine($"FinalDefID {finalDefIdToFind} not found in response.");
Console.WriteLine("请求失败:" + returnMsg);
} }
} }
else catch (Exception ex)
{ {
Console.WriteLine("请求失败:未返回数据"); Console.WriteLine("Error occurred:");
Console.WriteLine(ex.Message);
} }
return productType; return productType;
// sw.Stop(); // sw.Stop();
@ -3793,7 +3796,7 @@ namespace HisenceYoloDetection
{ {
if (!m_sKEYENCEBar.IsNullOrEmpty()) if (!m_sKEYENCEBar.IsNullOrEmpty())
{ {
OcrBarBox.Text = m_sKEYENCEBar; OcrBarBox.Text = m_sKEYENCEBar.Substring(0, m_sKEYENCEBar.Length - 7);
} }
} }