www.pudn.com > IrMobile.zip > CSerial.cpp


// Serial.cpp: implementation of the CSerial class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "resource.h" 
#include "CSerial.h" 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CSerial::CSerial() 
{ 
	m_hIDComDev = NULL; 
	m_bOpened = FALSE; 
} 
 
CSerial::~CSerial() 
{ 
	PortClose(); 
} 
 
BOOL CSerial::Open( int nPort, int nBaud ) 
{ 
 
	if( m_bOpened ) return( TRUE ); 
 
	TCHAR szPort[15]; 
	DCB dcb; 
 
	wsprintf( szPort, _T("COM%d:"), nPort ); 
	m_hIDComDev = CreateFile( szPort, 
		GENERIC_READ | GENERIC_WRITE, 
		0, 
		NULL, 
		OPEN_EXISTING, 
		0, 
		NULL  
		); 
 
	if( m_hIDComDev == INVALID_HANDLE_VALUE) 
	{ 
		DWORD dwError=GetLastError(); 
		//::MessageBox(0,_T("打开端口出错!请确定端口设备的使用情况后关闭重试."),_T("ERROR!"),MB_OK ); 
		return( FALSE ); 
	} 
	else 
	{ 
		m_bOpened = TRUE; 
	} 
 
	// Change the CommTimeOuts structure settings. 
	COMMTIMEOUTS CommTimeOuts; 
	GetCommTimeouts (szPort, &CommTimeOuts); 
 
    CommTimeOuts.ReadIntervalTimeout = MAXDWORD;   
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0;   
    CommTimeOuts.ReadTotalTimeoutConstant = 0;     
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;   
    CommTimeOuts.WriteTotalTimeoutConstant = 0;     
 
	if(!SetCommTimeouts( m_hIDComDev, &CommTimeOuts )) 
	{ 
		//could not creatthe read thread; 
		 //::MessageBox (NULL,_T("不能设置延时参数"),_T("ERROR"),MB_OK); 
		 DWORD dwError=GetLastError(); 
		 return FALSE; 
	} 
 
	dcb.DCBlength = sizeof( DCB ); 
	GetCommState( m_hIDComDev, &dcb ); 
	dcb.BaudRate = nBaud; 
 
    dcb.fBinary = TRUE;               // Binary mode; no EOF check  
    dcb.fParity = TRUE;               // Enable parity checking  
    dcb.fOutxCtsFlow = FALSE;         // No CTS output flow control  
    dcb.fOutxDsrFlow = FALSE;         // No DSR output flow control  
    dcb.fDtrControl = DTR_CONTROL_ENABLE;  
                                      // DTR flow control type  
    dcb.fDsrSensitivity = FALSE;      // DSR sensitivity  
    dcb.fTXContinueOnXoff = TRUE;     // XOFF continues Tx  
    dcb.fOutX = FALSE;                // No XON/XOFF out flow control  
    dcb.fInX = TRUE;                 // No XON/XOFF in flow control  
    dcb.fErrorChar = FALSE;           // Disable error replacement  
    dcb.fNull = FALSE;                // Disable null stripping  
    dcb.fRtsControl = RTS_CONTROL_ENABLE;  
                                      // RTS flow control  
    dcb.fAbortOnError = FALSE;        // Do not abort reads/writes on  
                                      // error 
    dcb.ByteSize = 8;                 // Number of bits/byte, 4-8  
    dcb.Parity = 2;            // 0-4=no,odd,even,mark,space  
    dcb.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2  
 
 
	if( !SetCommState( m_hIDComDev, &dcb )|| 
	!SetupComm( m_hIDComDev, 1024, 1024 )/*|| 
										 m_OverlappedRead.hEvent == NULL || 
										 m_OverlappedWrite.hEvent == NULL*/ ) 
	{ 
		DWORD dwError = GetLastError(); 
		//if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent ); 
		//if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent ); 
		CloseHandle( m_hIDComDev ); 
		//::MessageBox(0,_T("初始化端口不正确!"),_T("ERROR!"),MB_OK ); 
		return( FALSE ); 
	} 
	 
	else 
	{ 
	} 
	 
	m_bOpened = TRUE; 
	 
	return( m_bOpened ); 
} 
 
//====================== 
BOOL CSerial::PortClose (void) 
{ 
	if (m_bOpened) 
	{ 
		// Close the communication port. 
		if (!CloseHandle (m_hIDComDev)) 
		{ 
			DWORD dwError = GetLastError (); 
			 
		} 
		else 
		{ 
			//::MessageBox (NULL,_T("正常退出,谢谢使用!"),_T("RIGHT"),MB_OK);   } 
		} 
	} 
	else 
	{ 
		return(FALSE);  
	} 
	return( TRUE );   
} 
 
BOOL CSerial::SendData( const char *buffer, int size ) 
{ 
	DWORD dwNumByteWritten; 
	if(!WriteFile(m_hIDComDev,buffer,size,&dwNumByteWritten,NULL)) 
	{ 
		DWORD dwError = GetLastError(); 
		return FALSE; 
	} 
	return TRUE; 
} 
 
int CSerial::ReadData( char *data ) 
{ 
	char Byte; 
	DWORD dwCommModemStatus,dwBytesTransferred; 
	int len = 0; 
	 
	SetCommMask (m_hIDComDev, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD ); 
	 
	if (m_hIDComDev!= INVALID_HANDLE_VALUE)  
	{ 
		WaitCommEvent (m_hIDComDev, &dwCommModemStatus, 0); 
		 
		 
		SetCommMask (m_hIDComDev, EV_RXCHAR | EV_CTS | EV_DSR ); 
		 
		if (dwCommModemStatus & EV_RXCHAR)  
		{ 
			// Loop for waiting for the data. 
			do  
			{ 
				// Read the data from the serial port. 
				ReadFile (m_hIDComDev, 
					&Byte,  
					1,  
					&dwBytesTransferred, 
					0 
					); 
				 
				// Display the data read. 
				if (dwBytesTransferred == 1) 
				{ 
					data[len] = Byte; 
					data[len+1] = 0; 
					len++; 
				} 
			} 
			while (dwBytesTransferred == 1); 
		}    
	} 
	return len; 
}