这是我写的一个简单的串口程序,具有异步读写功能,在VC6.0上运行时,总是出现读错误,实在找不出原因了,来求教!
// wwww.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "stdio.h"
//#include "mscomm.h"
#define BUFFER_SIZE 1024
//#define MSG(szMsg) MessageBox (NULL, szMsg , TEXT("Error"), MB_OK)
HANDLE hCom;
OVERLAPPED m_OverlappedRead, m_OverlappedWrite;
BOOL OpenSerial(LPCTSTR lpszPortNum);
DWORD WriteDataSerial(LPCVOID pdata, DWORD len);
DWORD ReadDataSerial(TCHAR ReceiveBuf[BUFFER_SIZE]);
void StrToHex(char *str,char*strhex);
void CloseSerial(void);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
long LastError=ERROR_SUCCESS;
LastError=OpenSerial("COM1");
if(LastError!=TRUE)
{
MessageBox(NULL,TEXT("open failed"),TEXT("error"),MB_OK);
return LastError;
}
BYTE cmd[]="Hello world";
LastError=WriteDataSerial(cmd,11);
if(!LastError)
{
MessageBox(NULL,TEXT("write failed"),TEXT("error"),MB_OK);
return 0;
}
char ReceiveBuf[1024];
memset(ReceiveBuf,0,sizeof(ReceiveBuf));
LastError=ReadDataSerial(ReceiveBuf);
if(!LastError)
{
MessageBox(NULL,TEXT("ReadDataSerial:read data failed"),TEXT("Error"), MB_OK);
return 0;
}
CloseSerial();
return TRUE;
}
BOOL OpenSerial(LPCTSTR lpszPortNum)
{
DCB dcb;
BOOL bSuccess;
hCom = CreateFile( lpszPortNum,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hCom ==INVALID_HANDLE_VALUE)
{
MessageBox (NULL, TEXT("OpenSerial():CreateFile() failed, invalid handle value!"), TEXT("Error"), MB_OK);
return FALSE;
}
memset(&m_OverlappedRead,0,sizeof(OVERLAPPED));
memset(&m_OverlappedWrite,0,sizeof(OVERLAPPED));
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts(hCom,&CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.ReadTotalTimeoutConstant=0;
CommTimeOuts.WriteTotalTimeoutMultiplier=50;
CommTimeOuts.WriteTotalTimeoutConstant=2000;
if(!SetCommTimeouts(hCom,&CommTimeOuts))
{
MessageBox(NULL,TEXT("SetCommTimeOuts failed"),TEXT("error"),MB_OK);
return FALSE;
}
m_OverlappedRead. hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_OverlappedWrite. hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
GetCommState(hCom,&dcb);
dcb.BaudRate=9600;
dcb.ByteSize=1;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
SetCommState(hCom,&dcb);
/*if(!SetCommState(hCom,&dcb)||
!SetupComm(hCom,1000,1000)||
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(hCom);
return FALSE;
}*/
return TRUE;
}
DWORD WriteDataSerial(LPCVOID pdata, DWORD len)
{
DWORD dwBytesWritten;
DWORD result;
if(hCom==INVALID_HANDLE_VALUE ) return FALSE;
result=WriteFile(hCom,pdata,len,&dwBytesWritten,&m_OverlappedWrite);
if(!result)
{
if (GetLastError() == ERROR_IO_PENDING)
{
WaitForSingleObject(m_OverlappedWrite.hEvent,1000);
GetOverlappedResult(hCom,&m_OverlappedWrite,&dwBytesWritten,FALSE);
return dwBytesWritten;
}
else
{
MessageBox(NULL,TEXT("write continue"),TEXT("error"),MB_OK);
return 0;
}
}
}
DWORD ReadDataSerial(TCHAR ReceiveBuf[BUFFER_SIZE])
{
COMSTAT CommStat;
DWORD dwError;
DWORD dwBytesRead;
DWORD bReadStatus;
if(hCom==INVALID_HANDLE_VALUE) return FALSE;
ClearCommError(hCom,&dwError,&CommStat);
bReadStatus=ReadFile(hCom,ReceiveBuf,256,&dwBytesRead,&m_OverlappedRead);
if(!bReadStatus)
{
if(GetLastError()==ERROR_IO_PENDING)
{ WaitForSingleObject(m_OverlappedRead.hEvent,1000);
GetOverlappedResult(hCom,&m_OverlappedRead,&dwBytesRead,FALSE);
return dwBytesRead;
}
else
return 0;
}
return dwBytesRead;
}
void CloseSerial(void)
{
CloseHandle(hCom);
}