From a10a8c6413bf523eb1546bee94dd615611a71b04 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Wed, 20 May 2026 19:59:15 +0800 Subject: [PATCH] =?UTF-8?q?feat=20-=20=E5=86=99=E5=85=A5=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=20=E6=97=A0=E9=99=90=E5=86=99=E5=85=A5?= =?UTF-8?q?=EF=BC=8C=E8=B6=85=E6=97=B6=E5=88=A4=E5=A4=B1=E8=B4=A5=EF=BC=8C?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1=E5=86=99=E5=85=A5=E5=89=8D=E9=83=BD=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Config/DeviceInfoConfig.cs | 7 +- SlnMesnac.Rfid/Factory/RflyFactory.cs | 423 +++++++++--------- .../IndexPage/ProductionLineViewModel.cs | 152 +++---- SlnMesnac.WPF/appsettings.json | 8 +- 4 files changed, 297 insertions(+), 293 deletions(-) diff --git a/SlnMesnac.Config/DeviceInfoConfig.cs b/SlnMesnac.Config/DeviceInfoConfig.cs index b54ca6e..229d038 100644 --- a/SlnMesnac.Config/DeviceInfoConfig.cs +++ b/SlnMesnac.Config/DeviceInfoConfig.cs @@ -76,7 +76,12 @@ namespace SlnMesnac.Config /// /// 二次验证超时时间(毫秒) /// - public int? VerifyTimeoutMilliseconds { get; set; } + public int? VerifyTimeoutMilliseconds { get; set; } + + /// + /// 写入超时时间(毫秒) + /// + public int? WriteTimeoutMilliseconds { get; set; } } } \ No newline at end of file diff --git a/SlnMesnac.Rfid/Factory/RflyFactory.cs b/SlnMesnac.Rfid/Factory/RflyFactory.cs index f58095d..53ed73b 100644 --- a/SlnMesnac.Rfid/Factory/RflyFactory.cs +++ b/SlnMesnac.Rfid/Factory/RflyFactory.cs @@ -98,10 +98,10 @@ namespace SlnMesnac.Rfid.Factory { //lock (GloalVar.TCPLocker) //{ - m_deviceID = deviceid; - m_strIP = ip; - _tcpClient.SetupAsync(new TouchSocketConfig().SetRemoteIPHost($"{ip}:{port}")); - _tcpClient.ConnectAsync(1500).GetAwaiter().GetResult(); + m_deviceID = deviceid; + m_strIP = ip; + _tcpClient.SetupAsync(new TouchSocketConfig().SetRemoteIPHost($"{ip}:{port}")); + _tcpClient.ConnectAsync(1500).GetAwaiter().GetResult(); //} _tcpClient.Received = (client, e) => { @@ -120,7 +120,7 @@ namespace SlnMesnac.Rfid.Factory Array.Copy(e.Memory.Span.ToArray(), 0, receivedBuffer, 0, e.Memory.Span.Length); if (receivedBuffer.Length > 3) { - + //心跳 if (e.Memory.Span.ToArray()[3] == 0xBF) { @@ -193,50 +193,50 @@ namespace SlnMesnac.Rfid.Factory { //lock (GloalVar.TCPLocker) //{ - Log.Information($"设置心跳:【{second}】秒"); - byte[] data = new byte[3]; - data[0] = 0x00; - data[1] = second; - data[2] = 0x01; + Log.Information($"设置心跳:【{second}】秒"); + byte[] data = new byte[3]; + data[0] = 0x00; + data[1] = second; + data[2] = 0x01; - BaseSendDataEntity entity = new BaseSendDataEntity() + BaseSendDataEntity entity = new BaseSendDataEntity() + { + Code = 0xBF, + Data = data + }; + + byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); + + var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() + { + FilterFunc = response => { - Code = 0xBF, - Data = data - }; - - byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); - - var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() - { - FilterFunc = response => - { - // 检查响应数据是否符合预期 - if (response.Memory.Length > 0) - { - // 可以根据实际情况添加更多的检查逻辑 - return true; - } - return false; - } - }); - - using (var responsedData = waitClient.SendThenResponseAsync(result, 2000).GetAwaiter().GetResult()) - { - var reciveBuffer = responsedData.Memory.ToArray(); - - byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length); - Log.Information($"{m_deviceID}接收读取指令{_stringChange.bytesToHexStr(resultBuffer, resultBuffer.Length)}"); - - if (resultBuffer[3] == 0xBF || resultBuffer[4] == 0x00) + // 检查响应数据是否符合预期 + if (response.Memory.Length > 0) { + // 可以根据实际情况添加更多的检查逻辑 return true; } - else - { - return false; - } + return false; } + }); + + using (var responsedData = waitClient.SendThenResponseAsync(result, 2000).GetAwaiter().GetResult()) + { + var reciveBuffer = responsedData.Memory.ToArray(); + + byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length); + Log.Information($"{m_deviceID}心跳设置返回{_stringChange.bytesToHexStr(resultBuffer, resultBuffer.Length)}"); + + if (resultBuffer[3] == 0xBF || resultBuffer[4] == 0x00) + { + return true; + } + else + { + return false; + } + } //} } @@ -556,47 +556,48 @@ namespace SlnMesnac.Rfid.Factory { //lock (GloalVar.TCPLocker) //{ - #region 指令封装 - MessagePack pMessagePack = new MessagePack(); - pMessagePack.m_pData = new byte[8]; - pMessagePack.m_pData[0] = 0xAA; - pMessagePack.m_pData[1] = 0x55; - pMessagePack.m_pData[2] = 0x02; - pMessagePack.m_pData[3] = 0x12; - pMessagePack.m_pData[4] = 0x00; - pMessagePack.m_pData[5] = 0x00; - Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); - pMessagePack.m_pData[6] = _stringChange.CalculateVerify(bCRC, bCRC.Length); - pMessagePack.m_pData[7] = 0x0D; - #endregion 指令封装 + #region 指令封装 + MessagePack pMessagePack = new MessagePack(); + pMessagePack.m_pData = new byte[8]; + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x02; + pMessagePack.m_pData[3] = 0x12; + pMessagePack.m_pData[4] = 0x00; + pMessagePack.m_pData[5] = 0x00; + Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); + pMessagePack.m_pData[6] = _stringChange.CalculateVerify(bCRC, bCRC.Length); + pMessagePack.m_pData[7] = 0x0D; + #endregion 指令封装 - _tcpClient.SendAsync(pMessagePack.m_pData).GetAwaiter().GetResult(); - //var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() - //{ - // FilterFunc = response => - // { - // // 检查响应数据是否符合预期 - // if (response.Memory.Length > 0) - // { - // // 可以根据实际情况添加更多的检查逻辑 - // return true; - // } - // return false; - // } - //}); - //Log.Information($"{m_deviceID}发送停止连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); - //using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) - //{ - // var reciveBuffer = responsedData.Memory.ToArray(); - // Log.Information($"{m_deviceID}接收停止连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); - // if (reciveBuffer[3] == 0x12) - // { - // return true; - // } - //} - //return false; - return true; + _tcpClient.SendAsync(pMessagePack.m_pData).GetAwaiter().GetResult(); + Log.Information($"{deviceid} 停止盘点"); + //var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() + //{ + // FilterFunc = response => + // { + // // 检查响应数据是否符合预期 + // if (response.Memory.Length > 0) + // { + // // 可以根据实际情况添加更多的检查逻辑 + // return true; + // } + // return false; + // } + //}); + //Log.Information($"{m_deviceID}发送停止连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + //using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) + //{ + // var reciveBuffer = responsedData.Memory.ToArray(); + // Log.Information($"{m_deviceID}接收停止连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + // if (reciveBuffer[3] == 0x12) + // { + // return true; + // } + //} + //return false; + return true; //} } catch (Exception e) @@ -619,46 +620,46 @@ namespace SlnMesnac.Rfid.Factory //lock (GloalVar.TCPLocker) //{ - #region 指令封装 - MessagePack pMessagePack = new MessagePack(); - pMessagePack.m_pData = new byte[8]; - pMessagePack.m_pData[0] = 0xAA; - pMessagePack.m_pData[1] = 0x55; - pMessagePack.m_pData[2] = 0x02; - pMessagePack.m_pData[3] = 0x11; - pMessagePack.m_pData[4] = 0x00; - pMessagePack.m_pData[5] = 0x00; - Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); - pMessagePack.m_pData[6] = _stringChange.CalculateVerify(bCRC, bCRC.Length); - pMessagePack.m_pData[7] = 0x0D; - #endregion 指令封装 + #region 指令封装 + MessagePack pMessagePack = new MessagePack(); + pMessagePack.m_pData = new byte[8]; + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x02; + pMessagePack.m_pData[3] = 0x11; + pMessagePack.m_pData[4] = 0x00; + pMessagePack.m_pData[5] = 0x00; + Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); + pMessagePack.m_pData[6] = _stringChange.CalculateVerify(bCRC, bCRC.Length); + pMessagePack.m_pData[7] = 0x0D; + #endregion 指令封装 - _tcpClient.SendAsync(pMessagePack.m_pData).GetAwaiter().GetResult(); - - //var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() - //{ - // FilterFunc = response => - // { - // // 检查响应数据是否符合预期 - // if (response.Memory.Length > 0) - // { - // // 可以根据实际情况添加更多的检查逻辑 - // return true; - // } - // return false; - // } - //}); - //Log.Information($"{m_deviceID}发送连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); - //using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) - //{ - // var reciveBuffer = responsedData.Memory.ToArray(); - // Log.Information($"{m_deviceID}接收连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); - // if (reciveBuffer[3] == 0x11) - // { - // return true; - // } - //} - return true; + _tcpClient.SendAsync(pMessagePack.m_pData).GetAwaiter().GetResult(); + Log.Information($"{deviceid} 开始盘点"); + //var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() + //{ + // FilterFunc = response => + // { + // // 检查响应数据是否符合预期 + // if (response.Memory.Length > 0) + // { + // // 可以根据实际情况添加更多的检查逻辑 + // return true; + // } + // return false; + // } + //}); + //Log.Information($"{m_deviceID}发送连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + //using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) + //{ + // var reciveBuffer = responsedData.Memory.ToArray(); + // Log.Information($"{m_deviceID}接收连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + // if (reciveBuffer[3] == 0x11) + // { + // return true; + // } + //} + return true; //} } catch (Exception e) @@ -713,102 +714,102 @@ namespace SlnMesnac.Rfid.Factory { //lock (GloalVar.TCPLocker) //{ - #region 指令封装 - //自动根据写入数据封装 - Base03HENtity rawData = new Base03HENtity() + #region 指令封装 + //自动根据写入数据封装 + Base03HENtity rawData = new Base03HENtity() + { + TimeOut = _stringChange.IntToBytes(2, 5000), + AccessPassword = _stringChange.IntToBytes(4, 0), + SelectBank = 01, + SelectAddress = _stringChange.IntToBytes(4, 32), + SelectLength = (byte)(pSelectData.Count() * 8), + SelectData = pSelectData, + WriteBank = 01, + WriteAddress = _stringChange.IntToBytes(4, 1), + WordCount = (byte)((finalWriteData.Count() / 2)), + WriteData = finalWriteData + }; + + int selectLength = rawData.SelectLength / 8; + int wordCount = rawData.WordCount * 2; + byte[] bytes = new byte[2 + 4 + 1 + 4 + 1 + selectLength + 1 + 4 + 1 + wordCount]; + + int index = 0; + + Array.Copy(rawData.TimeOut, 0, bytes, index, 2); + index += 2; + + Array.Copy(rawData.AccessPassword, 0, bytes, index, 4); + index += 4; + + bytes[index] = rawData.SelectBank; + index++; + + Array.Copy(rawData.SelectAddress, 0, bytes, index, 4); + index += 4; + + bytes[index] = rawData.SelectLength; + index++; + + Array.Copy(rawData.SelectData, 0, bytes, index, selectLength); + index += selectLength; + + bytes[index] = rawData.WriteBank; + index++; + + Array.Copy(rawData.WriteAddress, 0, bytes, index, 4); + index += 4; + + bytes[index] = rawData.WordCount; + index++; + + Array.Copy(rawData.WriteData, 0, bytes, index, wordCount); + index += wordCount; + + BaseSendDataEntity entity = new BaseSendDataEntity() + { + Code = 0x03, + Data = bytes + }; + byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); + #endregion 指令封装 + + var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() + { + FilterFunc = response => { - TimeOut = _stringChange.IntToBytes(2, 5000), - AccessPassword = _stringChange.IntToBytes(4, 0), - SelectBank = 01, - SelectAddress = _stringChange.IntToBytes(4, 32), - SelectLength = (byte)(pSelectData.Count() * 8), - SelectData = pSelectData, - WriteBank = 01, - WriteAddress = _stringChange.IntToBytes(4, 1), - WordCount = (byte)((finalWriteData.Count() / 2)), - WriteData = finalWriteData - }; - - int selectLength = rawData.SelectLength / 8; - int wordCount = rawData.WordCount * 2; - byte[] bytes = new byte[2 + 4 + 1 + 4 + 1 + selectLength + 1 + 4 + 1 + wordCount]; - - int index = 0; - - Array.Copy(rawData.TimeOut, 0, bytes, index, 2); - index += 2; - - Array.Copy(rawData.AccessPassword, 0, bytes, index, 4); - index += 4; - - bytes[index] = rawData.SelectBank; - index++; - - Array.Copy(rawData.SelectAddress, 0, bytes, index, 4); - index += 4; - - bytes[index] = rawData.SelectLength; - index++; - - Array.Copy(rawData.SelectData, 0, bytes, index, selectLength); - index += selectLength; - - bytes[index] = rawData.WriteBank; - index++; - - Array.Copy(rawData.WriteAddress, 0, bytes, index, 4); - index += 4; - - bytes[index] = rawData.WordCount; - index++; - - Array.Copy(rawData.WriteData, 0, bytes, index, wordCount); - index += wordCount; - - BaseSendDataEntity entity = new BaseSendDataEntity() - { - Code = 0x03, - Data = bytes - }; - byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); - #endregion 指令封装 - - var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions() - { - FilterFunc = response => + // 检查响应数据是否符合预期 + if (response.Memory.Length > 0) { - // 检查响应数据是否符合预期 - if (response.Memory.Length > 0) - { - // 可以根据实际情况添加更多的检查逻辑 - return true; - } - return false; - } - }); - Log.Information($"{m_deviceID}发送写入指令{_stringChange.bytesToHexStr(result, result.Length)}"); - using (var responsedData = waitClient.SendThenResponseAsync(result, 6000).GetAwaiter().GetResult()) - { - var reciveBuffer = responsedData.Memory.ToArray(); - Log.Information($"{m_deviceID}接收写入指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); - //只要收到消息,就算心跳 - if (GloalVar.HeartBeatRecoard.TryGetValue(deviceid, out var value)) - { - GloalVar.HeartBeatRecoard[deviceid] = DateTime.Now; - } - else - { - GloalVar.HeartBeatRecoard.Add(deviceid, DateTime.Now); - } - if (reciveBuffer[3] == 0x03 && reciveBuffer[4] == 0x00) - { - Log.Information($"写入成功,写入数值{pWriteData}"); + // 可以根据实际情况添加更多的检查逻辑 return true; } + return false; } - return false; + }); + Log.Information($"{m_deviceID}发送写入指令{_stringChange.bytesToHexStr(result, result.Length)}"); + using (var responsedData = waitClient.SendThenResponseAsync(result, 6000).GetAwaiter().GetResult()) + { + var reciveBuffer = responsedData.Memory.ToArray(); + Log.Information($"{m_deviceID}接收写入指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + //只要收到消息,就算心跳 + if (GloalVar.HeartBeatRecoard.TryGetValue(deviceid, out var value)) + { + GloalVar.HeartBeatRecoard[deviceid] = DateTime.Now; + } + else + { + GloalVar.HeartBeatRecoard.Add(deviceid, DateTime.Now); + } + if (reciveBuffer[3] == 0x03 && reciveBuffer[4] == 0x00) + { + Log.Information($"写入成功,写入数值{pWriteData}"); + return true; + } + } + return false; //} - + } catch (Exception e) { diff --git a/SlnMesnac.WPF/ViewModel/IndexPage/ProductionLineViewModel.cs b/SlnMesnac.WPF/ViewModel/IndexPage/ProductionLineViewModel.cs index 601003a..3264ae0 100644 --- a/SlnMesnac.WPF/ViewModel/IndexPage/ProductionLineViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/IndexPage/ProductionLineViewModel.cs @@ -31,6 +31,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using System.Windows.Markup; using System.Windows.Threading; using System.Xml.Serialization; using TouchSocket.Core; @@ -60,9 +61,11 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage private RealReadDataImpl databaseService = RealReadDataImpl.Instance; private System.Threading.Timer ReReadTimer; private bool IsVerify = false; + private int WriteTime = 0; private string LastWrite; private string LastRFIDEPC; private CancellationTokenSource? _verifyCts; + private CancellationTokenSource? _writeCts; #endregion @@ -149,7 +152,6 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage public ProductionLineViewModel() { - GetOrderInfoCommand = new RelayCommand(t => RefreshData(t)); ChangeTypeView = App.ServiceProvider.GetService(); // 构造函数里注册 WeakReferenceMessenger.Default.Register(this, RefreshOrderNo); @@ -196,8 +198,10 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage { try { + _writeCts?.Cancel(); var rfidInfo = rfidList.FirstOrDefault(x => x.deviceid == iCombineId); var deviceInfo = Deviceinfo.FirstOrDefault(x => x.Deviceid == iCombineId); + var deviceConfig = appConfig.deviceInfoConfig.FirstOrDefault(x => x.Deviceid == iCombineId); //初次写入 if (!IsVerify) { @@ -229,6 +233,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage return; } deviceInfo.ReadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + //拼接订单号写入标签 string WriteData = deviceInfo.OrderNo + deviceInfo.LineNo @@ -237,59 +242,77 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage + deviceInfo.SerialNo; CurrentState = "写入中"; LastRFIDEPC = tagInfos[0].EPCstring; - //写入 + + //写入前暂停心跳和连续盘点 StopInventoryTimer(iCombineId); await rfidInfo.Set_HeartBeat(0); Task.Delay(100).Wait(); - var deviceConfig = appConfig.deviceInfoConfig.FirstOrDefault(x => x.Deviceid == iCombineId); - for (int i = 0; i < deviceConfig.WriteCount; i++) + + //处理写入字符串,并写入 + var originBytes = tagInfos[0].EPC.Where(b => b != 0x00).ToArray(); + bool writeflag = rfidInfo.Set_Write(originBytes, WriteData).GetAwaiter().GetResult(); + WriteTime++; + + if (writeflag) //写入成功 { - bool writeflag = rfidInfo.Set_Write(tagInfos[0].EPC, WriteData).GetAwaiter().GetResult(); - if (writeflag) + LastWrite = WriteData; + IsVerify = true; + WriteTime = 0; + + //开始二次验证 + rfidInfo.Set_BeginIdentify().GetAwaiter().GetResult(); + + // 启动验证超时检测 + _verifyCts?.Cancel(); + _verifyCts = new CancellationTokenSource(); + var token = _verifyCts.Token; + var capturedTagInfos = tagInfos; + var timeoutMs = deviceConfig.VerifyTimeoutMilliseconds ?? 20000; + _ = Task.Run(async () => { - LastWrite = WriteData; - IsVerify = true; - - rfidInfo.Set_BeginIdentify().GetAwaiter().GetResult(); - - // 启动验证超时检测 - _verifyCts?.Cancel(); - _verifyCts = new CancellationTokenSource(); - var token = _verifyCts.Token; - var capturedTagInfos = tagInfos; - var timeoutMs = deviceConfig.VerifyTimeoutMilliseconds ?? 20000; - _ = Task.Run(async () => + try { - try - { - await Task.Delay(timeoutMs, token); - if (IsVerify) - { - Log.Error($"二次验证超时未读到标签,标签已离开,记为写入失败"); - DataAdd(iCombineId, capturedTagInfos, false); - } - } - catch (TaskCanceledException) { } - }, token); + await Task.Delay(timeoutMs, token); + Log.Error($"二次验证超时未读到标签,标签已离开,记为写入失败"); + await DataAdd(iCombineId, capturedTagInfos, false); + } + catch (TaskCanceledException) { } + }, token); - StartInventoryTimer(iCombineId, deviceConfig.InventoryIntervalSeconds.Value); - await rfidInfo.Set_HeartBeat(5); - return; - } - else - { - Log.Error($"第{i + 1}次写入失败,重试中..."); - Task.Delay(100).Wait(); - continue; - } + //开启定时盘点和心跳 + StartInventoryTimer(iCombineId, deviceConfig.InventoryIntervalSeconds.Value); + await rfidInfo.Set_HeartBeat(5); + return; } - Log.Error($"写入失败,[{tagInfos[0].EPCstring}] 记为失败写入"); - //插入失败记录 - DataAdd(iCombineId, tagInfos, false); - StartInventoryTimer(iCombineId, deviceConfig.InventoryIntervalSeconds.Value); - await rfidInfo.Set_HeartBeat(5); - return; + else //写入失败 + { + Log.Error($"第{WriteTime + 1}次写入失败,重试中..."); + //重试前再读取一次标签 + rfidInfo.Set_BeginIdentify().GetAwaiter().GetResult(); + //设置下次读取超时 + _writeCts?.Cancel(); + _writeCts = new CancellationTokenSource(); + var token = _writeCts.Token; + var capturedTagInfos = tagInfos; + var timeoutMs = deviceConfig.WriteTimeoutMilliseconds ?? 10000; + _ = Task.Run(async () => + { + try + { + await Task.Delay(timeoutMs, token); + Log.Error($"写入前读取超时,[{tagInfos[0].EPCstring}] 记为写入失败"); + await DataAdd(iCombineId, capturedTagInfos, false); + WriteTime = 0; + } + catch (TaskCanceledException) { } + }, token); + + StartInventoryTimer(iCombineId, deviceConfig.InventoryIntervalSeconds.Value); + await rfidInfo.Set_HeartBeat(5); + Task.Delay(100).Wait(); + } + return; } else { @@ -313,7 +336,6 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage _verifyCts = new CancellationTokenSource(); var token = _verifyCts.Token; var capturedTagInfos = tagInfos; - var deviceConfig = appConfig.deviceInfoConfig.FirstOrDefault(x => x.Deviceid == iCombineId); var timeoutMs = deviceConfig?.VerifyTimeoutMilliseconds ?? 20000; _ = Task.Run(async () => { @@ -321,7 +343,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage { await Task.Delay(timeoutMs, token); Log.Error($"二次验证超时写入标签超时,标签已离开,记为写入失败"); - DataAdd(iCombineId, capturedTagInfos, false); + await DataAdd(iCombineId, capturedTagInfos, false); } catch (TaskCanceledException) { } }, token); @@ -338,7 +360,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage _verifyCts?.Cancel(); Log.Information($"验证成功"); //插入成功记录 - DataAdd(iCombineId, tagInfos, true); + await DataAdd(iCombineId, tagInfos, true); return; } } @@ -356,7 +378,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage /// /// /// - private void DataAdd(string iCombineId, List tagInfos, bool isSuccess) + private async Task DataAdd(string iCombineId, List tagInfos, bool isSuccess) { var deviceInfo = Deviceinfo.FirstOrDefault(x => x.Deviceid == iCombineId); var rfidInfo = rfidList.FirstOrDefault(x => x.deviceid == iCombineId); @@ -393,8 +415,10 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage LastRFIDEPC = ""; LastWrite = ""; - rfidInfo.Set_BeginIdentify().GetAwaiter().GetResult(); + await rfidInfo.Set_BeginIdentify(); + await Task.Delay(100); StartInventoryTimer(iCombineId, deviceConfig.InventoryIntervalSeconds.Value); + await rfidInfo.Set_HeartBeat(5); CurrentState = "盘点中"; return; } @@ -403,34 +427,6 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage #region 数据流相关 - /// - /// 测试按钮 - /// - /// - - private void RefreshData(object? t) - { - Random random = new Random(); - string ss = random.Next(10, 99).ToString(); - string EPCstring = "JSSD" + ss; - List tagInfos = new List(); - TagInfo tagInfo = new TagInfo() - { - EPCstring = "JSSD" + ss, - EPC = Encoding.ASCII.GetBytes(EPCstring), - }; - tagInfos.Add(tagInfo); - - Deviceinfo.FirstOrDefault(x => x.Deviceid == "10001").RfidEPC = _StringChange.bytesToHexStr(tagInfos[0].EPC, tagInfos[0].EPC.Length); - Deviceinfo.FirstOrDefault(x => x.Deviceid == "10001").RfidASCII = EPCstring; - Deviceinfo.FirstOrDefault(x => x.Deviceid == "10001").WriteStatus = "成功"; - Deviceinfo.FirstOrDefault(x => x.Deviceid == "10001").ReadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - Deviceinfo.FirstOrDefault(x => x.Deviceid == "10001").WriteTime = DateTime.Now.AddMinutes(1).ToString("yyyy-MM-dd HH:mm:ss"); - RecvIdentifyData_Instance("10001", tagInfos); - } - - public RelayCommand GetOrderInfoCommand { get; set; } - /// /// 开始订单初次流程 /// diff --git a/SlnMesnac.WPF/appsettings.json b/SlnMesnac.WPF/appsettings.json index efd94f3..55b5c4f 100644 --- a/SlnMesnac.WPF/appsettings.json +++ b/SlnMesnac.WPF/appsettings.json @@ -9,7 +9,7 @@ "AllowedHosts": "*", "AppConfig": { //是否显示控制台 - "ShowConsole": false, + "ShowConsole": true, "logPath": "D:\\net6.0-windows\\log", "InstanceDataPath": "D:\\net6.0-windows\\log", "SqlConfig": [ @@ -95,11 +95,13 @@ //初次读到数据后写入数据延迟设置,单位毫秒 "WriteDelaySet": 1000, //写入失败重试次数(每次写入设置为五秒超时时间) - "WriteCount": 30, + //"WriteCount": 30, //定时重置盘点间隔时间,单位毫秒 "InventoryIntervalSeconds": 5000, //二次验证超时时间,单位毫秒 - "VerifyTimeoutMilliseconds": 20000 + "VerifyTimeoutMilliseconds": 10000, + //写入超时时间,单位毫秒 + "WriteTimeoutMilliseconds": 10000, }, { "DeviceId": "10002",