public TagInformationInfo GetTagInformationInfoIns(string strSource) { TagInformationInfo tagInfo = new TagInformationInfo(); tagInfo.MachineID = Convert.ToInt32(strSource.Substring(2, 2), 16).ToString();//StrMacID try { tagInfo.ReaderKey = tagInfo.MachineID.ToString(); tagInfo.States = strSource.Substring(4, 2); //StrStutasCode tagInfo.IDNum = strSource.Substring(8, 8); //StrID tagInfo.RecordIndex = strSource.Substring(16, 4); //StrRecordIndex tagInfo.TabNum = tagInfo.RecordIndex; tagInfo.Keyboard = strSource.Substring(20, 2); //StrTagKey tagInfo.Power = strSource.Substring(22, 2); //StrPowerValue tagInfo.TagID = strSource.Substring(24, 6); //StrTagID tagInfo.TagExistFirstDatetime = DateTime.Parse(ReaderGeneral.ChangetoDatebyHexstring(strSource.Substring(30, 12))); //StrFirstReadTime //this.StrValid = strSource.Substring(42, 2); tagInfo.ReadTimes = (int)Convert.ToInt64(strSource.Substring(44, 2), 16); //StrReadTimes tagInfo.ReadDatetime = DateTime.Parse(tagInfo.TagExistFirstDatetime.Date.ToShortDateString() + " " + ReaderGeneral.ChangetoDatebyHexstring(strSource.Substring(46, 6))); //StrLastReadTime tagInfo.CRC = strSource.Substring(52, 4); //StrCRC tagInfo.IsPowerLow = this.Power != "00" ? true : false; tagInfo.Antenna = 1; } catch (Exception ex) { if (tagInfo.MachineID != "0" || tagInfo.MachineID != "255") { Exception exx = new Exception(ex.Message + "-" + tagInfo.MachineID); throw exx; } throw ex; } return(tagInfo); }
/// <summary> /// 指令发送-批量确定数据 /// </summary> /// <param name="iMacID">机号</param> /// <param name="strRecordIndex">流水号</param> private void OrderSend_ConfirmBatchData(int iMacID, string strRecordIndex) { byte[] arrDataSend = { 0xC0, 0x00, 0x06, 0x02, 0x00, 0x00, 0x00, 0x00, 0xC1 }; byte[] byMacID = BitConverter.GetBytes(iMacID); arrDataSend[1] = byMacID[0]; int iRecordIndex = Convert.ToInt32(strRecordIndex, 16); byte[] byRecordIndex = BitConverter.GetBytes(iRecordIndex); arrDataSend[4] = byRecordIndex[1]; arrDataSend[5] = byRecordIndex[0]; byte lowCRC; byte highCRC; ReaderGeneral.CRC16_CCITT(arrDataSend, out highCRC, out lowCRC); arrDataSend[6] = lowCRC; arrDataSend[7] = highCRC; try { this.axMSComm05.Output = arrDataSend; Application.DoEvents(); } catch (Exception ex) { throw ex; } }
/// <summary> /// 抹除所有数据 /// </summary> private void OrderSend_DeleteRecord(int machineID) { byte[] data = { 0xC0, 0x00, 0xE9, 0x00, 0x00, 0x00, 0x00, 0xC1 }; byte[] iMachineID = BitConverter.GetBytes(machineID); data[1] = iMachineID[0]; byte lowCRC; byte highCRC; ReaderGeneral.CRC16_CCITT(data, out highCRC, out lowCRC); data[5] = lowCRC; data[6] = highCRC; try { if (this._SendClient != null) { this._SendClient.Send(data); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取机器时间 /// </summary> public void OrderSend_GetMacTime(int iMachineID) { byte[] arrDataSend = { 0xC0, 0x00, 0xAA, 0x00, 0x00, 0x00, 0xC1 }; byte[] bMacID = BitConverter.GetBytes(iMachineID); arrDataSend[1] = bMacID[0]; byte bLowCRC; byte bHighCRC; ReaderGeneral.CRC16_CCITT(arrDataSend, out bHighCRC, out bLowCRC); arrDataSend[4] = bLowCRC; arrDataSend[5] = bHighCRC; try { if (this._SendClient != null) { this._SendClient.Send(arrDataSend); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取数据已上传完毕的回发指令 /// </summary> /// <returns></returns> private string GetUploadedCollectionInstruction(int iMacID) { if (iMacID == 0) { return(null); } string strInst = string.Empty; byte[] data = { 0xC2, 0x00, 0x09, 0x00, 0x00, 0x00, 0xC3 }; byte[] iMachineID = BitConverter.GetBytes(iMacID); data[1] = iMachineID[0]; byte lowCRC; byte highCRC; ReaderGeneral.CRC16_CCITT(data, out highCRC, out lowCRC); data[4] = lowCRC; data[5] = highCRC; string strTmp = BitConverter.ToString(data); string[] arrTmp = strTmp.Split('-'); if (arrTmp.Length != 7) { return(null); } foreach (string strItem in arrTmp) { strInst += strItem; } return(strInst); }
/// <summary> /// 获取批量数据 /// </summary> private void OrderSend_GetBatchData(int iMachineID) { byte[] arrDataSend = { 0xC0, 0x00, 0x05, 0x00, 0x00, 0x00, 0xC1 }; byte[] bMacID = BitConverter.GetBytes(iMachineID); arrDataSend[1] = bMacID[0]; byte bLowCRC; byte bHighCRC; ReaderGeneral.CRC16_CCITT(arrDataSend, out bHighCRC, out bLowCRC); arrDataSend[4] = bLowCRC; arrDataSend[5] = bHighCRC; try { if (this._SendClient != null) { ReaderGeneral.ConsoleWrite("Begin Send...." + iMachineID.ToString()); if (this._listHistoryIndex.Count > 1024 * 100) { this._listHistoryIndex.Clear(); } this._SendClient.Send(arrDataSend); ReaderGeneral.ConsoleWrite("End Send...." + iMachineID.ToString()); } } catch (Exception ex) { ReaderGeneral.ConsoleWrite(ex.Message); //throw ex; } }
public override ReturnValueInfo Connect() { ReturnValueInfo rvInfo = new ReturnValueInfo(); //检查非空值 if (string.IsNullOrEmpty(base.ReaderIP)) { rvInfo.IsSuccess = false; rvInfo.MessageText = "IP地址不能为空,属性为ReaderIP"; return(rvInfo); } try { //检查地址格式 string strRegx = @"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])"; bool lIPChk = Common.General.IsRegEx(strRegx, base.ReaderIP); if (!lIPChk) { rvInfo.MessageText = "IP地址格式有误"; return(rvInfo); } //转换IP格式 List <byte> listIP = new List <byte>(); string[] arrIP = base.ReaderIP.Split('.'); foreach (string strIPItem in arrIP) { listIP.Add(Convert.ToByte(strIPItem)); } //打开连接 IPAddress ipAddr = new IPAddress(listIP.ToArray()); ReaderGeneral.ConsoleWrite("Begin Connecting............"); this._SendClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //this._SendClient.SendTimeout = 3000; //this._SendClient.ReceiveTimeout = 3000; this._SendClient.BeginConnect(ipAddr, ListenPort, new AsyncCallback(ConnectCallBack), this._SendClient); this._listHistoryIndex = new List <string>(); ReaderGeneral.ConsoleWrite("Into Waiting............."); //this._ConnectDone.WaitOne(); ReaderGeneral.ConsoleWrite("Wait Complete..........."); } catch (Exception ex) { rvInfo.MessageText = "连接出现异常:" + ex.Message; Common.General.WriteLocalLogs(Common.General.GetCurrentFuncName(), "连接出现异常:" + ex.Message, string.Empty, SystemLog.SystemLog.LogType.Error, SystemLog.SystemLog.FileType.LogFile); } return(rvInfo); }
protected override void OnTagResult(TagInfoResultEventArgs tagArgs) { #region 数据返回,设置读写器状态为连通中 if (this._isReading) { if (tagArgs != null && tagArgs.TagInformationList != null && tagArgs.TagInformationList.Count > 0) { int iMacIDRv = Convert.ToInt32(tagArgs.TagInformationList[0].MachineID); if (this.DicReaderDetails.Keys.Contains(iMacIDRv)) { ReaderExDetailInfo readerChk = this.DicReaderDetails[iMacIDRv]; readerChk.Ex_dtRvDataFormatChk = DateTime.Now; readerChk.Ex_dtRvData = DateTime.Now; readerChk.Ex_iCounterRvData = 0; if (readerChk.CurrentStatus != EnumEquipmentStatus.Conneted) { EquipmentStatusEventArgs statusArgs = new EquipmentStatusEventArgs(); statusArgs.MachineID = iMacIDRv; statusArgs.EquipmentStatus = EnumEquipmentStatus.Conneted; statusArgs.RecordTime = DateTime.Now; readerChk.CurrentStatus = EnumEquipmentStatus.Conneted; readerChk.Ex_lIsSendInvalidDataWarning = false; OnStatusChange(statusArgs); } } } } #endregion try { if (tagArgs != null) { if (tagArgs.TagInformationList != null && tagArgs.TagInformationList.Count > 0 && !string.IsNullOrEmpty(tagArgs.ExamineNum)) { if (!this._listHistoryIndex.Contains(tagArgs.ExamineNum)) { this._listHistoryIndex.Add(tagArgs.ExamineNum); base.OnTagResult(tagArgs); } OrderSend_ConfirmBatchData(int.Parse(tagArgs.TagInformationList[0].MachineID), tagArgs.ExamineNum); ReaderGeneral.ConsoleWrite("数据确认流水号:【" + tagArgs.ExamineNum + "】,数据来源机:" + tagArgs.TagInformationList[0].MachineID); } } } catch (Exception ex) { Common.General.WriteLocalLogs(Common.General.GetCurrentFuncName(), "确认数据异常:" + ex.Message, string.Empty, SystemLog.SystemLog.LogType.Error, SystemLog.SystemLog.FileType.LogFile); } }
/// <summary> /// 指令发送-校对读写器时间 /// </summary> /// <param name="strMacID">机号</param> private void OrderSend_ResetTime(string strMacID) { byte[] data = { 0xC0, 0xFF, 0xA9, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1 }; int imacID = 0; if (!string.IsNullOrEmpty(strMacID) && int.TryParse(strMacID, out imacID)) { byte[] bMacID = BitConverter.GetBytes(imacID); data[1] = bMacID[0]; } int year = DateTime.Now.Year - 2000; int month = DateTime.Now.Month; int day = DateTime.Now.Day; int hour = DateTime.Now.Hour; int minute = DateTime.Now.Minute; int second = DateTime.Now.Second; data[4] = BitConverter.GetBytes(year)[0]; data[5] = BitConverter.GetBytes(month)[0]; data[6] = BitConverter.GetBytes(day)[0]; data[7] = BitConverter.GetBytes(hour)[0]; data[8] = BitConverter.GetBytes(minute)[0]; data[9] = BitConverter.GetBytes(second)[0]; byte lowCRC; byte highCRC; ReaderGeneral.CRC16_CCITT(data, out highCRC, out lowCRC); data[11] = lowCRC; data[12] = highCRC; try { this.axMSComm05.Output = data; Application.DoEvents(); } catch (Exception ex) { throw ex; } }
public TH24GTypeAR_Socket() { try { this._isConnected = false; this._iMaxRvDataTime = -3600; this._iRetryMaxCount = 2000; this._isReading = false; this.MachineID = 0; this._dicReaderDetails = new Dictionary <int, ReaderExDetailInfo>(); this._ConnectDone = new ManualResetEvent(false); this.OnMachineIDChanging += new MachineIDChangedDelegate(TH24GTypeAR_Socket_OnMachineIDChanging); this._tmrReadBatchData = new System.Timers.Timer(); this._tmrReadBatchData.Interval = 1000; this._tmrReadBatchData.Elapsed += new System.Timers.ElapsedEventHandler(_tmrReadBatchData_Elapsed); this._tmrChkNullData = new System.Timers.Timer(); this._tmrChkNullData.Interval = 3000; this._tmrChkNullData.Elapsed += new System.Timers.ElapsedEventHandler(_tmrChkNullData_Elapsed); this._tmrCheckTime = new System.Timers.Timer(); this._tmrCheckTime.Interval = 5000; this._tmrCheckTime.Elapsed += new System.Timers.ElapsedEventHandler(_tmrCheckTime_Elapsed); this._tmrReceiveData = new System.Timers.Timer(); this._tmrReceiveData.Interval = 4000; this._tmrReceiveData.Elapsed += new System.Timers.ElapsedEventHandler(_tmrReceiveData_Elapsed); } catch (Exception ex) { ReaderGeneral.ConsoleWrite(ex.Message); ReaderExceptionEventArgs args = new ReaderExceptionEventArgs(); args.ExceptionObject = ex; args.RecordTime = DateTime.Now; OnReaderException(args); } }
/// <summary> /// 重置机器时间 /// </summary> private void OrderSend_ResetTime(int iMachineID) { byte[] data = { 0xC0, 0xFF, 0xA9, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1 }; byte[] bMacID = BitConverter.GetBytes(iMachineID); data[1] = bMacID[0]; int year = DateTime.Now.Year - 2000; int month = DateTime.Now.Month; int day = DateTime.Now.Day; int hour = DateTime.Now.Hour; int minute = DateTime.Now.Minute; int second = DateTime.Now.Second; data[4] = BitConverter.GetBytes(year)[0]; data[5] = BitConverter.GetBytes(month)[0]; data[6] = BitConverter.GetBytes(day)[0]; data[7] = BitConverter.GetBytes(hour)[0]; data[8] = BitConverter.GetBytes(minute)[0]; data[9] = BitConverter.GetBytes(second)[0]; byte lowCRC; byte highCRC; ReaderGeneral.CRC16_CCITT(data, out highCRC, out lowCRC); data[11] = lowCRC; data[12] = highCRC; try { if (this._SendClient != null) { this._SendClient.Send(data); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 格式化机器时间值 /// </summary> /// <param name="strInput">需要处理的返回指令数据</param> /// <returns></returns> private DateTime?DataHandling_FormatMacTimeVal(string strInput, ref int iMacID) { DateTime?dtMac = null; try { List <string> listSourceData = ReaderGeneral.HandleSourceDataToList(strInput, this._iRecordMaxLen); if (listSourceData.Count > 0) { foreach (string itemData in listSourceData) { string strCut = DataHandling_CutDataHeadTail(itemData); if (!string.IsNullOrEmpty(strCut)) { iMacID = Convert.ToInt32(strCut.Substring(0, 2), 16); int iYear = Convert.ToInt32(strCut.Substring(4, 2), 16) + 2000; int iMonth = Convert.ToInt32(strCut.Substring(6, 2), 16); int iDay = Convert.ToInt32(strCut.Substring(8, 2), 16); int iHour = Convert.ToInt32(strCut.Substring(10, 2), 16); int iMinute = Convert.ToInt32(strCut.Substring(12, 2), 16); int iSecond = Convert.ToInt32(strCut.Substring(14, 2), 16); string strDate = iYear.ToString() + "-" + iMonth.ToString() + "-" + iDay.ToString() + " " + iHour.ToString() + ":" + iMinute.ToString() + ":" + iSecond.ToString(); DateTime dtDate = DateTime.Parse(strDate); //TIPS: 使读写器的时间误差在2分钟内。 if (dtDate >= DateTime.Now.AddMinutes(-2) && dtDate <= DateTime.Now.AddMinutes(2)) { dtMac = dtDate; break; } } } } } catch (Exception) { return(null); } return(dtMac); }
/// <summary> /// 抹除读写器数据 /// </summary> /// <param name="machineID"></param> private void OrderSend_DeleteRecord(int machineID) { byte[] data = { 0xC0, 0x00, 0xE9, 0x00, 0x00, 0x00, 0x00, 0xC1 }; byte[] iMachineID = BitConverter.GetBytes(machineID); data[1] = iMachineID[0]; byte lowCRC; byte highCRC; ReaderGeneral.CRC16_CCITT(data, out highCRC, out lowCRC); data[5] = lowCRC; data[6] = highCRC; try { this.axMSComm05.Output = data; } catch (Exception ex) { throw ex; } }
/// <summary> /// 标签数据格式化实体构造函数 /// </summary> /// <param name="strTagData">未格式化的源数据</param> public TagFormat(string strTagData) { try { this.StrMacID = strTagData.Substring(0, 2); this.StrStutasCode = strTagData.Substring(2, 2); this.StrID = strTagData.Substring(4, 8); this.StrRecordIndex = strTagData.Substring(12, 4); this.StrTagKey = strTagData.Substring(16, 4); this.StrPowerValue = strTagData.Substring(18, 2); this.StrTagID = strTagData.Substring(20, 6); this.StrFirstReadTime = ReaderGeneral.ChangetoDatebyHexstring(strTagData.Substring(26, 12)); this.StrValid = strTagData.Substring(38, 2); this.StrReadTimes = Convert.ToInt64(strTagData.Substring(40, 2), 16).ToString(); this.StrLastReadTime = DateTime.Parse(StrFirstReadTime).ToShortDateString() + " " + ReaderGeneral.ChangetoDatebyHexstring(strTagData.Substring(42, 6)); this.StrCRC = strTagData.Substring(48, 4); } catch (Exception Ex) { Exception ex = new Exception("异常信息:" + Ex.Message + Environment.NewLine + "异常源数据" + strTagData); throw ex; } }
/// <summary> /// 指令发送-批量获取11条记录 /// </summary> /// <param name="iMachineID">机号</param> public void OrderSend_GetBatchData(int iMachineID) { byte[] arrDataSend = { 0xC0, 0x00, 0x05, 0x00, 0x00, 0x00, 0xC1 }; byte[] bMacID = BitConverter.GetBytes(iMachineID); arrDataSend[1] = bMacID[0]; byte bLowCRC; byte bHighCRC; ReaderGeneral.CRC16_CCITT(arrDataSend, out bHighCRC, out bLowCRC); arrDataSend[4] = bLowCRC; arrDataSend[5] = bHighCRC; try { this.axMSComm05.Output = arrDataSend; Application.DoEvents(); } catch (Exception) { //throw ex; } }
private void ConnectCallBack(IAsyncResult res) { try { // 从state对象获取socket. Socket client = (Socket)res.AsyncState; // 完成连接. client.EndConnect(res); ReaderGeneral.ConsoleWrite("Socket connected to " + client.RemoteEndPoint.ToString()); this._isConnected = true; } catch (Exception ex) { ReaderGeneral.ConsoleWrite(ex.Message); this._SendClient = null; ReaderGeneral.ConsoleWrite("需要重新初始化.............."); } // 连接已完成,主线程继续. //this._ConnectDone.Set(); ReaderGeneral.ConsoleWrite("Connect OK............"); }
/// <summary> /// 确认批量数据 /// </summary> private void OrderSend_ConfirmBatchData(int iMacID, string strRecordIndex) { byte[] arrDataSend = { 0xC0, 0x00, 0x06, 0x02, 0x00, 0x00, 0x00, 0x00, 0xC1 }; byte[] byMacID = BitConverter.GetBytes(iMacID); arrDataSend[1] = byMacID[0]; int iRecordIndex = Convert.ToInt32(strRecordIndex, 16); byte[] byRecordIndex = BitConverter.GetBytes(iRecordIndex); arrDataSend[4] = byRecordIndex[1]; arrDataSend[5] = byRecordIndex[0]; byte lowCRC; byte highCRC; ReaderGeneral.CRC16_CCITT(arrDataSend, out highCRC, out lowCRC); arrDataSend[6] = lowCRC; arrDataSend[7] = highCRC; try { if (this._SendClient != null) { this._SendClient.Send(arrDataSend); //DataReceive(); } } catch (Exception ex) { ReaderExceptionEventArgs args = new ReaderExceptionEventArgs(); args.MachineID = iMacID; args.ExceptionObject = ex; args.RecordTime = DateTime.Now; OnReaderException(args); } }
public override bool Disconnect() { try { if (!this._isConnected) { return(true); } if (this._tmrChkNullData != null) { this._tmrChkNullData.Stop(); } if (this._tmrReadBatchData != null) { this._tmrReadBatchData.Stop(); } if (this._tmrReceiveData != null) { this._tmrReceiveData.Stop(); } if (this._tmrCheckTime != null) { this._tmrCheckTime.Stop(); } if (this._dicReaderDetails != null) { foreach (KeyValuePair <int, ReaderExDetailInfo> detailPair in this._dicReaderDetails) { if (detailPair.Value.CurrentStatus != EnumEquipmentStatus.Disconneted) { detailPair.Value.CurrentStatus = EnumEquipmentStatus.Disconneted; EquipmentStatusEventArgs statusArgs = new EquipmentStatusEventArgs(); statusArgs.EquipmentStatus = EnumEquipmentStatus.Disconneted; statusArgs.RecordTime = DateTime.Now; statusArgs.MachineID = detailPair.Value.Ex_MacID; OnStatusChange(statusArgs); } detailPair.Value.ResetAllSign(); } } this._listHistoryIndex.Clear(); if (this._SendClient == null) { this._SendClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } this._SendClient.Shutdown(SocketShutdown.Both); this._SendClient.Disconnect(false); this._SendClient.Close(); ReaderGeneral.ConsoleWrite("Disconnected.........."); } catch (Exception ex) { ReaderExceptionEventArgs args = new ReaderExceptionEventArgs(); args.ExceptionObject = ex; args.RecordTime = DateTime.Now; OnReaderException(args); return(false); } finally { this._isConnected = false; } return(true); }
/// <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; } }
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; }