#include "stdafx.h"
#include "Comm.h"
#include "Serial.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;
SerialPort ite;
CString ss;
char lpBuffer[1024]; 
CStringList Data;
HANDLE m_hComm; /* 串口操作句柄 */
HANDLE hCommThread; //全局变量,串口线程
UINT SerialPort1ThreadProcess(HWND hSendWnd);
OVERLAPPED osWrite;
OVERLAPPED osRead; using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
    ite.OpenPort();
DWORD threadID;
    hCommThread=CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,
(LPTHREAD_START_ROUTINE)SerialPort1ThreadProcess,
NULL, 0, &threadID);
//::SetThreadPriority(hCommThread,THREAD_PRIORITY_BELOW_NORMAL);
if (hCommThread == NULL)

::printf("创建串口1处理线程失败"); 
::PostQuitMessage(0); 

// initialize MFC and print and error on failure

// TODO: code your application's behavior here.
// CString strHello;
// strHello.LoadString(IDS_HELLO);
// cout << (LPCTSTR)strHello << endl;

while(1)
{
           Sleep(1);
//    int l=0;
//    l=ss.GetLength();
//    printf("l==========================%d\n",l);
//    ss.Format("%s",lpBuffer);
//    Data.AddHead(ss);    Data.AddHead("@00RD0000000157*\r");
   while (!Data.IsEmpty())
   {
  printf("发送数据send!\n");
   CString str;
   str=Data.GetHead();
 //             cout <<(LPCTSTR)str << endl;   Sleep(50);      ite.OnSend(str);
              Data.RemoveAll();
   }
   printf("主循环!\n");
   
        } return nRetCode;
}UINT SerialPort1ThreadProcess(HWND hSendWnd)
{
while (1)
{
DWORD dwEvtMask=0; //读取的字节数
LPOVERLAPPED os;
memset(&os,0,sizeof(os)); memset(&osRead,0,sizeof(OVERLAPPED)); 

    osRead.Offset=0;   
    osRead.OffsetHigh=0;   
    osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);  SetCommMask(m_hComm,EV_RXCHAR|EV_TXEMPTY);//有哪些串口事件需要监视?    WaitCommEvent(m_hComm,&dwEvtMask,os);//等待串口通信事件的发生

if ((dwEvtMask & EV_RXCHAR)==EV_RXCHAR)
{
   COMSTAT ComStat;
           DWORD dwLength;
   DWORD dwErrorFlags;
           DWORD dwError;
   DWORD dwBytesRead=0;
  
   ClearCommError(m_hComm,&dwErrorFlags,&ComStat);//此句子可以用来确定收到的字节数     dwLength=ComStat.cbInQue; //上一句算出收到的字节数之后赋给dwLength
        if (dwLength>=10)//也许是去掉噪声
   {
    BOOL fReadStat; 
fReadStat=ReadFile(m_hComm,lpBuffer,dwLength,&dwBytesRead,&osRead);

if (!fReadStat)
{
 if (GetLastError()==ERROR_IO_PENDING)
 {  
 while(!GetOverlappedResult(m_hComm,&osRead,&dwBytesRead,TRUE))
 {
 dwError=GetLastError();   
                             if(dwError==ERROR_IO_INCOMPLETE)   
 continue;   
 else
                             {
 //wsprintf(dwError, "<CE-%u>", dwError ) ;
                                 ClearCommError(m_hComm,&dwErrorFlags,&ComStat);
                                 break;
 }   }
 }

         else
{
                      dwLength=0;
                      ClearCommError(m_hComm, &dwErrorFlags, &ComStat );
}
} printf("dwLength==========================%d\n",dwLength);
ss.Format("%s",lpBuffer); if(ss=="@00RD00123452*\r")

{
printf("返回成功receive!\n");

// cout << (LPCTSTR)ss << endl;
  }
   } }
printf("进程循环中\n");


}
return TRUE;

解决方案 »

  1.   

    这明显是不断申请内存而且有完没被释放,这么改试试:
    while (!Data.IsEmpty())
    {Data.AddHead("@00RD0000000157*\r");printf("发送数据send!\n");
    CString str;
    str=Data.GetHead();
     // cout <<(LPCTSTR)str << endl;Sleep(50);   ite.OnSend(str);
      Data.RemoveAll();
    }
    就是把Data.AddHead("@00RD0000000157*\r");这条语句放在while (!Data.IsEmpty())里,因为这条语句是不断在执行,只有Data不为空时才被释放
      

  2.   

    最后的Data.RemoveAll();不知是不是将内存释放的,我想实现不断的向串口发送那些数据,
    另一个问题是,收到的数据会从8个字符之后从新发送一次 ,而且错误,不知为什么?