串口读取数据修改

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

No files matched your search

+42 -28
View File
@@ -121,37 +121,40 @@ namespace WindowsFormsApp1
//机台状态
机台状态.Text = "机台状态:" + Enum.GetName(typeof(plcstatus), Init.Plcstatus);
int oldstatus = Init.Plcstatus;
DateTime oldPlcstatus_LastTime = Init.Plcstatus_LastTime;
//状态+报警
if (oldstatus != PlcData.Read_Status || (Plc_Time - oldPlcstatus_LastTime).TotalSeconds > 5)
if (PlcData.Read_Status != 0)
{
//反馈状态
Init.Plcstatus = PlcData.Read_Status;
Init.Plcstatus_LastTime = DateTime.Now;
otherdata.Plc_Status = PlcData.Read_Status;
var statusresult = apiClient.CallFunctionByName("Status", PlcData, otherdata);
if (statusresult.Success)
DateTime oldPlcstatus_LastTime = Init.Plcstatus_LastTime;
//状态+报警
if (oldstatus != PlcData.Read_Status || (Plc_Time - oldPlcstatus_LastTime).TotalSeconds > 5)
{
MES链接状态.Text = "MES链接状态:OK";
MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
MES_MsgfORM("Status", statusresult.Mesresult, statusresult.Requesttime);
if (statusresult.Mesresult != mesresult.PASS)
//反馈状态
Init.Plcstatus = PlcData.Read_Status;
Init.Plcstatus_LastTime = DateTime.Now;
otherdata.Plc_Status = PlcData.Read_Status;
var statusresult = apiClient.CallFunctionByName("Status", PlcData, otherdata);
if (statusresult.Success)
{
PlcData.Write_Status = 1001;
siemensHelp.WriteAddress(plcaddress, PlcData);
MES链接状态.Text = "MES链接状态:OK";
MES链接状态.BackColor = SystemColors.Control;
MES通讯时间.Text = "MES通讯时间:" + DateTime.Now.ToString("MM-dd HH:mm:ss");
MES_MsgfORM("Status", statusresult.Mesresult, statusresult.Requesttime);
if (statusresult.Mesresult != mesresult.PASS)
{
PlcData.Write_Status = 1001;
siemensHelp.WriteAddress(plcaddress, PlcData);
continue;
}
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
var errresult = siemensHelp.WriteAddress(plcaddress, PlcData);
continue;
}
}
else
{
MES链接状态.Text = "MES链接状态:NG";
MES链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"MES通讯异常!");
PlcData.Write_Status = 1002;
var errresult = siemensHelp.WriteAddress(plcaddress, PlcData);
continue;
}
}
//机台报警
if (PlcData.Read_Status == (short)plcstatus.停机)
@@ -201,7 +204,7 @@ namespace WindowsFormsApp1
{
continue;
}
else if (PlcData.Read_Trigger != 0 && PlcData.Write_Status != 0)
else if (PlcData.Read_Trigger == PlcData.Write_Status)
{
continue;
}
@@ -217,6 +220,8 @@ namespace WindowsFormsApp1
}
continue;
}
LogExecute.logNet.WriteDebug($"读取状态字{PlcData.Read_Trigger}");
var nowflow = Init.t_Flows.FirstOrDefault(a => a.hopestatus == PlcData.Read_Trigger);
if (nowflow == null)
{
@@ -229,6 +234,7 @@ namespace WindowsFormsApp1
LogExecute.logNet.WriteError($"未找到当前期望接口ID {nowflow.hopeinterfaceid},对应的接口!");
continue;
}
#endregion
#region 根据流程,判断是否需要扫码、气密、扭力等
if (nowflow.scan)
@@ -602,7 +608,7 @@ namespace WindowsFormsApp1
int textLength = richTextBox1.TextLength;
int lineCount = richTextBox1.GetLineFromCharIndex(textLength);
if (lineCount >= 20)
if (lineCount >= 200)
{
richTextBox1.Clear();
}
@@ -820,7 +826,15 @@ namespace WindowsFormsApp1
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 index2 = 1;
+8 -1
View File
@@ -66,7 +66,14 @@ namespace WindowsFormsApp1
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)
{
this.Invoke(new Action(() =>
+126 -78
View File
@@ -43,7 +43,7 @@ namespace WindowsFormsApp1.server
_line = "Testline";
_stationID = "TestStation";
_machineID = "TestMachine";
_opID = "OPID";
_opID = "";
}
// 通用API调用方法,减少代码重复
@@ -58,6 +58,16 @@ namespace WindowsFormsApp1.server
// 调用接口
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;
// 检查响应状态码
if (response.IsSuccessStatusCode)
@@ -71,18 +81,24 @@ namespace WindowsFormsApp1.server
// 访问动态对象的属性
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))
{
// 回写对应PLC
if (statusCode > 0)
PlcData.Write_Status = (short)statusCode;
var result = res.Result.Value == "PASS";
return res.RESULT.Value == "PASS"
if (result)
{
// 回写对应PLC
if (statusCode > 0)
PlcData.Write_Status = (short)statusCode;
if(eventName == "SN_Release")
PlcData.Write_stSN = res.SN.Value;
}
return result
? 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
{
@@ -99,13 +115,14 @@ namespace WindowsFormsApp1.server
{
throw new Exception($"请求失败,状态码:{response.StatusCode}");
}
*/
}
catch (Exception ex)
{
LogExecute.logNet.WriteError($"{eventName} 接口请求失败: {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)
@@ -120,9 +137,9 @@ namespace WindowsFormsApp1.server
OPID = _opID,
Token = "",
FixSN = "",
SN = PlcData.Read_MaterialCode0,
CarrierID = PlcData.Read_MaterialCode1,
SendTime = DateTime.Now
SN = PlcData.Read_MaterialCode1,
CarrierID = "",
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
};
return CallApi(url, request, "SN_CheckIN", PlcData, PLC_Status.SN_CheckIN);
@@ -141,7 +158,7 @@ namespace WindowsFormsApp1.server
Token = "",
FixSN = "",
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);
@@ -160,7 +177,7 @@ namespace WindowsFormsApp1.server
Token = "",
FixSN = "",
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);
@@ -179,7 +196,7 @@ namespace WindowsFormsApp1.server
Token = "",
FixSN = "",
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);
@@ -197,47 +214,12 @@ namespace WindowsFormsApp1.server
OPID = _opID,
Token = "",
FixSN = "",
SN = OtherData.JS_SN,
SN = Init.ScanString,
CarrierID = "",
SendTime = DateTime.Now
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
};
// 特殊处理:需要设置Write_stSN
string jsonPayload = JsonConvert.SerializeObject(request);
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);
return CallApi(url, request, "SN_Release", PlcData, PLC_Status.SN_Release);
}
public ApiFormResult Glue_CheckOut(string url, plcdata PlcData, otherdata OtherData)
@@ -253,7 +235,7 @@ namespace WindowsFormsApp1.server
Token = "",//判断站,胶水不用传
FixSN = "",//判断站,胶水不用传
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);
@@ -270,7 +252,7 @@ namespace WindowsFormsApp1.server
MachineID = _machineID,
OPID = _opID,
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);
@@ -287,10 +269,9 @@ namespace WindowsFormsApp1.server
MachineID = _machineID,
OPID = _opID,
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);
}
@@ -305,11 +286,11 @@ namespace WindowsFormsApp1.server
MachineID = _machineID,
OPID = _opID,
AlarmID = OtherData.Plc_ErrorMsgs[0],
SendTime = DateTime.Now,
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),
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)
@@ -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);
@@ -362,17 +343,48 @@ namespace WindowsFormsApp1.server
var lineCode = Init.t_Line.linecode;
// 基础请求对象
var request = new
var request = new CheckOutObject()
{
EventID = "SN_Checkout",
Line = _line,
StationID = _stationID,
MachineID = _machineID,
OPID = _opID,
SN = OtherData.JS_SN,
SendTime = DateTime.Now
SNInfo = new List<SNInfoItem>()
{
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对象
switch (lineCode)
{
@@ -386,6 +398,11 @@ namespace WindowsFormsApp1.server
case "OP1010":// 传气密
case "OP1330":
var dcInfo = new DCInfoItem();
dcInfo.Item = PlcData.Read_MaterialCode2;
dcInfo.Value = "气密测试值";
dcInfo.Result = "PASS";
//request.SNInfo
break;
case "OP1050":// 胶水码
@@ -426,17 +443,6 @@ namespace WindowsFormsApp1.server
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;
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
@@ -452,7 +458,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID,
MachineID = _machineID,
OPID = _opID,
SendTime = DateTime.Now
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
};
return CallApi(url, request, "Test", PlcData, PLC_Status.Test);
@@ -467,7 +473,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID,
MachineID = _machineID,
OPID = _opID,
SendTime = DateTime.Now
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
};
return CallApi(url, request, "OEEDataCollection", PlcData, PLC_Status.OEEDataCollection);
@@ -482,7 +488,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID,
MachineID = _machineID,
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);
@@ -497,7 +503,7 @@ namespace WindowsFormsApp1.server
StationID = _stationID,
MachineID = _machineID,
OPID = _opID,
SendTime = DateTime.Now
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
};
return CallApi(url, request, "DispatchPara", PlcData, PLC_Status.DispatchPara);
@@ -599,5 +605,47 @@ namespace WindowsFormsApp1.server
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; }
}
}
}