高分求解,关于串口通信(win32 api)的问题

oilinsea 2004-03-05 04:15:08
为什么我的程序读串口的数据的时候,只能一次8字节的读取
我的程序:
//打开串口,串口设置,以及打开监视线程
int CVc_demoDlg::m_fnStartReceive()
{
DCB dcb;
COMMTIMEOUTS timeouts;

//打开串口
m_hCom = CreateFile( "COM2", //端口
GENERIC_READ | GENERIC_WRITE, // 允许读写
0, //share mode
NULL, //security attribute
OPEN_EXISTING, //打开方式
FILE_ATTRIBUTE_NORMAL, //准备异步通信
NULL );
m_pData.m_hComm = m_hCom;//准备把串口句柄传给线程

//设置串口参数
dcb.DCBlength = sizeof(DCB);
GetCommState(m_hCom,&dcb);
GetCommTimeouts(m_hCom,&timeouts);
timeouts.ReadIntervalTimeout = 0xFFFFFFFF;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 5000;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts(m_hCom,&timeouts);
dcb.BaudRate = 9600;
dcb.fParity = NOPARITY;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;

SetCommState(m_hCom,&dcb);
SetupComm(m_hCom,4096,4096);
SetCommMask(m_hCom,EV_RXCHAR);

//启动线程
m_pWatchThread = AfxBeginThread(CommWatchProc,
&m_pData,
THREAD_PRIORITY_BELOW_NORMAL,
0,
CREATE_SUSPENDED,
NULL);
m_pWatchThread->m_bAutoDelete = TRUE;
m_pWatchThread->ResumeThread();

return 0;
}
//监视线程
UINT CommWatchProc( LPVOID pParam )
{
DWORD dwEvent=0;
COMSTAT ComStat;
DWORD dwBytesRead,dwError;
char * pData;
CReceiveData * p = (CReceiveData *)pParam;

pData = p->m_pData;
while (g_fReceiveFlag)//g_fReceiveFlag全局变量,决定是否监视
{
if (p->m_hComm == NULL) return -1;
WaitCommEvent(p->m_hComm,&dwEvent,NULL);
if (dwEvent & EV_RXCHAR)
{
ClearCommError(p->m_hComm,&dwError,&ComStat);
dwBytesRead = ComStat.cbInQue;
if (dwBytesRead > 0)
{
if (!ReadFile(p->m_hComm,pData,dwBytesRead,&dwBytesRead,NULL))
{
dwBytesRead = 0;
}
if (dwBytesRead > 0)
{
SendMessage(p->m_hWnd,WM_NOTIFYRECEIVE,
dwBytesRead,(LONG)(char *)p->m_pData);
}
PurgeComm(p->m_hComm,PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR);
}
}
}
if (p->m_hComm) CloseHandle(p->m_hComm);
return 0;
}
//线程参数的类
class CReceiveData :public CObject
{
public:
HWND m_hWnd;//窗口句柄
HANDLE m_hComm;//打开的串口的句柄
char * m_pData;//接收数据的缓冲区,初始化的时候指向char * data[1000]
};

/////////
上面我的程序在下位程序通过串口一次性发送100字节的数据过来时,监视线程中的ClearCommError(p->m_hComm,&dwError,&ComStat);每次都置ComStat.cbInQue为8,在线程的循环内ReadFile要读多次才能把100字节读取完,怎么才能让监视线程在一次循环之内把我下位程序发送的100字节全部读取完?????
...全文
25 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复

还没有回复,快来抢沙发~

发帖
硬件/系统
创建于2007-09-28

2594

社区成员

VC/MFC 硬件/系统
申请成为版主
帖子事件
创建了帖子
2004-03-05 04:15
社区公告
暂无公告