修改已知问题
This commit is contained in:
@@ -50,13 +50,40 @@ namespace FATrace.WPLApp.Services
|
||||
private void LineSglModel_BoxSprayCodeReqHandle(object? sender, string e)
|
||||
{
|
||||
//首先复位PLC信号
|
||||
KeyencePlcMcNet!.Write("D1100", (Int16)0);
|
||||
var SetValueResult = KeyencePlcMcNet!.Write("D1100", (Int16)0);
|
||||
if (!SetValueResult.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("外箱喷码-复位PLC信号失败-执行两次写入0");
|
||||
KeyencePlcMcNet!.Write("D1100", (Int16)0);
|
||||
KeyencePlcMcNet!.Write("D1100", (Int16)0);
|
||||
}
|
||||
|
||||
//获取箱子喷码的数据+A
|
||||
var ReqData = FreeSql.Select<LineTempCode>()
|
||||
.OrderBy(a => a.Id)
|
||||
.OrderByDescending(a => a.Id)
|
||||
.Limit(1)
|
||||
.ToList();
|
||||
|
||||
//无数据再重试一次
|
||||
if (ReqData.Count <= 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
ReqData = FreeSql.Select<LineTempCode>()
|
||||
.OrderByDescending(a => a.Id)
|
||||
.Limit(1)
|
||||
.ToList();
|
||||
|
||||
LogService.Warn("外箱喷码-不存在请求的数据-重试一次");
|
||||
}
|
||||
|
||||
if (ReqData.Count > 0)
|
||||
{
|
||||
var CodeItem = ReqData.FirstOrDefault();
|
||||
@@ -65,12 +92,15 @@ namespace FATrace.WPLApp.Services
|
||||
var BoxSprayCodeRev = RevData(BoxSprayCodeSource);
|
||||
|
||||
BoxSprayCode = BoxSprayCodeSource;
|
||||
KeyencePlcMcNet.Write("D1150", BoxSprayCodeRev);
|
||||
|
||||
var Result = KeyencePlcMcNet.Write("D1150", BoxSprayCodeRev);
|
||||
if (!Result.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("外箱喷码-写入条码 D1150 PLC信号失败-执行两次写入条码");
|
||||
KeyencePlcMcNet.Write("D1150", BoxSprayCodeRev);
|
||||
}
|
||||
Console.WriteLine($"外箱喷码:{BoxSprayCodeSource}-发送OK");
|
||||
|
||||
//这里需要删除数据吗?还是等扫码后删除,会不会过来新的箱子
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -78,13 +108,22 @@ namespace FATrace.WPLApp.Services
|
||||
KeyencePlcMcNet.Write("D1102", (Int16)1);
|
||||
Console.WriteLine($"外箱喷码:不存在请求的数据");
|
||||
AddAlarm("外箱喷码", "不存在请求的数据");
|
||||
LogService.Warn("外箱喷码-不存在请求的数据");
|
||||
}
|
||||
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(1000).ConfigureAwait(false);
|
||||
KeyencePlcMcNet!.Write("D1102", (Int16)0);
|
||||
var Data = KeyencePlcMcNet!.Write("D1102", (Int16)0);
|
||||
if (!Data.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("外箱喷码-复位D1102PLC信号失败-执行两次写入0");
|
||||
KeyencePlcMcNet!.Write("D1102", (Int16)0);
|
||||
KeyencePlcMcNet!.Write("D1102", (Int16)0);
|
||||
}
|
||||
|
||||
//KeyencePlcMcNet.Write("D1150", new Int16[30]);
|
||||
});
|
||||
|
||||
@@ -99,7 +138,14 @@ namespace FATrace.WPLApp.Services
|
||||
private void LineSglModel_BoxScanCodeReqHandle(object? sender, string e)
|
||||
{
|
||||
//首先复位PLC信号
|
||||
KeyencePlcMcNet!.Write("D1200", (Int16)0);
|
||||
var SetValueResult = KeyencePlcMcNet!.Write("D1200", (Int16)0);
|
||||
if (!SetValueResult.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("外箱扫描码请求-复位PLC信号失败-执行两次写入0");
|
||||
KeyencePlcMcNet!.Write("D1200", (Int16)0);
|
||||
KeyencePlcMcNet!.Write("D1200", (Int16)0);
|
||||
}
|
||||
|
||||
string BoxScanCode = string.Empty;
|
||||
BoxScanCode = this.BoxScanCode;
|
||||
@@ -111,7 +157,14 @@ namespace FATrace.WPLApp.Services
|
||||
if (IsExist.Count() > 0)
|
||||
{
|
||||
//存在条码数据 OK,返回PLC结果
|
||||
KeyencePlcMcNet!.Write("D1210", (Int16)1);
|
||||
var DataResult = KeyencePlcMcNet!.Write("D1210", (Int16)1);
|
||||
if (!DataResult.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("外箱扫描码请求-置位D1210PLC信号失败-执行两次写入1");
|
||||
KeyencePlcMcNet!.Write("D1210", (Int16)1);
|
||||
KeyencePlcMcNet!.Write("D1210", (Int16)1);
|
||||
}
|
||||
|
||||
Console.WriteLine($"外箱扫描码:{BoxScanCode}-存在找到");
|
||||
// 加入队列
|
||||
@@ -181,8 +234,15 @@ namespace FATrace.WPLApp.Services
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(1000).ConfigureAwait(false);
|
||||
KeyencePlcMcNet!.Write("D1210", (Int16)0);
|
||||
await Task.Delay(1200).ConfigureAwait(false);
|
||||
var DataResult = KeyencePlcMcNet!.Write("D1210", (Int16)0);
|
||||
if (!DataResult.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("外箱扫描码请求-复位D1210PLC信号失败-执行两次写入0");
|
||||
KeyencePlcMcNet!.Write("D1210", (Int16)0);
|
||||
KeyencePlcMcNet!.Write("D1210", (Int16)0);
|
||||
}
|
||||
|
||||
KeyencePlcMcNet.Write("D1250", new Int16[30]);
|
||||
});
|
||||
@@ -198,7 +258,14 @@ namespace FATrace.WPLApp.Services
|
||||
private void LineSglModel_WeightScanCodeHandle(object? sender, string e)
|
||||
{
|
||||
//首先复位PLC信号
|
||||
KeyencePlcMcNet!.Write("D1000", (Int16)0);
|
||||
var SetValueResult = KeyencePlcMcNet!.Write("D1000", (Int16)0);
|
||||
if (!SetValueResult.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("称重扫描码-复位PLC信号失败-执行两次写入0");
|
||||
KeyencePlcMcNet!.Write("D1000", (Int16)0);
|
||||
KeyencePlcMcNet!.Write("D1000", (Int16)0);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(WeightScanCode))
|
||||
{
|
||||
@@ -206,24 +273,47 @@ namespace FATrace.WPLApp.Services
|
||||
if (IsExist.Count() > 0)
|
||||
{
|
||||
//存在条码数据 OK,返回PLC结果
|
||||
KeyencePlcMcNet!.Write("D1010", (Int16)1);
|
||||
var SetValueResult1 = KeyencePlcMcNet!.Write("D1010", (Int16)1);
|
||||
if (!SetValueResult1.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("称重扫描码-置位数据D1010 PLC信号失败-执行两次写入1");
|
||||
KeyencePlcMcNet!.Write("D1010", (Int16)1);
|
||||
KeyencePlcMcNet!.Write("D1010", (Int16)1);
|
||||
}
|
||||
|
||||
Console.WriteLine($"称重扫描码:{WeightScanCode}");
|
||||
//// 加入队列
|
||||
//EnqueueMessage(WeightScanCode);
|
||||
LogService.Info($"称重扫描码:{WeightScanCode}");
|
||||
|
||||
var TempData = FreeSql.Insert<LineTempCode>(new LineTempCode()
|
||||
try
|
||||
{
|
||||
Code = WeightScanCode
|
||||
}).ExecuteAffrows();
|
||||
var delAff = FreeSql.Delete<LineTempCode>()
|
||||
.Where(p => p.Id > 0)
|
||||
.ExecuteAffrows();
|
||||
|
||||
var insAff = FreeSql.Insert<LineTempCode>(new LineTempCode()
|
||||
{
|
||||
Code = WeightScanCode
|
||||
}).ExecuteAffrows();
|
||||
|
||||
if (insAff <= 0)
|
||||
{
|
||||
LogService.Warn($"称重扫描码:{WeightScanCode} 写入临时表 LineTempCode 失败(affrows={insAff}),delAff={delAff}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogService.Error($"称重扫描码:{WeightScanCode} 写入临时表 LineTempCode 异常: {ex}");
|
||||
}
|
||||
try
|
||||
{
|
||||
EventAggregator?.GetEvent<DashboardRefreshEvent>()?.Publish(true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
var Result = FreeSql.Update<RawProUse>()
|
||||
@@ -254,8 +344,16 @@ namespace FATrace.WPLApp.Services
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(1000).ConfigureAwait(false);
|
||||
KeyencePlcMcNet!.Write("D1010", (Int16)0);
|
||||
await Task.Delay(1200).ConfigureAwait(false);
|
||||
|
||||
//KeyencePlcMcNet!.Write("D1010", (Int16)0);
|
||||
var SetValueResult2 = KeyencePlcMcNet!.Write("D1010", (Int16)0);
|
||||
if (!SetValueResult2.IsSuccess)
|
||||
{
|
||||
//写入失败,重新写入
|
||||
LogService.Warn("称重扫描码-复位数据D1010 PLC信号失败-执行两次写入0");
|
||||
KeyencePlcMcNet!.Write("D1010", (Int16)0);
|
||||
}
|
||||
|
||||
KeyencePlcMcNet.Write("D1050", new Int16[30]);
|
||||
});
|
||||
@@ -323,7 +421,6 @@ namespace FATrace.WPLApp.Services
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 扫描控制
|
||||
private CancellationTokenSource? _plcScanCts;
|
||||
private Task? _plcScanTask;
|
||||
@@ -331,10 +428,6 @@ namespace FATrace.WPLApp.Services
|
||||
public bool IsScanning => _isScanning;
|
||||
private bool _disposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// 简单字符串队列(FIFO)
|
||||
/// </summary>
|
||||
private readonly ConcurrentQueue<string> _messageQueue = new();
|
||||
|
||||
/// <summary>
|
||||
/// 产线信号模型
|
||||
@@ -366,6 +459,7 @@ namespace FATrace.WPLApp.Services
|
||||
if (connect.IsSuccess)//初始连接状态的显示判断
|
||||
{
|
||||
Console.WriteLine($"PLC-连接 OK");
|
||||
LogService.Error($"PLC-连接 OK");
|
||||
PlcConnected = true;
|
||||
try { SysRunService.PlcLinkState = true; SysRunService.SysRunState.ComState = 1; SysRunService.SysRunState.ComMsg = "正常"; } catch { }
|
||||
}
|
||||
@@ -400,7 +494,31 @@ namespace FATrace.WPLApp.Services
|
||||
var token = _plcScanCts.Token;
|
||||
_isScanning = true;
|
||||
|
||||
try { SysRunService.PlcScanState = 1; } catch { }
|
||||
|
||||
_plcScanTask = Task.Run(() => PlcScanLoopAsync(period, token), token);
|
||||
_plcScanTask.ContinueWith(t =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (t.IsFaulted)
|
||||
{
|
||||
try { SysRunService.PlcScanState = 2; } catch { }
|
||||
try { LogService.Error($"PLC扫描任务异常退出: {t.Exception}"); } catch { }
|
||||
}
|
||||
else if (t.IsCanceled)
|
||||
{
|
||||
try { SysRunService.PlcScanState = 0; } catch { }
|
||||
}
|
||||
else
|
||||
{
|
||||
try { SysRunService.PlcScanState = 0; } catch { }
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}, TaskScheduler.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -425,6 +543,8 @@ namespace FATrace.WPLApp.Services
|
||||
_plcScanTask = null;
|
||||
_plcScanCts?.Dispose();
|
||||
_plcScanCts = null;
|
||||
|
||||
try { SysRunService.PlcScanState = 0; } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -513,13 +633,12 @@ namespace FATrace.WPLApp.Services
|
||||
}
|
||||
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogService.Error($"PLC 扫描异常: {ex}");
|
||||
LogService.Error($"PLC 扫描异常 Exception: {ex.ToString()}");
|
||||
|
||||
// 这里属于循环内部异常:循环仍会继续;
|
||||
// 但若后续由于未捕获异常导致扫描任务退出,StartPlcScan 的 ContinueWith 也会做最终状态处理。
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -527,14 +646,23 @@ namespace FATrace.WPLApp.Services
|
||||
{
|
||||
await Task.Delay(period, token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
// 正常停止扫描时会触发取消,这里不应记录为错误。
|
||||
// 不允许从 finally 中 break/return,
|
||||
// 这里吞掉异常,循环将在下一次 while 判断时依据 token 自动退出
|
||||
}
|
||||
}
|
||||
}
|
||||
_isScanning = false;
|
||||
|
||||
// 退出循环:如果是正常取消,则 StopPlcScanAsync 会把状态置为停止;
|
||||
// 若是外部未走 StopPlcScanAsync,而 token 已取消,也归为停止。
|
||||
try
|
||||
{
|
||||
if (token.IsCancellationRequested) SysRunService.PlcScanState = 0;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user