|
-
May 5th, 2004, 10:19 PM
#1
Serial Communications problems
hComm = CreateFile( "com2",
GENERIC_READ ,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE)
{
MessageBox("fail");
return false;
}
SetupComm(hComm,1024,512);
PurgeComm(hComm,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
//COMMTIMEOUTS cto;
//cto.
DCB dcb;
GetCommState(hComm,&dcb);
dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
dcb.fBinary=true;
dcb.fParity=false;
SetCommState(hComm,&dcb);
OVERLAPPED osRead = {0};
DWORD dwReaded;
DWORD dwRes;
BOOL fRes;
osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osRead.hEvent == NULL)
{
return ;
}
char lpBuf[1024];
// Issue write.
if (!ReadFile(hComm, lpBuf, 1024, &dwReaded,&osRead))
{
if(ERROR_IO_PENDING!=GetLastError())
MessageBox("failread");
return;
}
dwRes = WaitForSingleObject(osRead.hEvent, INFINITE);
GetOverlappedResult(hComm, &osRead, &dwReaded, FALSE);
CloseHandle(osRead.hEvent);
MessageBox("succ");
I am sure there is no data arrived at the "com2",but this operation successfully completed,and the dwReaded=0,
when switch to com1,the result seems correct(it returns ERROR_IO_PENDING error),BTW the com2 had been used by another program before(already exit),why?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|