www.pudn.com > bluetoothtest.rar > BlueTooth.cpp


// BlueTooth.cpp: implementation of the BlueTooth class. 
#include "stdafx.h" 
#include "BlueTooth.h" 
#include "Afxwin.h" 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
HANDLE hComm;//串口句柄 
CString strInChar;//接收到的字符 
 
BlueTooth::BlueTooth() 
{ 
 
} 
 
BlueTooth::~BlueTooth() 
{ 
 
} 
//初始化 
BOOL BlueTooth::OnInitDialog() 
{ 
 
//波特率 
	BaudRates[0]=1200; 
	BaudRates[1]=2400; 
	BaudRates[2]=4800; 
	BaudRates[3]=9600; 
	BaudRates[4]=19200; 
	BaudRates[5]=38400; 
	BaudRates[6]=57600; 
	BaudRates[7]=115200; 
//串口号 
	PortIDs[0]="COM1:"; 
	PortIDs[1]="COM2:"; 
	PortIDs[2]="COM3:"; 
	PortIDs[3]="COM4:"; 
	PortIDs[4]="COM5:"; 
	PortIDs[5]="COM6:"; 
	PortIDs[6]="COM7:"; 
	PortIDs[7]="COM8:"; 
	PortIDs[8]="COM9:"; 
//初始化 
	PortNo=1; 
	BaudRate=3; 
	DataBits=2; 
	StopBits=1; 
	Parity=0; 
	hComm=INVALID_HANDLE_VALUE; 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
/*读端口线程 
ReadFile (hPort,                // Port handle 
            &Byte,                // Pointer to data to read 
            1,                    // Number of bytes to read 
            &dwBytesTransferred,  // Pointer to number of bytes read 
            NULL                  // Must be NULL for Windows CE 
*/ 
DWORD WINAPI ReadPortThread(LPVOID lpvoid) 
{ 
	BOOL fReadState; 
	DWORD dwLength; 
 
	 
	while(hComm!=INVALID_HANDLE_VALUE) 
	{ 
		unsigned char* buf=new unsigned char[512]; 
		fReadState=ReadFile(hComm,buf,512,&dwLength,NULL); 
		if(!fReadState) 
		{ 
			AfxMessageBox(CString("can't read data from com port!")); 
		} 
		else 
		{ 
		 
			if(dwLength!=0) 
			{ 
				strInChar=CString(buf).Left(dwLength); 
			    
			} 
		} 
		delete[] buf; 
	} 
	return 0; 
} 
//打开端口 
BOOL BlueTooth::OpenPort(CString PortID) 
{ 
	if(hComm==INVALID_HANDLE_VALUE) 
	{ 
		hComm=CreateFile(PortID,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0); 
		if(hComm==INVALID_HANDLE_VALUE ) 
		{ 
			AfxMessageBox(CString("can't open port!please check if used")); 
			return FALSE; 
		} 
		else 
		{ 
			GetCommState(hComm,&dcb); 
			dcb.BaudRate=BaudRates[BaudRate]; 
			dcb.ByteSize=DataBits+6; 
			dcb.Parity=Parity; 
			dcb.StopBits=StopBits; 
			dcb.fParity=FALSE; 
			dcb.fBinary=TRUE; 
			dcb.fDtrControl=0; 
			dcb.fRtsControl=0; 
			dcb.fOutX=dcb.fInX=dcb.fTXContinueOnXoff=0; 
			 
			SetCommMask(hComm,EV_RXCHAR);//EV_RXCHAR:A character was received and  
			                             //placed in the input buffer 
 
			SetupComm(hComm,16384,16384);//Specifies the recommended size, in bytes,  
			                             //of the device's internal input/output buffer.  
			if(!SetCommState(hComm,&dcb))//set com state by dcb 
			{ 
				AfxMessageBox(CString("can't configure port by current patameter,please checkout patameter")); 
				PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);//clear output&input buffer 
				CloseHandle(hComm); 
				hComm=INVALID_HANDLE_VALUE; 
				return FALSE; 
			} 
			else 
			{ 
				GetCommTimeouts(hComm,&CommTimeOuts);//retrieve time-out parameters 
				 
				CommTimeOuts.ReadIntervalTimeout=100;//maximum acceptable time 
				CommTimeOuts.ReadTotalTimeoutMultiplier=1; 
				CommTimeOuts.ReadTotalTimeoutConstant=100; 
				CommTimeOuts.WriteTotalTimeoutMultiplier=0; 
				CommTimeOuts.WriteTotalTimeoutConstant=0; 
 
				if(!SetCommTimeouts(hComm,&CommTimeOuts)) 
				{ 
					AfxMessageBox(CString("can't setting timeout parameter")); 
					PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR); 
					CloseHandle(hComm); 
					hComm=INVALID_HANDLE_VALUE; 
					return FALSE; 
				} 
				else 
				{ 
					PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR); 
 
					if(hReadThread=CreateThread(NULL,0,ReadPortThread,0,0,&dwThreadID)) 
					{ 
					}					 
					else 
					{ 
						AfxMessageBox(CString("can't create read COM port Thread")); 
					} 
					return TRUE; 
				} 
			} 
		} 
	} 
	else 
	{ 
		return FALSE; 
	} 
} 
//向端口写数据 
DWORD BlueTooth::WritePort(CString strsend) 
{ 
	BOOL fWriteState; 
	DWORD dwBytesWritten; 
	DWORD dwCharToWrite;//the length of characters to transmit 
 
	dwCharToWrite=(DWORD)strsend.GetLength(); 
	dwBytesWritten=0; 
	 
	if(hComm!=INVALID_HANDLE_VALUE&&dwCharToWrite!=0) 
	{ 
		unsigned char* buf=new unsigned char[dwCharToWrite]; 
		for(int i=0;i<(int)dwCharToWrite;i++) 
		{ 
			buf[i]=(unsigned char)strsend.GetAt(i);//GetAt(i):get a character specified by i 
		} 
 
		fWriteState=WriteFile(hComm,buf,dwCharToWrite*sizeof(unsigned char),&dwBytesWritten,NULL); 
		if(!fWriteState) 
		{ 
			AfxMessageBox(CString("can't write data to port")); 
		} 
		delete[] buf;		 
	} 
	return dwBytesWritten; 
} 
//关闭端口 
BOOL BlueTooth::ClosePort() 
{ 
	if(hComm!=INVALID_HANDLE_VALUE) 
	{ 
		SetCommMask(hComm,0); 
 
		if(hReadThread) 
		{ 
			TerminateThread(hReadThread,0); 
			CloseHandle(hReadThread); 
		} 
		PurgeComm(hComm,PURGE_TXCLEAR|PURGE_RXCLEAR); 
		CloseHandle(hComm); 
		hComm=INVALID_HANDLE_VALUE; 
		return TRUE; 
	} 
	else 
	{ 
		hComm=INVALID_HANDLE_VALUE; 
		return TRUE; 
	} 
} 
//接收字符 
void BlueTooth::OnRece()  
{ 
	 
		if(strInChar.GetLength()!=0) 
		{ 
			str_receive+=strInChar;//receive the characters save into "str_receive" 
			strInChar=_T(""); 
		} 
		else 
				AfxMessageBox(_T("no reve")); 
		 
} 
void BlueTooth::OnSend(CString strsend)  
{ 
	WritePort(strsend); 
}