串口读取数据修改

apiclient现场测试
This commit is contained in:
zhougf committed 2025-03-07 22:07:51 +08:00
1 parent 6c7278ed45
commit 2bd24f5316
3 files changed
+149 -80

No files matched your search

+17 -3
View File
@@ -121,6 +121,8 @@ namespace WindowsFormsApp1
//机台状态 //机台状态
机台状态.Text = "机台状态:" + Enum.GetName(typeof(plcstatus), Init.Plcstatus); 机台状态.Text = "机台状态:" + Enum.GetName(typeof(plcstatus), Init.Plcstatus);
int oldstatus = Init.Plcstatus; int oldstatus = Init.Plcstatus;
if (PlcData.Read_Status != 0)
{
DateTime oldPlcstatus_LastTime = Init.Plcstatus_LastTime; DateTime oldPlcstatus_LastTime = Init.Plcstatus_LastTime;
//状态+报警 //状态+报警
if (oldstatus != PlcData.Read_Status || (Plc_Time - oldPlcstatus_LastTime).TotalSeconds > 5) if (oldstatus != PlcData.Read_Status || (Plc_Time - oldPlcstatus_LastTime).TotalSeconds > 5)
@@ -153,6 +155,7 @@ namespace WindowsFormsApp1
continue; continue;
} }
} }
}
//机台报警 //机台报警
if (PlcData.Read_Status == (short)plcstatus.停机) if (PlcData.Read_Status == (short)plcstatus.停机)
{ {
@@ -201,7 +204,7 @@ namespace WindowsFormsApp1
{ {
continue; continue;
} }
else if (PlcData.Read_Trigger != 0 && PlcData.Write_Status != 0) else if (PlcData.Read_Trigger == PlcData.Write_Status)
{ {
continue; continue;
} }
@@ -217,6 +220,8 @@ namespace WindowsFormsApp1
} }
continue; continue;
} }
LogExecute.logNet.WriteDebug($"读取状态字{PlcData.Read_Trigger}");
var nowflow = Init.t_Flows.FirstOrDefault(a => a.hopestatus == PlcData.Read_Trigger); var nowflow = Init.t_Flows.FirstOrDefault(a => a.hopestatus == PlcData.Read_Trigger);
if (nowflow == null) if (nowflow == null)
{ {
@@ -229,6 +234,7 @@ namespace WindowsFormsApp1
LogExecute.logNet.WriteError($"未找到当前期望接口ID {nowflow.hopeinterfaceid},对应的接口!"); LogExecute.logNet.WriteError($"未找到当前期望接口ID {nowflow.hopeinterfaceid},对应的接口!");
continue; continue;
} }
#endregion #endregion
#region 根据流程,判断是否需要扫码、气密、扭力等 #region 根据流程,判断是否需要扫码、气密、扭力等
if (nowflow.scan) if (nowflow.scan)
@@ -602,7 +608,7 @@ namespace WindowsFormsApp1
int textLength = richTextBox1.TextLength; int textLength = richTextBox1.TextLength;
int lineCount = richTextBox1.GetLineFromCharIndex(textLength); int lineCount = richTextBox1.GetLineFromCharIndex(textLength);
if (lineCount >= 20) if (lineCount >= 200)
{ {
richTextBox1.Clear(); richTextBox1.Clear();
} }
@@ -820,7 +826,15 @@ namespace WindowsFormsApp1
Init.AirtightPort?.Open(); Init.AirtightPort?.Open();
} }
// 读取串口接收到的数据 // 读取串口接收到的数据
string ReturnValue = Init.AirtightPort.ReadExisting(); // 读取串口接收到的数据
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 index1 = 1;
int index2 = 1; int index2 = 1;
+8 -1
View File
@@ -66,7 +66,14 @@ namespace WindowsFormsApp1
Init.ScanPort?.Open(); Init.ScanPort?.Open();
} }
// 读取串口接收到的数据 // 读取串口接收到的数据
ReturnValue = Init.ScanPort.ReadExisting(); var dataBuilder = new StringBuilder();
while(Init.ScanPort.BytesToRead > 0)
{
System.Threading.Thread.Sleep(100);
dataBuilder.Append(Init.ScanPort.ReadExisting());
}
ReturnValue = dataBuilder.ToString();
if (this.InvokeRequired) if (this.InvokeRequired)
{ {
this.Invoke(new Action(() => this.Invoke(new Action(() =>
+124 -76
View File
@@ -43,7 +43,7 @@ namespace WindowsFormsApp1.server
_line = "Testline"; _line = "Testline";
_stationID = "TestStation"; _stationID = "TestStation";
_machineID = "TestMachine"; _machineID = "TestMachine";
_opID = "OPID"; _opID = "";
} }
// 通用API调用方法,减少代码重复 // 通用API调用方法,减少代码重复
@@ -58,6 +58,16 @@ namespace WindowsFormsApp1.server
// 调用接口 // 调用接口
try try
{ {
// 测试
LogExecute.logNet.WriteDebug($"{eventName} 接口请求成功\n请求:{jsonPayload}");
// 回写对应PLC
if (statusCode > 0)
PlcData.Write_Status = (short)statusCode;
if (eventName == "SN_Release")
PlcData.Write_stSN = "testsn";
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
/*
var response = _httpClient.PostAsync(url, httpContent).Result; var response = _httpClient.PostAsync(url, httpContent).Result;
// 检查响应状态码 // 检查响应状态码
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
@@ -71,18 +81,24 @@ namespace WindowsFormsApp1.server
// 访问动态对象的属性 // 访问动态对象的属性
if (res != null) if (res != null)
{ {
LogExecute.logNet.WriteDebug($"{eventName} 接口请求成功\n请求:{jsonPayload}\n 返回: {res}"); LogExecute.logNet.WriteDebug($"{eventName} 接口请求成功\n请求:{jsonPayload}\n 返回: {responseContent}");
// 成功! // 成功!
if (res.RESULT != null && !string.IsNullOrEmpty(res.RESULT.Value)) if (res.Result != null && !string.IsNullOrEmpty(res.Result.Value))
{
var result = res.Result.Value == "PASS";
if (result)
{ {
// 回写对应PLC // 回写对应PLC
if (statusCode > 0) if (statusCode > 0)
PlcData.Write_Status = (short)statusCode; PlcData.Write_Status = (short)statusCode;
if(eventName == "SN_Release")
return res.RESULT.Value == "PASS" PlcData.Write_stSN = res.SN.Value;
}
return result
? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now) ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now); : ApiFormResult.Sucess(mesresult.FAIL, true, res.MSG.Value, PlcData, DateTime.Now);
} }
else if (res.IsSuccess != null) // 打印 mes不返回result else if (res.IsSuccess != null) // 打印 mes不返回result
{ {
@@ -99,13 +115,14 @@ namespace WindowsFormsApp1.server
{ {
throw new Exception($"请求失败,状态码:{response.StatusCode}"); throw new Exception($"请求失败,状态码:{response.StatusCode}");
} }
*/
} }
catch (Exception ex) catch (Exception ex)
{ {
LogExecute.logNet.WriteError($"{eventName} 接口请求失败: {ex.Message}"); LogExecute.logNet.WriteError($"{eventName} 接口请求失败: {ex.Message}");
throw new Exception(ex.Message); throw new Exception(ex.Message);
} }
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now); //return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
} }
public ApiFormResult SN_CheckIN(string url, plcdata PlcData, otherdata OtherData) public ApiFormResult SN_CheckIN(string url, plcdata PlcData, otherdata OtherData)
@@ -120,9 +137,9 @@ namespace WindowsFormsApp1.server
OPID = _opID, OPID = _opID,
Token = "", Token = "",
FixSN = "", FixSN = "",
SN = PlcData.Read_MaterialCode0, SN = PlcData.Read_MaterialCode1,
CarrierID = PlcData.Read_MaterialCode1, CarrierID = "",
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "SN_CheckIN", PlcData, PLC_Status.SN_CheckIN); return CallApi(url, request, "SN_CheckIN", PlcData, PLC_Status.SN_CheckIN);
@@ -141,7 +158,7 @@ namespace WindowsFormsApp1.server
Token = "", Token = "",
FixSN = "", FixSN = "",
CarrierID = PlcData.Read_MaterialCode1, CarrierID = PlcData.Read_MaterialCode1,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "Carrier_Check", PlcData, PLC_Status.Carrier_Check); return CallApi(url, request, "Carrier_Check", PlcData, PLC_Status.Carrier_Check);
@@ -160,7 +177,7 @@ namespace WindowsFormsApp1.server
Token = "", Token = "",
FixSN = "", FixSN = "",
SN = OtherData.JS_SN,//扫码 SN = OtherData.JS_SN,//扫码
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "SN_FeedingCheck", PlcData, PLC_Status.SN_FeedingCheck); return CallApi(url, request, "SN_FeedingCheck", PlcData, PLC_Status.SN_FeedingCheck);
@@ -179,7 +196,7 @@ namespace WindowsFormsApp1.server
Token = "", Token = "",
FixSN = "", FixSN = "",
SN = OtherData.JS_SN, SN = OtherData.JS_SN,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "Link_Comp", PlcData, PLC_Status.Link_Comp); return CallApi(url, request, "Link_Comp", PlcData, PLC_Status.Link_Comp);
@@ -197,47 +214,12 @@ namespace WindowsFormsApp1.server
OPID = _opID, OPID = _opID,
Token = "", Token = "",
FixSN = "", FixSN = "",
SN = OtherData.JS_SN, SN = Init.ScanString,
CarrierID = "", CarrierID = "",
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
// 特殊处理:需要设置Write_stSN // 特殊处理:需要设置Write_stSN
string jsonPayload = JsonConvert.SerializeObject(request); return CallApi(url, request, "SN_Release", PlcData, PLC_Status.SN_Release);
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
try
{
var response = _httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
string responseContent = response.Content.ReadAsStringAsync().Result;
dynamic res = JsonConvert.DeserializeObject(responseContent);
if (res != null)
{
LogExecute.logNet.WriteDebug($"SN_Release 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
if (!string.IsNullOrEmpty(res.RESULT.Value))
{
PlcData.Write_Status = PLC_Status.SN_Release;
PlcData.Write_stSN = res.MSG.Value; // 特殊处理
return res.RESULT.Value == "PASS"
? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
}
}
}
else
{
throw new Exception($"请求失败,状态码:{response.StatusCode}");
}
}
catch (Exception ex)
{
LogExecute.logNet.WriteError($"SN_Release 接口请求失败: {ex.Message}");
throw new Exception(ex.Message);
}
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
} }
public ApiFormResult Glue_CheckOut(string url, plcdata PlcData, otherdata OtherData) public ApiFormResult Glue_CheckOut(string url, plcdata PlcData, otherdata OtherData)
@@ -253,7 +235,7 @@ namespace WindowsFormsApp1.server
Token = "",//判断站,胶水不用传 Token = "",//判断站,胶水不用传
FixSN = "",//判断站,胶水不用传 FixSN = "",//判断站,胶水不用传
SN = PlcData.Read_MaterialCode0, SN = PlcData.Read_MaterialCode0,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "Glue_CheckOut", PlcData, PLC_Status.Glue_CheckOut); return CallApi(url, request, "Glue_CheckOut", PlcData, PLC_Status.Glue_CheckOut);
@@ -270,7 +252,7 @@ namespace WindowsFormsApp1.server
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
SN = OtherData.JS_SN, SN = OtherData.JS_SN,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "Data_Collection", PlcData, PLC_Status.Data_Collection); return CallApi(url, request, "Data_Collection", PlcData, PLC_Status.Data_Collection);
@@ -287,10 +269,9 @@ namespace WindowsFormsApp1.server
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
STATUS = OtherData.Plc_Status, STATUS = OtherData.Plc_Status,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
// 注意:这个API不需要设置PlcData.Write_Status
return CallApi(url, request, "Status", PlcData, 0); return CallApi(url, request, "Status", PlcData, 0);
} }
@@ -305,11 +286,11 @@ namespace WindowsFormsApp1.server
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
AlarmID = OtherData.Plc_ErrorMsgs[0], AlarmID = OtherData.Plc_ErrorMsgs[0],
SendTime = DateTime.Now, SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),
ResetTime = "" ResetTime = ""
}; };
return CallApi(url, request, "Alarm", PlcData, PLC_Status.Alarm); return CallApi(url, request, "Alarm", PlcData, 0);
} }
public ApiFormResult SN_FileUpload(string url, plcdata PlcData, otherdata OtherData) public ApiFormResult SN_FileUpload(string url, plcdata PlcData, otherdata OtherData)
@@ -336,7 +317,7 @@ namespace WindowsFormsApp1.server
} }
} }
}, },
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "SN_FileUpload", PlcData, PLC_Status.SN_FileUpload); return CallApi(url, request, "SN_FileUpload", PlcData, PLC_Status.SN_FileUpload);
@@ -362,17 +343,48 @@ namespace WindowsFormsApp1.server
var lineCode = Init.t_Line.linecode; var lineCode = Init.t_Line.linecode;
// 基础请求对象 // 基础请求对象
var request = new var request = new CheckOutObject()
{ {
EventID = "SN_Checkout", EventID = "SN_Checkout",
Line = _line, Line = _line,
StationID = _stationID, StationID = _stationID,
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
SN = OtherData.JS_SN, SNInfo = new List<SNInfoItem>()
SendTime = DateTime.Now {
new SNInfoItem()
{
SN = PlcData.Read_MaterialCode0,
Result = PlcData.Read_Result == 1 ? "PASS" : "FAIL",
DC_INFO = new List<DCInfoItem>()
{
new DCInfoItem()
{
Item = PlcData.Read_MaterialCode2,
Value = PlcData.Read_UploadData1.ToString(),
Result = PlcData.Read_Result == 1 ? "PASS" : "FAIL"
}
},
CompList = new List<CompListItem>()
{
new CompListItem()
{
CompID = "",
Qty = "1"
}
}
}
},
Qty = "",
CarrierID = PlcData.Read_MaterialCode1,
Mold = "",
Token = "",
FixSN = "",
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
//LogExecute.logNet.WriteDebug($"checkout:{request}");
// 根据不同的lineCode可以在这里扩展request对象 // 根据不同的lineCode可以在这里扩展request对象
switch (lineCode) switch (lineCode)
{ {
@@ -386,6 +398,11 @@ namespace WindowsFormsApp1.server
case "OP1010":// 传气密 case "OP1010":// 传气密
case "OP1330": case "OP1330":
var dcInfo = new DCInfoItem();
dcInfo.Item = PlcData.Read_MaterialCode2;
dcInfo.Value = "气密测试值";
dcInfo.Result = "PASS";
//request.SNInfo
break; break;
case "OP1050":// 胶水码 case "OP1050":// 胶水码
@@ -426,17 +443,6 @@ namespace WindowsFormsApp1.server
public ApiFormResult Initialization(string url, plcdata PlcData, otherdata OtherData) public ApiFormResult Initialization(string url, plcdata PlcData, otherdata OtherData)
{ {
// 组装 request json
var request = new
{
EventID = "Initialization",
Line = _line,
StationID = _stationID,
MachineID = _machineID,
OPID = _opID,
SendTime = DateTime.Now
};
// 直接设置状态码 // 直接设置状态码
PlcData.Write_Status = PLC_Status.Initialization; PlcData.Write_Status = PLC_Status.Initialization;
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now); return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
@@ -452,7 +458,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID, StationID = _stationID,
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "Test", PlcData, PLC_Status.Test); return CallApi(url, request, "Test", PlcData, PLC_Status.Test);
@@ -467,7 +473,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID, StationID = _stationID,
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "OEEDataCollection", PlcData, PLC_Status.OEEDataCollection); return CallApi(url, request, "OEEDataCollection", PlcData, PLC_Status.OEEDataCollection);
@@ -482,7 +488,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID, StationID = _stationID,
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "Get_Para", PlcData, PLC_Status.Get_Para); return CallApi(url, request, "Get_Para", PlcData, PLC_Status.Get_Para);
@@ -497,7 +503,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID, StationID = _stationID,
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
SendTime = DateTime.Now SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
return CallApi(url, request, "DispatchPara", PlcData, PLC_Status.DispatchPara); return CallApi(url, request, "DispatchPara", PlcData, PLC_Status.DispatchPara);
@@ -599,5 +605,47 @@ namespace WindowsFormsApp1.server
public const int Fail = 1001; public const int Fail = 1001;
} }
//checkout class
// 根对象类
public class CheckOutObject
{
public List<SNInfoItem> SNInfo { get; set; }
public string Qty { get; set; }
public string Mold { get; set; }
public string EventID { get; set; }
public string Line { get; set; }
public string StationID { get; set; }
public string MachineID { get; set; }
public string CarrierID { get; set; }
public string OPID { get; set; }
public string SendTime { get; set; }
public string Token { get; set; }
public string FixSN { get; set; }
}
// SNInfo 列表中的每个元素类
public class SNInfoItem
{
public string SN { get; set; }
public string Result { get; set; }
public List<DCInfoItem> DC_INFO { get; set; }
public List<CompListItem> CompList { get; set; }
}
// DC_INFO 列表中的每个元素类
public class DCInfoItem
{
public string Item { get; set; }
public string Value { get; set; }
public string Result { get; set; }
}
// CompList 列表中的每个元素类
public class CompListItem
{
public string CompID { get; set; }
public string Qty { get; set; }
}
} }
} }