串口数据读不完整怎么继续读?
DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
while(TRUE)
{
DWORD dwLength=0;
COMSTAT ComStat;
DWORD dwErrorFlags=0;
DWORD SIZE;
unsigned char buff[1024],buff1[512];
memset(buff, 0, sizeof(buff));
memset(buff1, 0, sizeof(buff1));
Sleep(200);
ClearCommError(hcom,&dwErrorFlags,&ComStat);
if (ComStat.cbInQue == 0)
continue;
dwLength = ComStat.cbInQue ;
if(dwLength>0)
{
bReadStat= ReadFile(hcom,buff,dwLength,&SIZE,NULL);TRACE1("dwLength=%d",SIZE);
if(!bReadStat)
{
return FALSE;}
if(m_bDisplay)
{
CString temp;
if(dwLength==1)
{
int iLen = m_ctrlMyRichEdit.GetWindowTextLength();
m_ctrlMyRichEdit.SetSel( iLen, iLen );
m_ctrlMyRichEdit.ReplaceSel(_T("\r\n"));
SetTime();
temp.Format(_T("%02X "),buff[0]);
AppendText((BYTE*)temp.GetBuffer(0),20,RGB(255, 0,0));
}
else if(dwLength==22)
{
int iLen = m_ctrlMyRichEdit.GetWindowTextLength();
m_ctrlMyRichEdit.SetSel( iLen, iLen );
m_ctrlMyRichEdit.ReplaceSel(_T("\r\n"));
SetTime();
for(unsigned int i=0;i<22;i++)
{
temp.Format(_T("%02X "),buff[i]);
AppendText((BYTE*)temp.GetBuffer(0),20,RGB(255, 0,0));}
BuildData(buff);
}}
我现在是根据dwLength = ComStat.cbInQue来判断串口数据长度再输出,但是当数据分两次过来时就不能判断了,当数据第一次读不完时怎么继续读呢?另外两次长度如何累加?
------解决方案--------------------“按ComStat.cbInQue来的”
是 累加 这个 值 就是 上次长度。
注意及时 清零