我的串口通讯线程
BOOL CMainFrame::BeginMonitorPort()
{
// HANDLE HPorthandle;
// if (CMyAppThread:: m_hShutdownEvent != NULL)
// ResetEvent(CMyAppThread::m_hShutdownEvent);
// CMyAppThread::m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// HPorthandle=m_Port1.m_hComm;
if(m_pFindThread)
{
AfxMessageBox("已经开始监控!");
return true;
::WaitForSingleObject(m_pFindThread->m_hThread,INFINITE);
delete m_pFindThread;
m_pFindThread=NULL;
}
//重要的地方开始了
bContinue=1;
if(m_pFindThread=AfxBeginThread(CMyAppThread::ReadComThread,(LPVOID)m_hComm1,THREAD_PRIORITY_ABOVE_NORMAL,
0,CREATE_SUSPENDED,NULL))
{
// AfxMessageBox("create thread success!");
m_pFindThread->m_bAutoDelete=FALSE;
m_pFindThread->ResumeThread();
return TRUE;
}
return FALSE;
}
void CMainFrame::OnQuickconnect()
{
// TODO: Add your control notification handler code here
CString sPort;
CString sPaud;
CString szFtpInfo;
char * buffer = NULL;
m_wndMyDialogBar.GetDlgItemText(IDC_PORT,sPort);
m_wndMyDialogBar.GetDlgItemText(IDC_PAUD,sPaud);
if (m_bConnected )
{
AfxMessageBox("串口已经连接。");
return;
};
sPort="COM"+sPort;
char *ch = sPort.GetBuffer(sPort.GetLength() + 1);
sPort.ReleaseBuffer();
// buffer=szFtpInfo;
if(DrfCommOpen(&m_hComm1,ch) )
{ AfxMessageBox("初始化串口失败");
buffer="初始化串口失败";
m_bConnected=FALSE;}
else {
buffer="串口已经连接成功";
m_bConnected=TRUE;};
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)buffer);
// m_bConnected=TRUE;
int iBaud;
// long lBaud;
//设置读写器的波特率
switch (GetBaud()){
case 9600 : {iBaud=0;break;}
case 19200 :{iBaud=1;break;}
case 38400: {iBaud=2;break;}
case 57600: {iBaud=3;break;}
case 115200: {iBaud=4;break;}
default:{iBaud=5;break;}
}
if (DrfSetBaudRate(m_hComm1,iBaud,0xff)) {
AfxMessageBox("设置波特率失败");
buffer="设置波特率失败";}
else buffer="设置波特率成功";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)buffer);
szFtpInfo="connect to COM";
szFtpInfo+=sPort;
szFtpInfo+=" sucess!";
}
UINT CMyAppThread::ReadComThread(LPVOID lParam)
{
HANDLE pCOM=lParam;
char * pInfo;
unsigned int Cnt,i,j;
// TagIds* pRfid;
CString str,IDInfo,sInfo;
LPTSTR pStr;
// TagIds* pRfid=GlobalAlloc(TagIds); //new (TagIds);
TagIds* pRfid=new (TagIds);
TagIds* pRfid1=new (TagIds);
if (bContinue){
ClearIDBuffer (pCOM,0xff) ;
pInfo="读串口线程启动中。。。。。。";
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)pInfo);}
else return 0L;
/*
HINSTANCE hdll=::AfxLoadLibrary("DrfApiV10.dll");
typedef BOOL (_stdcall *lpDrfGen2MultiTagIdentify)(HANDLE hCom, unsigned int * Count,TagIds *value, unsigned char ReaderAddr);
lpDrfGen2MultiTagIdentify DrfGen2MultiTagIdentify1;
DrfGen2MultiTagIdentify1=(lpDrfGen2MultiTagIdentify)::GetProcAddress(hdll,"DrfGen2MultiTagIdentify");
*/
//delete pRfid1;
int thetest;
thetest=0;
while(bContinue)
{
thetest++;
// DrfGen2MultiTagIdentify (pCOM,&Cnt,pRfid,0Xff);
if(DrfGen2MultiTagIdentify(pCOM,&Cnt,pRfid,0Xff)==0 )
{
for(i = 0; i < Cnt; i++) {
for(j = 0; j < 12; j++) {
str.Format("%2x",pRfid[i].Ids[j]);
IDInfo+=str;
}
IDInfo.MakeUpper();
int nLen=IDInfo.GetLength();
pStr=IDInfo.GetBuffer(nLen+2);
pStr[nLen]=13;
pStr[nLen+1]=10;
IDInfo.ReleaseBuffer();
}
pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)IDInfo);
};
// i=0;
}//while bContinue
// if(pRfid) pRfid=NULL;
// AfxMessageBox("串口未连接inner the thread。");
// ClearIDBuffer (pCOM,0xff) ;
// AfxMessageBox("ClearIDBuffer ");
// ::Sleep(1000);
delete pRfid1;
////////////////////////////////
// ::AfxFreeLibrary(hdll);
////////////////////////////////
// GlobalFree(pRfid);
return 0L;
}