예제 #1
0
//---------------------------------------------------------------------------
void __fastcall TCollectDataWOThread::Execute()
{
	//---- Place thread code here ----
	if(SerReceiveFunc&&SendAckFunc)
	{
    	String filepathstr = GlobalPath+"\\StoreRealCWOIBData.dat";
		hStoreToFile = CreateFile(filepathstr.t_str(),
								  GENERIC_READ | GENERIC_WRITE,
								  0,
								  NULL,
								  CREATE_ALWAYS,
								  FILE_ATTRIBUTE_NORMAL,
								  NULL);
        if(!hStoreToFile)
        {
            MessageBox(SDIAppForm->Handle, "无法创建数据存储文件,请联系技术人员!", "Error", MB_OK|MB_ICONERROR);
			*validcollecttag = false;
			ModeOFCollect = 0;
			return;
        }

		ValidLostInfoT->Active = false;
		ValidLostInfoT->Active = true;
		ValidLostInfoT->First();

		*validcollecttag = true;
		ModeOFCollect = 5;
		String CollectTime = "*CollectTime* ";
		CollectTime += Date();
		CollectTime += " ";
		CollectTime += Time();

//		DataSaveToFileStream<<endl<<CollectTime.t_str()<<endl;

		TTreeNode* tmpnode = ValidPosTreeList->Items->GetFirstNode();
		while(tmpnode)
		{
			if(!Terminated)
			{
				tmpnode->Data;
				POSPARA* tmppospara = (POSPARA*)(tmpnode->Data);
				if(4 == tmppospara->tag)
				{
					WORD ReceiveStatus = -1;
					WORD SynchroStatus = -1;
					char temp[10] = "";
					unsigned char como = (unsigned char)(tmppospara->comnum);
					WORD PosNO = (WORD)(tmppospara->posnum);
					unsigned char ReceiveCMD = 0x25;
					unsigned char SynchroCMD = 0x54;
					ZeroMemory(receivebuf, 16);
					int receivelen = 16;
					unsigned char* dir = "E:\\RAD\\2010-9-26\\Recieve";
					int dirlen = strlen(dir);

			 //		unsigned char* SynchroTagbuf = new unsigned char[300];
			 //		ZeroMemory(SynchroTagbuf, *ONLinePosNum);

					String comparestr = "FFFFFF";

			 /*		UpdateDataBaseQuery->Close();
					UpdateDataBaseQuery->SQL->Clear();
					String tmpsql = "select * from SFJPARAM where JH='";
					tmpsql += tmppospara->posnum;
					tmpsql += "'";
					UpdateDataBaseQuery->SQL->Add(tmpsql);
					UpdateDataBaseQuery->Open();
					Place = UpdateDataBaseQuery->FieldByName("STNAME")->AsAnsiString.Trim();
					UpdateDataBaseQuery->Close();    */

					if(!Terminated)
					{
						ReceiveStatus = SerReceiveFunc(como, PosNO, ReceiveCMD, receivebuf, receivelen, 1, dir, dirlen);
						if(0 == ReceiveStatus)
						{
							SynchroStatus = SendAckFunc(como, SynchroCMD);
							if(0 == SynchroStatus)
							{
								OrignalDataStr = "";
								for (int i = 0; i < receivelen; i++)
								{
									OrignalDataStr = OrignalDataStr + (IntToHex((byte)receivebuf[i], 2));
								}

								if(comparestr != OrignalDataStr.SubString(0, 6))
								{
									PosNumber = PosNO;
									DataStatusStr = "正常";
									Synchronize(SaveDataToFileFunc);
								//	Synchronize(UpdateDataBaseFunc);
									Synchronize(DisplayDataFunc);
								}
							}
							Sleep(50);
						}

						//挂失一条
						if(ValidGSTag)
						{
							int GSkh;
							unsigned char SendStr[3];
							int Strlen = 3;
							unsigned char status;
							unsigned char port;
							WORD pos;
							unsigned char CMD;

							if(!ValidLostInfoT->Eof)
							{
								GSkh = ValidLostInfoT->FieldByName("PARA")->AsInteger;
								SendStr[0] = (unsigned char)(GSkh/65536);
								SendStr[1] = (unsigned char)((GSkh%65536)/256);
								SendStr[2] = (unsigned char)GSkh;
								port = (unsigned char)(ValidLostInfoT->FieldByName("SFJPORT")->AsInteger);
								pos = WORD(ValidLostInfoT->FieldByName("SFJID")->AsInteger);

								if(1 == ValidLostInfoT->FieldByName("CMD")->AsInteger)
								{
									CMD = 0x33;
									status = SerSendFunc(port, pos, CMD, SendStr, Strlen, 1);
									if(0 == status)
									{
										ValidLostInfoT->Delete();
								   //		ValidLostInfoT->Next();
									}
									else
										ValidLostInfoT->Next();
								}
								else if(2 == ValidLostInfoT->FieldByName("CMD")->AsInteger)
								{
									CMD = 0x38;
									status = SerSendFunc(port, pos, CMD, SendStr, Strlen, 1);
									if(0 == status)
									{
										ValidLostInfoT->Delete();
									//	ValidLostInfoT->Next();
									}
									else
										ValidLostInfoT->Next();
								}
								Sleep(50);
							}
						}
					}
					else
					{
						*validcollecttag = false;
						ModeOFCollect = 0;

                        ValidCProgressBar->Properties->Marquee = false;

				  //		if(DataSaveToFileStream)
				  //			DataSaveToFileStream.close();
				  ///		RealTimeCollectFRM->Close();
						return;
					}
				}
			}
			else
			{
				*validcollecttag = false;
				ModeOFCollect = 0;
		   //		if(DataSaveToFileStream)
		   //			DataSaveToFileStream.close();
				RealTimeCollectFRM->Close();
				return;
			}
			tmpnode = tmpnode->GetNext();
			if(!tmpnode)
				tmpnode = ValidPosTreeList->Items->GetFirstNode();
		}
	}
	else
	{
		*validcollecttag = false;
		ModeOFCollect = 0;
	//	if(DataSaveToFileStream)
	//		DataSaveToFileStream.close();
		MessageBox(SDIAppForm->Handle, "Error in Load SerReceiveFunc Or SendAckFunc!", "Error", MB_OK);
	}
}
void __fastcall TBatchCollectDataThread::Execute()
{
	//---- Place thread code here ----
	ValidSBTN->Enabled = false;
	if(SerReceiveFunc&&SendAckFunc)
	{
/*		String filepathstr = GlobalPath+"\\StroeBatchDataFile.dat";
		hStoreToFile = CreateFile(filepathstr.t_str(),
								  GENERIC_READ | GENERIC_WRITE,
								  0,
								  NULL,
								  CREATE_ALWAYS,
								  FILE_ATTRIBUTE_NORMAL,
								  NULL);
		if(hStoreToFile)
		{      */
			errortag = false;
			*validcollecttag = true;
			ValidEBTN->Enabled = true;
			ModeOFCollect = 2;
			String CollectTime = "*CollectTime* ";
			CollectTime += Date();
			CollectTime += " ";
			CollectTime += Time();

			BatchDataSaveToFileStream<<endl<<CollectTime.t_str()<<endl;
			nodecount = ValidPosTreeList->Items->Count;
			currentcount = 0;

			ValidCProgressBar->Position = 0;
			TTreeNode* tmpnode = ValidPosTreeList->Items->GetFirstNode();
			while(tmpnode)
			{
            	errortag = false;
				poscollectedrecordcount = 0;
				if(!Terminated)
				{
					tmpnode->Data;
					POSPARA* tmppospara = (POSPARA*)(tmpnode->Data);
					if(4 == tmppospara->tag)
					{
						WORD ReceiveStatus = -1;
						WORD SynchroStatus = -1;
						char temp[10] = "";
						unsigned char como = (unsigned char)(tmppospara->comnum);
						PosNumber = tmppospara->posnum;
						WORD PosNO = (WORD)(tmppospara->posnum);
						unsigned char ReceiveCMD = 0x23;
						unsigned char SynchroCMD = 0x54;
						ZeroMemory(receivebuf, 2048);
						int receivelen = 2048;
						unsigned char* dir = "E:\\RAD\\2010-9-26\\Recieve";
						int dirlen = strlen(dir);

						UpdateDataBaseQuery->Close();
						UpdateDataBaseQuery->SQL->Clear();
						String tmpsql = "select * from SFJPARAM where JH=";
						tmpsql += tmppospara->posnum;
				   //		tmpsql += "'";
						UpdateDataBaseQuery->SQL->Add(tmpsql);
						UpdateDataBaseQuery->Open();
						Place = UpdateDataBaseQuery->FieldByName("STNAME")->AsAnsiString.Trim();
						UpdateDataBaseQuery->Close();

						int recordinpos = 0;
						WORD posstatus = -1;
						WORD poscomo = como;
						unsigned char statusCMD = 0x43;
						unsigned char statusbuf[16];
						ZeroMemory(statusbuf,16);
						int statuslen = 16;
						posstatus = SerReceiveFunc(poscomo, PosNO, statusCMD, statusbuf, statuslen, 1, dir, dirlen);
						if(0 == posstatus)
						{
							int lowptr = 256*(int)statusbuf[0] + (int)statusbuf[1];
							int highptr = 256*(int)statusbuf[2] + (int)statusbuf[3];
							if(highptr >= lowptr)
							{
								recordinpos = highptr - lowptr;
							}
							else
							{
								recordinpos = highptr + 6576 - lowptr;
							}
							posallrecordcount = recordinpos;
						}
						else
						{
							tmpnode->Text = "POS机异常";
							tmpnode = tmpnode->GetNext();
							currentcount++;
							continue;
						}

						tmpcaptionstr = "正在采集";
						tmpcaptionstr += tmppospara->posnum;
						tmpcaptionstr += "号机,位于:";
						tmpcaptionstr += Place;
						tmpcaptionstr += ",该POS机含有消费记录";
						tmpcaptionstr += recordinpos;
						tmpcaptionstr += "条。";
						ValidInfoPanel->Caption = tmpcaptionstr;
						bool continued = true;
						DisplayCollectingFunc();
						while(continued)
						{
							if(!Terminated)
							{
								ReceiveStatus = SerReceiveFunc(como, PosNO, ReceiveCMD, receivebuf, receivelen, 1, dir, dirlen);
								if(0 == ReceiveStatus)
								{
									SynchroStatus = SendAckFunc(como, SynchroCMD);
									if(0 == SynchroStatus)
									{
									//	for(int i = 0; i < 2048; i++)
									//		BatchDataSaveToFileStream<<receivebuf[i];
										if((0x00 == receivebuf[10])&&(0x00 == receivebuf[11]))
											continued = false;
										if(0x00 != receivebuf[8])
										{
											Synchronize(AnalyzeDataFunc);
										}
										CollectOverStautsStr = "已完成批量数据采集!";
									}
								//	else
								//	{
								//		MessageBox(SDIAppForm->Handle, "Sysc error", "error", MB_OK|MB_ICONERROR);
								//	}
								}
								else
								{
									continued = false;
									CollectOverStautsStr = "POS机通信异常,采集数据不完整!";
									errortag = true;
								}
								Sleep(100);
							}
							else
							{
								continued = false;
								*validcollecttag = false;
								ModeOFCollect = 0;
								CloseHandle(hStoreToFile);
						  //		ValidStoreBtn->Enabled = true;
								ValidEBTN->Enabled = true;
						  //		if(BatchDataSaveToFileStream)
						  //			BatchDataSaveToFileStream.close();
								BatchCollectFRM->Close();
								ValidSBTN->Enabled = true;
								return;
							}
						}
						DisplayEndCollectFunc();
					}
				}
				else
				{
					*validcollecttag = false;
					ModeOFCollect = 0;
			   //		CloseHandle(hStoreToFile);
			   //		ValidStoreBtn->Enabled = true;
					ValidEBTN->Enabled = true;
			   //		if(BatchDataSaveToFileStream)
			   //			BatchDataSaveToFileStream.close();
					BatchCollectFRM->Close();
					ValidSBTN->Enabled = true;
					return;
				}
				tmpnode = tmpnode->GetNext();
				currentcount++;
				ValidCProgressBar->Position = currentcount*100/nodecount;
			}
			*validcollecttag = false;
	   //		CloseHandle(hStoreToFile);
	   //		ValidStoreBtn->Enabled = true;
			ValidEBTN->Enabled = true;
	   //		if(BatchDataSaveToFileStream)
	   //			BatchDataSaveToFileStream.close();
	   		ValidCProgressBar->Position = 100;
			if(!errortag)
			{
				ValidInfoPanel->Caption = "数据采集完毕!";
				MessageBox(SDIAppForm->Handle, "数据采集完毕!", "采集完毕", MB_OK);
			}
			else
			{
				ValidInfoPanel->Caption = "数据采集过程中出现通信异常,部分数据采集不成功,可以通过重新采集修补!";
				MessageBox(SDIAppForm->Handle, "数据采集过程中出现通信异常,部分数据采集不成功,可以通过重新采集修补!", "采集不完整", MB_OK|MB_ICONERROR);
			}

	   /*		if(mrOk == MessageBox(SDIAppForm->Handle, "数据采集完毕,是否进行数据入库?", "采集完毕", MB_OKCANCEL|MB_ICONQUESTION))
            	ValidStoreBtn->Click();
            else
            {
                ValidCProgressBar->Properties->Marquee = false;
			}         */

/*		}
		else
		{
			MessageBox(SDIAppForm->Handle, "无法创建数据存储文件,请联系技术人员!", "Error", MB_OK|MB_ICONERROR);
	   //		if(BatchDataSaveToFileStream)
	   //			BatchDataSaveToFileStream.close();
			*validcollecttag = false;
			ModeOFCollect = 0;
			return;
		}         */
	}
	else
	{
		*validcollecttag = false;
		ModeOFCollect = 0;
		if(BatchDataSaveToFileStream)
			BatchDataSaveToFileStream.close();
		MessageBox(SDIAppForm->Handle, "Error in Load SerReceiveFunc Or SendAckFunc!", "Error", MB_OK|MB_ICONERROR);
	}
	ValidSBTN->Enabled = true;
}