⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c_comthread.cpp

📁 对日本KSG震动探测仪器编写的伴侣服务代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				}
				else{
					ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat );
					if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo )){
						// wsprintf( szError, "<CE-%u>", dwErrorFlags );
						// WriteTTYBlock( hWnd, szError, lstrlen( szError ) );
					}
					break;
				}
			}
			dwBytesSent += dwBytesWritten;
			if (dwBytesSent != dwBytesToWrite){
				wsprintf(szError,"\nProbable Write Timeout: Total of %ld bytes sent", dwBytesSent);
			}
			else{
				wsprintf(szError,"\n%ld bytes written", dwBytesSent);
			}
			OutputDebugString(szError);
		}
		else{
			// some other error occurred
			ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat );
			if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo )){
				// wsprintf( szError, "<CE-%u>", dwErrorFlags );
				// WriteTTYBlock( hWnd, szError, lstrlen( szError ) );
			}
			return ( FALSE );
		}
	}
	return ( TRUE );
}


/*=====================================
						m:16-Jan-1999
@p	<< Get Modem Status >>
=====================================*/
int		modem_stt(int ch)
{
	NPTTYINFO	npTTYInfo;
	LPDWORD lpModemStat;
	int a;

	npTTYInfo = &pTTYInfo_WRK[ch];
	lpModemStat = &Rcw[ch].ModemS;

	a = GetCommModemStatus(COMDEV( npTTYInfo ), lpModemStat);
	if (Rcw[ch].ModemS & MS_CTS_ON){
		CmsBit.CS |= Atv_dat[ch];				// CS 怣崋 on
	}
	else{
		CmsBit.CS &= Msk_dat[ch];				// CS 怣崋 off
	}

	if (Rcw[ch].ModemS & MS_RLSD_ON){
		CmsBit.CD |= Atv_dat[ch];				// CD 怣崋 on
	}
	else{
		CmsBit.CD &= Msk_dat[ch];				// CD 怣崋 off
	}
	return (a);
}

//************************************************************************
//  Description:
//     A secondary thread that will watch for COMM events.
//
//  Parameters:
//     LPSTR lpData
//        32-bit pointer argument
//
//  Win-32 Porting Issues:
//     - Added this thread to watch the communications device and
//       post notifications to the associated window.
//
//************************************************************************
DWORD FAR PASCAL CommWatchProc( LPSTR lpData )
{
	DWORD		dwEvtMask;
	NPTTYINFO	npTTYInfo = (NPTTYINFO) lpData;
	OVERLAPPED	os;
	int			nLength;
	BYTE		abIn[ MAXBLOCK + 1];
	char c;
	int a, ch, port, bat_f;

	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!", "TTY Error!",
					MB_ICONEXCLAMATION | MB_OK );
		return ( FALSE );
	}

	if (!SetCommMask( COMDEV( npTTYInfo ), EV_RXCHAR )){
		return ( FALSE );
	}

	ch = CHANNEL( npTTYInfo );
	port = PORT( npTTYInfo );

//@@@@@@@@@@@@@@@@@@@@@@@@
//	int a; for DEBUG 
/*
	LPDWORD lpModemStat;
	ulong dat;

	lpModemStat = &dat;

	dat = a = 999;
	a = GetCommModemStatus(COMDEV( npTTYInfo ), lpModemStat);
	a = 5;
*/
//@@@@@@@@@@@@@@@@@@@@@@@@@  



	while ( CONNECTED( npTTYInfo ) ){
		dwEvtMask = 0;
		WaitCommEvent( COMDEV( npTTYInfo ), &dwEvtMask, NULL );
		// 庴怣僀儀儞僩
		if (dwEvtMask & EV_RXCHAR){			// Any Character received
			do{
				nLength = ReadCommBlock(ch, (LPSTR) abIn, MAXBLOCK );

				bat_f = 0;
				for (a=0;a<nLength;a++){
					c = abIn[a];
					Rcw[ch].Buffer[Rcw[ch].WrtPoi ++] = c;
					Rcw[ch].WrtPoi &= RSB_MAX;
					Rcw[ch].RcvLen ++;

					if (Rcw[ch].DelEnb){		//
						switch (Rcw[ch].DelPhs){		//
						case 0:		// CR 慜
							if (c == C_CR){
								Rcw[ch].DelPhs = 1;
							}
							break;
						case 1:		// LF 慜
							// ver 1.30
							//if (c == C_LF){
							if (Rcw[ch].RcvLen == 3 && c == C_LF){
								bat_f = 1;
							}
							// no break;
						default:
							Rcw[ch].DelPhs = 0;
							break;
						}
					}

					if (Rcw[ch].RcvLen > BF_256-1){
						bat_f = 1;
					}

					if (bat_f){
						bat_f = 0;
						Rcw[ch].DelPhs = 0;
						Rcw[ch].RcvLen = 0;
						Rcw[ch].RcvCnt ++;
					}
				}
			}while (nLength > 0);
			a = 0;
		}

		// 偦偺懠偺僀儀儞僩傕僠僃僢僋
		if (dwEvtMask != EV_RXCHAR){		// 庴怣埲奜傕俷俶
			if (dwEvtMask & EV_TXEMPTY){	// Transmitt Queue Empty
				a = 1;
			}
			else if (dwEvtMask & EV_CTS){		// CTS changed state
				a = 2;
			}
			else if (dwEvtMask & EV_DSR){		// DSR changed state
				a = 3;
			}
			else if (dwEvtMask & EV_RLSD){		// RLSD changed state
				a = 4;
			}
			else if (dwEvtMask & EV_BREAK){		// BREAK received
				a = 5;
			}
			else if (dwEvtMask & EV_ERR){		// Line status error occurred
				a = 6;
			}
			else if (dwEvtMask & EV_RING){		// Ring signal detected
				a = 7;
			}
			else if (dwEvtMask & EV_PERR){		// Printer error occured
				a = 8;
			}
			else if (dwEvtMask & EV_RX80FULL){	// Receive buffer is 80 percent full
				a = 9;
			}
			else if (dwEvtMask & EV_EVENT1){	// Provider specific event 1
				a = 10;
			}
			else if (dwEvtMask & EV_EVENT2){	// Provider specific event 2
				a = 11;
			}
			a = 0;
		}
	}

	// get rid of event handle
	CloseHandle( os.hEvent );

	// clear information in structure (kind of a "we're done flag")
	THREADID( npTTYInfo ) = 0;
	HTHREAD( npTTYInfo ) = NULL;

	return( TRUE );

}

//---------------------------------------------------------------------------
//  Description:
//     Reads a block from the COM port and stuffs it into
//     the provided buffer.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//     LPSTR lpszBlock
//        block used for storage
//
//     int nMaxLength
//        max length of block to read
//
//  Win-32 Porting Issues:
//     - ReadComm() has been replaced by ReadFile() in Win-32.
//     - Overlapped I/O has been implemented.
//---------------------------------------------------------------------------
int NEAR ReadCommBlock(int ch, LPSTR lpszBlock, int nMaxLength )
{
	BOOL		fReadStat;
	COMSTAT		ComStat;
	DWORD		dwErrorFlags;
	DWORD		dwLength;
	DWORD		dwError;
	NPTTYINFO	npTTYInfo;

	npTTYInfo = &pTTYInfo_WRK[ch];

	// only try to read number of bytes in queue
	ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat );
	dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue );

	if (dwLength > 0){
		fReadStat = ReadFile( COMDEV( npTTYInfo ), lpszBlock,
							  dwLength, &dwLength, &READ_OS( npTTYInfo ) );
		if (!fReadStat){
			if (GetLastError() == ERROR_IO_PENDING){
				OutputDebugString("\n\rIO Pending");
				// We have to wait for read to complete.
				// This function will timeout according to the
				// CommTimeOuts.ReadTotalTimeoutConstant variable
				// Every time it times out, check for port errors
				while (!GetOverlappedResult( COMDEV( npTTYInfo ),
											 &READ_OS( npTTYInfo ), &dwLength, TRUE )){

					dwError = GetLastError();
					if (dwError == ERROR_IO_INCOMPLETE){	// normal result if not finished
						continue;
					}
					else{
						ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat );
						//if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ));
						break;
					}
				}
			}
			else{
				// some other error occurred
				dwLength = 0;
				ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat );
				//if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ));
			}
		}
	}
	return ( dwLength );
}

//---------------------------------------------------------------------------
//  Description:
//     Closes the connection to the port.  Resets the connect flag
//     in the TTYINFO struct.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//  Win-32 Porting Issues:
//     - Needed to stop secondary thread.  SetCommMask() will signal the
//       WaitCommEvent() event and the thread will halt when the
//       CONNECTED() flag is clear.
//     - Use new PurgeComm() API to clear communications driver before
//       closing device.
//---------------------------------------------------------------------------
BOOL NEAR CloseConnection(int ch)
{
	NPTTYINFO  npTTYInfo;

	npTTYInfo = &pTTYInfo_WRK[ch];

	// set connected flag to FALSE
	CONNECTED( npTTYInfo ) = FALSE ;

	// disable event notification and wait for thread
	// to halt
	SetCommMask( COMDEV( npTTYInfo ), 0 );

	// block until thread has been halted
	while(THREADID(npTTYInfo) != 0);

	// drop DTR
	EscapeCommFunction( COMDEV( npTTYInfo ), CLRDTR );

	// purge any outstanding reads/writes and close device handle
	PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT |
									PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
	CloseHandle( COMDEV( npTTYInfo ) );

	return ( TRUE );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -