码迷,mamicode.com
首页 > 其他好文 > 详细

串口通信中数据的分段接受

时间:2015-11-16 17:08:04      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

一、在对话框的OnInitDialog中创建接受数据的文件,并启动AutoSaveData线程

 1 CTime t = CTime::GetTickCount();
 2 CString str = t.Format(_T("%Y-%m-%d"));
 3 if(!m_rFile.Open(m_strCurPath+_T("\\")+str+_T(".txt"),CFile::modeCreate | CFile::modeWrite |CFile::modeNoTruncate)) 
 4 {
 5     AfxMessageBox( "创建记录文件失败!");
 6     m_brFileOpen = FALSE;
 7 }else{
 8     m_brFileOpen = TRUE;
 9 }
10 
11 m_Thread = AfxBeginThread(AutoSaveData,this);

 

二、线程方法实现如下

 1 UINT CSCOMMDlg::AutoSaveData(LPVOID pParam)
 2 {
 3     CSCOMMDlg* pDlg = (CSCOMMDlg*)pParam;
 4 
 5     CString str;
 6     int currentPoint = 0;
 7     while(!pDlg->m_bExitThread)
 8     {
 9         if(pDlg->sourceUnChar[0])
10         {
11             CTime t = CTime::GetCurrentTime();
12             CString sTime = t.Format("%Y-%m-%d %H:%M:%S ");
13             str = sTime + str;
14             bool check = TRUE;
15             while(check)
16             {
17                 check = FALSE;
18                 while(pDlg->sourceUnChar[currentPoint])
19                 {
20                     check = TRUE;
21                     str += pDlg->sourceUnChar[currentPoint];
22                     currentPoint++;
23                 }
24                 Sleep(20);
25             }
26             currentPoint = 0;
27             pDlg->m_countPackage = 0;
28             memset(pDlg->sourceUnChar,0,BUFFERE_LEN*sizeof(char));
29             str += _T("\r\n");
30             if (pDlg->m_brFileOpen)
31             {
32                 pDlg->m_rFile.SeekToEnd();
33                 pDlg->m_rFile.Write(str,str.GetLength());
34                 pDlg->m_rFile.Flush();
35             }else{
36                 CTime t = CTime::GetTickCount();
37                 CString str = t.Format(_T("%Y-%m-%d"));
38                 if(!pDlg->m_rFile.Open(pDlg->m_strCurPath+_T("\\")+str+_T(".txt"),CFile::modeCreate | CFile::modeWrite |CFile::modeNoTruncate)) 
39                 {
40                     AfxMessageBox( "创建记录文件失败!");
41                     pDlg->m_brFileOpen = FALSE;
42                 }else{
43                     pDlg->m_brFileOpen = TRUE;
44                 }
45             }
46             str.Empty();
47         }
48     }
49     return 0;
50 }

 

串口通信中数据的分段接受

标签:

原文地址:http://www.cnblogs.com/Arvin-XZL/p/4969138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!