Example #1
0
//////////////////////////////////////////////////////////////////////
// WatchCommDev
// return:   0  success
//          -1  error
//
int CAsyncPort::WatchCommDev()
{
	OVERLAPPED ovl;
	memset(& ovl, 0, sizeof (OVERLAPPED));

	// create I/O event used for overlapped read
	ovl.hEvent = CreateEvent (
		NULL,	// no security
		TRUE,	// explicit reset req
		FALSE,	// initial event reset
		NULL	// no name
	);

	if (ovl.hEvent == NULL) {
		return (-1);
	}

	if (! SetCommMask(m_hCommDev, EV_RXCHAR)){
		return (-1);
	}

	int nLength;
	BYTE baTmpInBuf[IN_QUEUE_SIZE];

	while (m_isConnected)
	{
		DWORD dwEvtMask = 0;

		WaitCommEvent(m_hCommDev, & dwEvtMask, NULL);

		if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR) {
			do {
				if ((nLength = ReadCommBlock((char *) baTmpInBuf, IN_QUEUE_SIZE)) > 0) {
					// sync
                    HANDLE hMutex = CreateMutex(NULL, FALSE, m_szMutexName);
                    if (hMutex == NULL) {
                        break;
                    }
                    if (WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED) {
                        CloseHandle(hMutex);
                        break;
                    }

					if (*m_szInLogPath) {
						HANDLE hFile = CreateFile (
							m_szInLogPath,
							GENERIC_WRITE,
							0,
							(LPSECURITY_ATTRIBUTES) NULL,
							OPEN_ALWAYS,
							FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
							NULL
						);
						if (INVALID_HANDLE_VALUE != hFile) {
							DWORD dwBytes;
							SetFilePointer(hFile, 0, NULL, FILE_END);
							WriteFile(hFile, baTmpInBuf, nLength, & dwBytes, NULL);
							CloseHandle(hFile);
						}
					}

					int nBytes =
						nLength + m_nInBufSize > IN_BUFFER_SIZE ?
						IN_BUFFER_SIZE - m_nInBufSize :
						nLength ;

					BYTE * bp = baTmpInBuf;
					int nIndex = (m_nInBufIndex + m_nInBufSize) % IN_BUFFER_SIZE;

					m_nInBufSize += nBytes;

					while (nBytes -- > 0) {
						m_baInBuf[nIndex] = *bp ++;
						nIndex = (nIndex + 1) % IN_BUFFER_SIZE;
					}

					ReleaseMutex(hMutex);
					CloseHandle(hMutex);
				}
			}
			while (nLength > 0) ;
		}
	}

	// get rid of event handle

	CloseHandle(ovl.hEvent);

	// clear information in structure (kind of a "we're done flag")

	m_dwThreadID = 0;
	m_hWatchThread = NULL;

	return(0);
}
Example #2
0
DWORD WINAPI CommMonitor( LPSTR lpData)
{
    //监控串口接收数据,并完成数据格式分析,将结果放入结果队列
	int		nItemCount,SampleIDLen,nDataOff,nResultLen,iSampleID;
	bool	bLogFile;
	char    TempWorkingID[7];
	LPSTR   lpPointer;
	//应答包
	char * p_Ack="\x2\x6\x3";
	char * p_Nak="\x2\x16\x3";
	char * p_IntSessionTXT="\x2I\x20\x3";
	char * p_ReleaseMaster="\x2M\x20\x20\x20\x20\x20\x3";
	char  PackageType;
	char SampleID[10];
	SYSTEMTIME SystemTime;
	CommData
    //通信类型1'、1''、2、3、4、5
	
	Init()
	ResetOpPointer()				

	FillDevName()					
	SetCommWaitMask(EV_RXFLAG)
	PurgeComm( hComm, PURGE_RXCLEAR );	
	memset(lpOpPointer , 0, c_RxBufferLength );
	if(NewLogFile("AEROSET.txt"))
	{
		bLogFile=TRUE;
	}

	while (TRUE)
	{


		WaitEvent(EV_RXFLAG)//等待接受数据包
		GetInQueueLength(dwReadLength)//分析数据
		ReadCommBlock(dwReadLength)
		PackageType=*(lpOpPointer+1);//数据包的类型
		if(bLogFile)
		{
			WriteLogData(hLogFile,lpOpPointer,dwReadLength);
		}
		//校验接收到的数据,若正确,则处理,否则给否定应答。
		/*
			checksum  calculation
		*/

		/*结果的数据块中可能包含病人信息、ID信息、结果数据等组。*/
		PackageType=*(lpOpPointer+1);
		switch(PackageType)
		{
		case 'Q':
			WriteCommBlock(p_Ack,3)//肯定应答
			//WriteCommChar(ACK)
			break;

		case 'R':
			//结果处理
			WriteCommBlock(p_Ack,3)//肯定应答
			iSampleID=StrToInt(lpOpPointer+c_SampleIDOff,c_SamlpeIDLen);
			itoa(iSampleID,&SampleID[0],10);
			SampleIDLen=strlen(SampleID);
			TempWorkingID[0]=lpDevice->DevItemType;
			if(SampleIDLen>=6)
			{
				strncpy(&TempWorkingID[1],&SampleID[SampleIDLen-5],5);
			}
			else
				strncpy(&TempWorkingID[6-SampleIDLen],SampleID,SampleIDLen);
			TempWorkingID[6]='\0';
			GetLocalTime(&SystemTime);
			nItemCount=0;
			for(nDataOff =c_DataOff; *(lpOpPointer+nDataOff)!=ETB ;)
			{
				strncpy(OutResult[nItemCount].WorkingID,TempWorkingID,7);
				OutResult[nItemCount].ItemNo=nItemCount+1;
				lpPointer=lpOpPointer+nDataOff;
				DeleSpace(4)
				strncpy(OutResult[nItemCount].ItemID,lpPointer,nResultLen);
				OutResult[nItemCount].ItemID[nResultLen]='\0';
				lpPointer=lpOpPointer+nDataOff+c_ResultOff;
				DeleSpace(c_ResultLen )
				FillResult(nItemCount, lpPointer ,nResultLen ) 
				FillDate(nItemCount, SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wHour,SystemTime.wMinute,SystemTime.wSecond)
				nDataOff = nDataOff + c_OneDataLen;
				nItemCount++;
			}
			if(nItemCount>0)
			{
				(*lpResultProcessRoutine)(lpDevice->nDevNO, OutResult, nItemCount);
				lpDevice->dwRecordCount+=nItemCount;
			}
		
			//WriteCommChar(ACK)
		///	WriteCommBlock(p_Ack,3)//肯定应答
			break;
		case ACK:
		case NAK:
			break;
       
		}
	

		ResetRxPointer()		
	

	}
	if(bLogFile)
	{
		CloseLogFile(hLogFile);
	}
	return TRUE;
} // end of CommWatchProc()
Example #3
0
DWORD FAR PASCAL CommWatchProc(LPSTR lpData)
{
    DWORD dwEvtMask;
    OVERLAPPED os;
    int nLength, len;
    BYTE abIn[MAXBLOCK + 1];
    BYTE recBuf[MAXBLOCK + 1];
    int i;
    static int recBufAnz = 0;
    char ch;
    short int *coord;

    memset(&os, 0, sizeof(OVERLAPPED));

    // create I/O event used for overlapped read

    os.hEvent = CreateEvent(NULL, // no security
                            TRUE, // explicit reset req
                            FALSE, // initial event reset
                            NULL); // no name
    if (os.hEvent == NULL)
    {
        MessageBox(NULL, "Failed to create event for thread!", "HEFDCOM Error!",
                   MB_ICONEXCLAMATION | MB_OK);
        return (FALSE);
    }

    if (!SetCommMask(headfinderInfo.idComDev, EV_RXCHAR))
        return (FALSE);

    while (headfinderInfo.fConnected)
    {
        dwEvtMask = 0;

        WaitCommEvent(headfinderInfo.idComDev, &dwEvtMask, NULL);
        if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR)
        {
            do
            {
                if (nLength = ReadCommBlock(hHEFDCOMWnd, (LPSTR)abIn, MAXBLOCK))
                {
                    // Prüfe empfangene Daten - reagiere je nach Inhalt
                    memcpy(recBuf + recBufAnz, abIn, nLength);
                    recBufAnz += nLength;
                    len = recBufAnz;
                    i = 0;

                    while (i < len)
                    {
                        while ((i < len) && (recBuf[i] != 0x80))
                            i++;
                        if (i == len)
                        {
                            recBufAnz = 0;
                            break;
                        }
                        if (i < len - 1)
                        {
                            if ((recBuf[i + 1]) == 0x83)
                            {
                                if ((len - i) > 6)
                                { // noch genügend Zeichen im Puffer -> auswerten
                                    i += 2;
                                    // i steht jetzt auf x
                                    // Byte vertauschen (big endian)
                                    ch = recBuf[i];
                                    recBuf[i] = recBuf[i + 1];
                                    recBuf[i + 1] = ch;
                                    coord = (short int *)(recBuf + i);
                                    headX = *coord;
                                    i += 2;
                                    ch = recBuf[i];
                                    recBuf[i] = recBuf[i + 1];
                                    recBuf[i + 1] = ch;
                                    coord = (short int *)(recBuf + i);
                                    headY = *coord;
                                    i += 2;
                                    ch = recBuf[i];
                                    recBuf[i] = recBuf[i + 1];
                                    recBuf[i + 1] = ch;
                                    coord = (short int *)(recBuf + i);
                                    headZ = *coord;
                                    fifoX[fifoPos] = headX;
                                    fifoY[fifoPos] = headY;
                                    fifoZ[fifoPos] = headZ;
                                    fifoPos++; // fifoPos &= fifoDepth;
                                    if (fifoPos == fifoDepth)
                                        fifoPos = 0;
                                    i += 2;
                                    if (i == len)
                                        recBufAnz = 0;
                                    // Koordinaten stehen jetzt in x,y und z zur Verfügung
                                    // im Beispiel werden Sie nur angezeigt:
                                    //char WndTxtBuf[80]="";
                                    //                      sprintf(WndTxtBuf,"HeadFinder - Augenposition | x: %d  y: %d  z: %d",x,y,z);
                                    //                    SetWindowText(hHEFDCOMWnd,WndTxtBuf);
                                }
                                else //if ((len-i)>6)
                                { //Länge reicht nicht - empfangene Daten abspeichern; aufheben
                                    memcpy(recBuf, recBuf + i, len - i);
                                    recBufAnz = len - i;
                                    recBuf[len - i] = 0;
                                    break;
                                }
                            }
                            else // if (recBuf[i+1]) == 0x83)
                            {
                                i++;
                            }
                        }
                        else // if (i<len-1)
                        { // i==len-1
                            recBuf[0] = 0x80;
                            recBufAnz = 1;
                            break;
                        }
                    } // while (i<len)

                    // force a paint
                }
            } while (nLength > 0);
        }
    } // while ( CONNECTED( npHEFDCOMInfo ) )

    // get rid of event handle

    CloseHandle(os.hEvent);

    // clear information in structure (kind of a "we're done flag")
    ExitThread(0); //HH

    return (TRUE);

} // end of CommWatchProc()
Example #4
0
DWORD WINAPI CommMonitor( LPSTR lpData)
{
	CommData
	SYSTEMTIME SystemTime;
	int	 i,nItemCount,nPackLen=0;
	BYTE  PackFun ;
	BOOL	OddFlag=FALSE;
	LPSTR lpPointer,lptemp1,lptemp2,hRXBuffer=(LPSTR)RxBuffer;
	char *p_ACK1="\x10\x31";
	char *p_ACK0="\x10\x30";

	Init()
	ResetOpPointer()				

	
	ResetRxPointer()


 	nItemCount =0;
	FillDevName()					
	SetCommWaitMask(EV_RXFLAG)
	PurgeComm( hComm, PURGE_RXCLEAR );	
	while (TRUE)
	{



		WaitEvent(EV_RXFLAG)


		GetInQueueLength(dwReadLength)
		ReadCommBlock(dwReadLength)
 

 

		lptemp1=strbchrnb(lpRxPointer,'[',1,(int)dwReadLength+5);
		PackFun= *(lptemp1+c_FuncNumOff);
		{
			case '0':	
			case 'D':
				ResetRxPointer()		
				ResetOpPointer()
				continue;
			case '1':	
				if (lptemp1!=lpOpPointer)	
				{
					for (i=0;*hRXBuffer!='[';hRXBuffer++,i++); 
					memmove(lpOpPointer ,hRXBuffer , (int)(lpRxPointer-hRXBuffer));
					lpRxPointer=lpRxPointer-i;
					hRXBuffer=(LPSTR)RxBuffer;
				}
				continue;
			case '5':	

				if ((*lpOpPointer)!='[')
				{
					for (i=0;*hRXBuffer!='[';hRXBuffer++,i++); 
					memmove(lpOpPointer ,hRXBuffer , (int)(lpRxPointer-hRXBuffer));
					lpRxPointer=lpRxPointer-i;
					hRXBuffer=(LPSTR)RxBuffer;
				continue;
			case '9':	
				
				break;			

			default :	

				continue;

		}


		wSampleID =(int)StrToInt(lpOpPointer+7,5);
		GetLocalTime(&SystemTime);
				

		lpPointer=strchrnb(lpOpPointer+1,'[',2,70);				
		while(*(lpPointer+2)=='2')	
		{

			FillSampleID(nItemCount, wSampleID)	

			lptemp1=strchrnb(lpPointer,',',1,4)+1;
			lptemp2=strchrnb(lptemp1,',',1,5);
			FillItemName(nItemCount, lptemp1,(lptemp2-lptemp1))	

			lptemp1=strchrnb(lptemp2+1,',',2,20)+1;
			for (;*lptemp1==' ';lptemp1++);
			lptemp2=strchrnb(lptemp1,',',1,10);
			FillResult(nItemCount,lptemp1,(int)(lptemp2-lptemp1))

			FillDate(nItemCount, SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wHour)

			lpPointer=strchrnb(lptemp2+1,'[',1,40);				
			nItemCount++;
		}	


		if(nItemCount>0)
		{
			(*lpResultProcessRoutine)(lpDevice->nDevNO, OutResult, nItemCount);
			lpDevice->dwRecordCount+=nItemCount;
		}

		
		nItemCount=0;	
//		nPackLen=0;
		ResetRxPointer()		
		ResetOpPointer()
		hRXBuffer=(LPSTR)RxBuffer;

	}
	return TRUE;
} // end of CommWatchProc()

void WINAPI BeforeSetCommState(DCB *pComDcb)
{
	pComDcb->EvtChar ='[';
}