// an work thread to monitor the _CTS and RX_CHAR
UINT CUload::CommWatchRead(LPVOID lpParam)
{ // return 0=OK 1=Error
CUload *pUload=(CUload*)lpParam;
OVERLAPPED os;
DWORD dwEventMask,dwTransfer;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);// attrb,Manual,init,name
if (os.hEvent==NULL)
{
AfxMessageBox("Can't create Event",MB_ICONSTOP);
return 1;// error
}
// set 2 events !
if (!SetCommMask(pUload->m_idComDev,EV_RXCHAR|EV_CTS)) return 1;
while (pUload->m_bConnected)
{ // break,only if m_bConnected=NULL
dwEventMask=0;
if(!WaitCommEvent(pUload->m_idComDev,&dwEventMask,&os))
{ // function fails
if (GetLastError()==ERROR_IO_PENDING)
{// TRUE=WaitForSingleObject() should be called inside
GetOverlappedResult(pUload->m_idComDev,&os,&dwTransfer,TRUE);
os.Offset+=dwTransfer;
}
}
if((dwEventMask & EV_RXCHAR)==EV_RXCHAR)
{ // some chats received.
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)1, // EV_RXCHAR
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
// if not at same time,use else if
if((dwEventMask & EV_CTS)==EV_CTS) { // CTS changed
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)0, // EV_CTS
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
}
CloseHandle(os.hEvent);
return 0;// OK
}
#6
非常感谢帮我解答疑惑
#7
// Events
//
线程可以监视 DSR:
#define EV_RXCHAR 0x0001 // Any Character received
#define EV_RXFLAG 0x0002 // Received certain character
#define EV_TXEMPTY 0x0004 // Transmitt Queue Empty
#define EV_CTS 0x0008 // CTS changed state
#define EV_DSR 0x0010 // DSR changed state #define EV_RLSD 0x0020 // RLSD changed state
#define EV_BREAK 0x0040 // BREAK received
#define EV_ERR 0x0080 // Line status error occurred
#define EV_RING 0x0100 // Ring signal detected
#define EV_PERR 0x0200 // Printer error occured
#define EV_RX80FULL 0x0400 // Receive buffer is 80 percent full
#define EV_EVENT1 0x0800 // Provider specific event 1
#define EV_EVENT2 0x1000 // Provider specific event 2
// an work thread to monitor the _CTS and RX_CHAR
UINT CUload::CommWatchRead(LPVOID lpParam)
{ // return 0=OK 1=Error
CUload *pUload=(CUload*)lpParam;
OVERLAPPED os;
DWORD dwEventMask,dwTransfer;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);// attrb,Manual,init,name
if (os.hEvent==NULL)
{
AfxMessageBox("Can't create Event",MB_ICONSTOP);
return 1;// error
}
// set 2 events !
if (!SetCommMask(pUload->m_idComDev,EV_RXCHAR|EV_CTS)) return 1;
while (pUload->m_bConnected)
{ // break,only if m_bConnected=NULL
dwEventMask=0;
if(!WaitCommEvent(pUload->m_idComDev,&dwEventMask,&os))
{ // function fails
if (GetLastError()==ERROR_IO_PENDING)
{// TRUE=WaitForSingleObject() should be called inside
GetOverlappedResult(pUload->m_idComDev,&os,&dwTransfer,TRUE);
os.Offset+=dwTransfer;
}
}
if((dwEventMask & EV_RXCHAR)==EV_RXCHAR)
{ // some chats received.
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)1, // EV_RXCHAR
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
// if not at same time,use else if
if((dwEventMask & EV_CTS)==EV_CTS) { // CTS changed
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)0, // EV_CTS
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
}
CloseHandle(os.hEvent);
return 0;// OK
}
#define EV_RXCHAR 0x0001 // Any Character received
#define EV_RXFLAG 0x0002 // Received certain character
#define EV_TXEMPTY 0x0004 // Transmitt Queue Empty
#define EV_CTS 0x0008 // CTS changed state
#define EV_DSR 0x0010 // DSR changed state #define EV_RLSD 0x0020 // RLSD changed state
#define EV_BREAK 0x0040 // BREAK received
#define EV_ERR 0x0080 // Line status error occurred
#define EV_RING 0x0100 // Ring signal detected
#define EV_PERR 0x0200 // Printer error occured
#define EV_RX80FULL 0x0400 // Receive buffer is 80 percent full
#define EV_EVENT1 0x0800 // Provider specific event 1
#define EV_EVENT2 0x1000 // Provider specific event 2
#8
专门一个线程:
// an work thread to monitor the _CTS and RX_CHAR
UINT CUload::CommWatchRead(LPVOID lpParam)
{ // return 0=OK 1=Error
CUload *pUload=(CUload*)lpParam;
OVERLAPPED os;
DWORD dwEventMask,dwTransfer;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);// attrb,Manual,init,name
if (os.hEvent==NULL)
{
AfxMessageBox("Can't create Event",MB_ICONSTOP);
return 1;// error
}
// set 2 events !
if (!SetCommMask(pUload->m_idComDev,EV_RXCHAR|EV_CTS)) return 1;
while (pUload->m_bConnected)
{ // break,only if m_bConnected=NULL
dwEventMask=0;
if(!WaitCommEvent(pUload->m_idComDev,&dwEventMask,&os))
{ // function fails
if (GetLastError()==ERROR_IO_PENDING)
{// TRUE=WaitForSingleObject() should be called inside
GetOverlappedResult(pUload->m_idComDev,&os,&dwTransfer,TRUE);
os.Offset+=dwTransfer;
}
}
if((dwEventMask & EV_RXCHAR)==EV_RXCHAR)
{ // some chats received.
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)1, // EV_RXCHAR
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
// if not at same time,use else if
if((dwEventMask & EV_CTS)==EV_CTS) { // CTS changed
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)0, // EV_CTS
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
}
CloseHandle(os.hEvent);
return 0;// OK
}
// an work thread to monitor the _CTS and RX_CHAR
UINT CUload::CommWatchRead(LPVOID lpParam)
{ // return 0=OK 1=Error
CUload *pUload=(CUload*)lpParam;
OVERLAPPED os;
DWORD dwEventMask,dwTransfer;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);// attrb,Manual,init,name
if (os.hEvent==NULL)
{
AfxMessageBox("Can't create Event",MB_ICONSTOP);
return 1;// error
}
// set 2 events !
if (!SetCommMask(pUload->m_idComDev,EV_RXCHAR|EV_CTS)) return 1;
while (pUload->m_bConnected)
{ // break,only if m_bConnected=NULL
dwEventMask=0;
if(!WaitCommEvent(pUload->m_idComDev,&dwEventMask,&os))
{ // function fails
if (GetLastError()==ERROR_IO_PENDING)
{// TRUE=WaitForSingleObject() should be called inside
GetOverlappedResult(pUload->m_idComDev,&os,&dwTransfer,TRUE);
os.Offset+=dwTransfer;
}
}
if((dwEventMask & EV_RXCHAR)==EV_RXCHAR)
{ // some chats received.
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)1, // EV_RXCHAR
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
// if not at same time,use else if
if((dwEventMask & EV_CTS)==EV_CTS) { // CTS changed
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)0, // EV_CTS
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
}
CloseHandle(os.hEvent);
return 0;// OK
}