private void DataReceive() { this._IsReceiving = true; try { if (!this._isReading) { this._IsReceiving = false; return; } ReaderGeneral.ConsoleWrite("开始接收数据:" + DateTime.Now.ToString()); byte[] arrBuff = new byte[10240]; int iRecLen = this._SendClient.Receive(arrBuff); ReaderGeneral.ConsoleWrite("接收到数据长度为:" + iRecLen); List <byte> listBuff = arrBuff.ToList(); listBuff.RemoveRange(iRecLen, listBuff.Count - iRecLen); ReaderGeneral.ConsoleWrite("数据池数据填充完毕:" + DateTime.Now.ToString()); //转换正常格式数据列 //string strChange = ReaderGeneral.ChangeHexArrayToString(arrBuff); List <string> listSource = ReaderGeneral.HandleSourceDataToList(listBuff.ToArray()); ReaderGeneral.ConsoleWrite("接收数据量:" + listSource.Count + "条。 " + DateTime.Now.ToString()); //符合正常监控数据的长度。 List <string> listRecord = listSource.Where(x => x.Length == 62).ToList(); if (listRecord != null && listRecord.Count > 0) { ReaderGeneral.ConsoleWrite("正常数据数量:" + listRecord.Count); } //符合确认返回数据的长度。 List <string> listConfirm = listSource.Where(x => x.Length == 14).ToList(); if (listConfirm != null && listConfirm.Count > 0) { ReaderGeneral.ConsoleWrite("确认数据:" + listConfirm.Count); } //符合时间数据的长度。 List <string> listTime = listSource.Where(x => x.Length == 28).ToList(); if (listTime != null && listTime.Count > 0) { ReaderGeneral.ConsoleWrite("时间值数据:" + listTime.Count); } if (this._currentDataType == EnumRecDataMode.CollectRecordMode) { #region 处理正常监控数据 Dictionary <string, TagInfoResultEventArgs> dicReceive = new Dictionary <string, TagInfoResultEventArgs>(); foreach (string strTag in listRecord) { TagInformationInfo tagInfo = new TagInformationInfo(); try { tagInfo = tagInfo.GetTagInformationInfoIns(strTag); } catch (Exception ex) { ReaderGeneral.ConsoleWrite(ex.Message); if (!string.IsNullOrEmpty(ex.Message)) { string[] arrString = ex.Message.Split('-'); if (arrString.Length > 1) { TagInfoResultEventArgs args = new TagInfoResultEventArgs(); args.TagInformationList = new List <TagInformationInfo>(); TagInformationInfo tagThrow = new TagInformationInfo(); tagThrow.MachineID = arrString[1]; args.TagInformationList.Add(tagThrow); OnTagResult(args); } } tagInfo = null; } if (tagInfo != null) { if (dicReceive.Keys.Contains(tagInfo.MachineID)) { if (dicReceive[tagInfo.MachineID] == null) { dicReceive[tagInfo.MachineID] = new TagInfoResultEventArgs(); dicReceive[tagInfo.MachineID].TagInformationList = new List <TagInformationInfo>(); } dicReceive[tagInfo.MachineID].TagInformationList.Add(tagInfo); } else { TagInfoResultEventArgs args = new TagInfoResultEventArgs(); args.TagInformationList = new List <TagInformationInfo>(); args.TagInformationList.Add(tagInfo); dicReceive.Add(tagInfo.MachineID, args); } if (tagInfo.States == "01") { dicReceive[tagInfo.MachineID].ExamineNum = tagInfo.RecordIndex; } } } if (dicReceive != null && dicReceive.Count > 0) { foreach (KeyValuePair <string, TagInfoResultEventArgs> kvPair in dicReceive) { ReaderGeneral.ConsoleWrite("抛出数据量:" + kvPair.Value.TagInformationList.Count); OnTagResult(kvPair.Value); } } #endregion #region 处理确认数据 foreach (string confirmItem in listConfirm) { try { int iMacID = Convert.ToInt32(confirmItem.Substring(2, 2), 16); string strFinish = GetFinishedCollectionInstruction(iMacID); string strUpload = GetUploadedCollectionInstruction(iMacID); if (confirmItem == strFinish || confirmItem == strUpload) { TagInfoResultEventArgs arg = new TagInfoResultEventArgs(); arg.TagInformationList = new List <TagInformationInfo>(); TagInformationInfo tagInfo = new TagInformationInfo(); tagInfo.MachineID = iMacID.ToString(); arg.TagInformationList.Add(tagInfo); ReaderGeneral.ConsoleWrite("no more data .................................................." + iMacID.ToString()); this.OnTagResult(arg); } } catch (Exception ex) { ReaderGeneral.ConsoleWrite("Error: " + ex.Message); } } #endregion } else if (this._currentDataType == EnumRecDataMode.AdjustTimeMode) { #region 处理时间检查 if (listTime != null && listTime.Count > 0) { foreach (string timeItem in listTime) { int iMacIDChkTime = 0; DateTime?dtGet = DataHandling_FormatMacTimeVal(timeItem, ref iMacIDChkTime); if (iMacIDChkTime != 0) { if (!this.DicReaderDetails.Keys.Contains(iMacIDChkTime)) { return; } ReaderExDetailInfo readerChk = this.DicReaderDetails[iMacIDChkTime]; if (dtGet != null) { // 获取到有效的机器时间后,将此时间值赋予获取成功标识。 readerChk.Ex_dtLastTimeChk = dtGet.Value; readerChk.Ex_iCounterTimeChk = 0; readerChk.Ex_lIsSendTimeChkWarning = false; } else { readerChk.Ex_iCounterTimeChk++; } readerChk.Ex_dtLastRunTime = DateTime.Now; } } } #endregion } ReaderGeneral.ConsoleWrite("数据接收完毕。" + DateTime.Now.ToString()); } catch (Exception ex) { ReaderGeneral.ConsoleWrite(ex.Message); } this._IsReceiving = false; }
/// <summary> /// 格式化处理批量数据 /// </summary> /// <param name="strSourceData">需要处理的返回指令数据</param> /// <param name="strRecordIndex">返回需要被确认的流水号</param> /// <returns></returns> private List <TagInformationInfo> DataHandling_FormatBatchTagData(string strSourceData, out string strRecordIndex) { try { List <TagInformationInfo> listTagInfos = new List <TagInformationInfo>(); strSourceData = strSourceData.Trim(); string[] arrStrData = new string[this._iRecordListMaxLen]; strRecordIndex = string.Empty; if (strSourceData.Length >= 14) { //处理正常数据 List <string> listSourceData = ReaderGeneral.HandleSourceDataToList(strSourceData, this._iRecordMaxLen); List <ReaderExceptionEventArgs> listExArgs = new List <ReaderExceptionEventArgs>(); if (listSourceData != null && listSourceData.Count > 0) { #region 处理正常数据返回 for (int i = 0; i < listSourceData.Count; i++) { try { TagInformationInfo tagFormat = new TagInformationInfo(); tagFormat = tagFormat.GetTagInformationInfoIns(listSourceData[i]); if (tagFormat != null) { listTagInfos.Add(tagFormat); } //转换 if (i == listSourceData.Count - 1) { strRecordIndex = tagFormat.MachineID + "-" + tagFormat.IDNum + "-" + tagFormat.RecordIndex + "-" + tagFormat.States; } } catch (Exception exFormat) { #region 异常处理 ReaderExceptionEventArgs readerEX = new ReaderExceptionEventArgs(); readerEX.EquipmentStatus = EnumEquipmentStatus.SigleRecordException; exFormat.Source = " COM:" + base.CommPort.ToString() + " " + listSourceData[i]; readerEX.ExceptionObject = exFormat; readerEX.RecordTime = DateTime.Now; listExArgs.Add(readerEX); //OnReaderException(readerEX); if (i == listSourceData.Count - 1) { strRecordIndex = Convert.ToInt32(listSourceData[i].Substring(0, 2), 16).ToString() + "-" + listSourceData[i].Substring(4, 8) + "-" + listSourceData[i].Substring(12, 4) + "-" + listSourceData[i].Substring(2, 2); } #endregion } } this._listTagInfos = listTagInfos; #endregion List <string> listLog = new List <string>(); if (listTagInfos.Count == 0) { ReaderExceptionEventArgs args = new ReaderExceptionEventArgs(); args.EquipmentStatus = EnumEquipmentStatus.BatchRecordException; args.ExceptionObject = new Exception("COM:" + base.CommPort.ToString() + ",批量数据异常,请注意最近无数据返回的读写器。"); args.RecordTime = DateTime.Now; OnReaderException(args); } else if (listTagInfos.Count > 0) { if (listExArgs.Count > 0 && listExArgs.Count <= listTagInfos.Count) { foreach (ReaderExceptionEventArgs args in listExArgs) { if (args == null) { continue; } if (!listLog.Contains(args.ExceptionObject.Message)) { args.MachineID = int.Parse(listTagInfos[0].MachineID); OnReaderException(args); } } } } return(listTagInfos); } //处理确认信息数据数据 List <string> listEmptyData = ReaderGeneral.HandleSourceDataToList(strSourceData); if (listEmptyData != null && listEmptyData.Count > 0) { #region 处理确认信息数据数据返回 int iMacIDFinish = Convert.ToInt32(strSourceData.Substring(2, 2), 16); //当读写器无未确认数据或未采集到数据时,会返回一条为7字节的数据。 string strFinished = GetFinishedCollectionInstruction(iMacIDFinish); string strUploaded = GetUploadedCollectionInstruction(iMacIDFinish); if (listEmptyData.Contains(strFinished) || listEmptyData.Contains(strUploaded)) { TagInfoResultEventArgs arg = new TagInfoResultEventArgs(); arg.TagInformationList = new List <TagInformationInfo>(); TagInformationInfo tagInfo = new TagInformationInfo(); tagInfo.MachineID = iMacIDFinish.ToString(); arg.TagInformationList.Add(tagInfo); this.OnTagResult(arg); } #endregion } } return(listTagInfos); } catch (Exception Ex) { throw Ex; } }