串口通信发送数据的问题
部分代码如下所示,发送的数据来自编辑框中的输入,即m_send 变量。问题在于
无论我发什么,接收端收到的都是发送的数据加上同样个byte的无效数据。
请指教。看看哪里的问题呢?
void CDllDialog::OnSend() { // TODO: Add your control notification handler code here COMSTAT ComStat; ULONG nLength=0; UpdateData(true); ULONG szLength = m_send.GetLength(); char *sz = new char[szLength]; if(sz==NULL) return; CString tempstr; tempstr = ChangeCharstr2Hexstr(m_send); memcpy(sz,tempstr.GetBuffer(szLength),szLength); DWORD dwErrorFlags; ClearCommError(h_com,&dwErrorFlags,&ComStat); BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite); if(!fState) { ULONG my_error=GetLastError(); if(my_error==ERROR_IO_PENDING) { GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE); } else nLength=0; } delete[] sz; sz=NULL; }//工作者线程//用于判断串口是否接收到数据,如果有数据,就发信息给WM_COMMNOTIFY// 工作者线程,负责监视串行口//UINT CommProc(LPVOID pParam){ OVERLAPPED os; DWORD dwMask, dwTrans; COMSTAT ComStat; DWORD dwErrorFlags; CDllDialog *pX1=(CDllDialog*)pParam; memset(&os, 0, sizeof(OVERLAPPED)); os.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL); if(os.hEvent==NULL) { AfxMessageBox("Can't create event object!"); return (UINT)-1; } while(pX1->m_bConnected) { ClearCommError(pX1->h_com,&dwErrorFlags,&ComStat); if(ComStat.cbInQue) { // 无限等待WM_COMMNOTIFY消息被处理完 WaitForSingleObject(pX1->m_hPostMsgEvent, INFINITE); //重置事件 ResetEvent(pX1->m_hPostMsgEvent); // 通知!!! PostMessage(pX1->GetSafeHwnd(),WM_COMMNOTIFY, EV_RXCHAR, 0); //pX1->m_hTermWnd continue; } dwMask=0; if(!WaitCommEvent(pX1->h_com, &dwMask, &os)) // 重叠操作 { if(GetLastError()==ERROR_IO_PENDING) // 无限等待重叠操作结果 GetOverlappedResult(pX1->h_com, &os, &dwTrans, TRUE); else { CloseHandle(os.hEvent); return (UINT)-1; } } } CloseHandle(os.hEvent); return 0;}//串口消息函数LRESULT CDllDialog::OnCommNotify(WPARAM wParam, LPARAM lParam){ if( (!m_bConnected) || (wParam&EV_RXCHAR)!=EV_RXCHAR) { SetEvent(m_hPostMsgEvent); //允许允许发送下一个信息 return 0L; } int nLength; char buf[4096/4]; CString str; nLength=ReadComm(buf,100); //获取数据和长度 if(nLength) { for(int i=0;i<nLength/2;i++) str+=DevideHexChar(buf[i]); str+=_T(""); m_receive.ReplaceSel(str); } SetEvent(m_hPostMsgEvent); //允许发送下一个信息 return 0L;}//读取串口数据,返回长度。//输入参数:要读的长度//注意事项:输入的要读的长度如果比实际缓冲里面的数据量大的话,返回的是较小的值DWORD CDllDialog::ReadComm(char *buf, DWORD dwLength){ DWORD length=0; COMSTAT ComStat; DWORD dwErrorFlags; ClearCommError(h_com,&dwErrorFlags,&ComStat); length=min(dwLength, ComStat.cbInQue); ReadFile(h_com,buf,length,&length,&m_osRead); return length;}CString CDllDialog::ChangeCharstr2Hexstr(CString Charstr){ CString Hexstr=_T(""); Charstr.MakeUpper(); HexStringFilter(Charstr); int Length=Charstr.GetLength(); if(Length%2) Charstr.Delete(Length-1); Length=Charstr.GetLength(); for(int i=0;i<Length/2;i++) { Hexstr+=CombineHexChar(Charstr.GetAt(i*2),Charstr.GetAt(i*2+1)); } return Hexstr;}void CDllDialog::HexStringFilter(CString &str){ BOOL bOK; for(int i=0;i<str.GetLength();) { bOK=((str.GetAt(i)>='0')&&(str.GetAt(i)<='9'))|| ((str.GetAt(i)>='A')&&(str.GetAt(i)<='F'))|| ((str.GetAt(i)>='a')&&(str.GetAt(i)<='f')); if(!bOK) str.Delete(i); else i++; }}char CDllDialog::CombineHexChar(char CharH, char CharL){ char result; CString temp; if(CharH>='0'&&CharH<='9') result=(CharH-'0'); else if(CharH>='a'&&CharH<='f') result=(CharH-'a'+10); else if(CharH>='A'&&CharH<='F') result=(CharH-'A'+10); else result=0; result<<=4; if(CharL>='0'&&CharL<='9') result+=(CharL-'0'); else if(CharL>='a'&&CharL<='f') result+=(CharL-'a'+10); else if(CharL>='A'&&CharL<='F') result+=(CharL-'A'+10); else result+=0; return result;}CString CDllDialog::DevideHexChar(char HexChar){ CString result=_T(""); int temp=(HexChar&0xF0)>>4; if(temp<10) result+=(temp+'0'); else result+=(temp+'A'-10); temp=HexChar&0x0F; if(temp<10) result+=(temp+'0'); else result+=(temp+'A'-10); return result;}