/// <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); } }
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); } }
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............"); }
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; }
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); }