commDlg.cpp
上传用户:zhouf86
上传日期:2013-02-16
资源大小:244k
文件大小:12k
源码类别:

串口编程

开发平台:

Visual C++

  1. // commDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "comm.h"
  5. #include "commDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. int LineHight[122];
  12. int LHTemp[120];
  13. CString DTemp;
  14. int LineNum=0;
  15. int NextTime;
  16. int RightLeftNum=0;
  17. int check='n';
  18. int gogo=0;
  19. int speed=9600;
  20. int tab=0;
  21. CString FilePath="c:\data.txt";
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CAboutDlg dialog used for App About
  24. class CAboutDlg : public CDialog
  25. {
  26. public:
  27. CAboutDlg();
  28. // Dialog Data
  29. //{{AFX_DATA(CAboutDlg)
  30. enum { IDD = IDD_ABOUTBOX };
  31. //}}AFX_DATA
  32. // ClassWizard generated virtual function overrides
  33. //{{AFX_VIRTUAL(CAboutDlg)
  34. protected:
  35. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  36. //}}AFX_VIRTUAL
  37. // Implementation
  38. protected:
  39. //{{AFX_MSG(CAboutDlg)
  40. //}}AFX_MSG
  41. DECLARE_MESSAGE_MAP()
  42. };
  43. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  44. {
  45. //{{AFX_DATA_INIT(CAboutDlg)
  46. //}}AFX_DATA_INIT
  47. }
  48. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  49. {
  50. CDialog::DoDataExchange(pDX);
  51. //{{AFX_DATA_MAP(CAboutDlg)
  52. //}}AFX_DATA_MAP
  53. }
  54. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  55. //{{AFX_MSG_MAP(CAboutDlg)
  56. // No message handlers
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CCommDlg dialog
  61. CCommDlg::CCommDlg(CWnd* pParent /*=NULL*/)
  62. : CDialog(CCommDlg::IDD, pParent)
  63. {
  64. //{{AFX_DATA_INIT(CCommDlg)
  65. m_SendData = _T("");
  66. m_ReceiveData = _T("");
  67. //}}AFX_DATA_INIT
  68. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  69. stop=FALSE;
  70. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  71. }
  72. void CCommDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74. CDialog::DoDataExchange(pDX);
  75. //{{AFX_DATA_MAP(CCommDlg)
  76. DDX_Control(pDX, IDC_STOPRECV, m_stop);
  77. DDX_Control(pDX, IDC_COMSEEPED, m_speed);
  78. //DDX_Control(pDX, IDC_HEX, m_hex);
  79. DDX_Control(pDX, IDC_COMSELECT, m_com);
  80. DDX_Control(pDX, IDC_MSCOMM1, m_Comm);
  81. DDX_Text(pDX, IDC_EDIT2, m_ReceiveData);
  82. //}}AFX_DATA_MAP
  83. }
  84. BEGIN_MESSAGE_MAP(CCommDlg, CDialog)
  85. //{{AFX_MSG_MAP(CCommDlg)
  86. ON_WM_SYSCOMMAND()
  87. ON_WM_PAINT()
  88. ON_WM_QUERYDRAGICON()
  89. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  90. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  91. ON_CBN_SELENDOK(IDC_COMSELECT, OnComselect)
  92. ON_CBN_SELENDOK(IDC_COMSEEPED, OnComspeed)
  93. ON_BN_CLICKED(IDC_STOPRECV, OnStoprecv)
  94. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  95. ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  96. ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
  97. ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
  98. ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
  99. //}}AFX_MSG_MAP
  100. END_MESSAGE_MAP()
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CCommDlg message handlers
  103. BOOL CCommDlg::OnInitDialog()
  104. {
  105. CDialog::OnInitDialog();
  106. // Add "About..." menu item to system menu.
  107. // IDM_ABOUTBOX must be in the system command range.
  108. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  109. ASSERT(IDM_ABOUTBOX < 0xF000);
  110. CMenu* pSysMenu = GetSystemMenu(FALSE);
  111. if (pSysMenu != NULL)
  112. {
  113. CString strAboutMenu;
  114. strAboutMenu.LoadString(IDS_ABOUTBOX);
  115. if (!strAboutMenu.IsEmpty())
  116. {
  117. pSysMenu->AppendMenu(MF_SEPARATOR);
  118. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  119. }
  120. }
  121. // Set the icon for this dialog.  The framework does this automatically
  122. //  when the application's main window is not a dialog
  123. SetIcon(m_hIcon, TRUE); // Set big icon
  124. SetIcon(m_hIcon, FALSE); // Set small icon
  125. // TODO: Add extra initialization here
  126. m_com.SetCurSel(0);
  127. m_speed.SetCurSel(2);
  128. return TRUE;  // return TRUE  unless you set the focus to a control
  129. }
  130. void CCommDlg::OnSysCommand(UINT nID, LPARAM lParam)
  131. {
  132. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  133. {
  134. CAboutDlg dlgAbout;
  135. dlgAbout.DoModal();
  136. }
  137. else
  138. {
  139. CDialog::OnSysCommand(nID, lParam);
  140. }
  141. }
  142. // If you add a minimize button to your dialog, you will need the code below
  143. //  to draw the icon.  For MFC applications using the document/view model,
  144. //  this is automatically done for you by the framework.
  145. // 画图函数
  146. // 算法:运用LineTo函数把120个点连成折线
  147. void CCommDlg::OnPaint() 
  148. {
  149. int hoc;
  150. if (NextTime<600 && stop==FALSE) hoc = 600;
  151. else hoc = NextTime;
  152. CString TabStr="";
  153. CPaintDC dc(this);
  154. dc.GetNearestColor(RGB(255,0,0));
  155. dc.MoveTo(108,250);
  156. if (NextTime<=600 && stop==FALSE)
  157. for (int i=0;i<(NextTime/5);i++)
  158. {
  159. dc.LineTo(108+(5*i),250-LineHight[i]);
  160. }
  161. else
  162. for (int i=0;i<121;i++)
  163. {
  164. dc.LineTo(108+(5*i),250-LineHight[i]);
  165. }
  166. TabStr.Format("%d",hoc/5-120);
  167. SetDlgItemText(IDC_TAB1,TabStr);
  168. TabStr.Format("%d",hoc/5-100);
  169. SetDlgItemText(IDC_TAB2,TabStr);
  170. TabStr.Format("%d",hoc/5-80);
  171. SetDlgItemText(IDC_TAB3,TabStr);
  172. TabStr.Format("%d",hoc/5-60);
  173. SetDlgItemText(IDC_TAB4,TabStr);
  174. TabStr.Format("%d",hoc/5-40);
  175. SetDlgItemText(IDC_TAB5,TabStr);
  176. TabStr.Format("%d",hoc/5-20);
  177. SetDlgItemText(IDC_TAB6,TabStr);
  178. TabStr.Format("%d",hoc/5);
  179. SetDlgItemText(IDC_TAB7,TabStr);
  180. }
  181. // The system calls this to obtain the cursor to display while the user drags
  182. //  the minimized window.
  183. HCURSOR CCommDlg::OnQueryDragIcon()
  184. {
  185. return (HCURSOR) m_hIcon;
  186. }
  187. BEGIN_EVENTSINK_MAP(CCommDlg, CDialog)
  188.     //{{AFX_EVENTSINK_MAP(CCommDlg)
  189. ON_EVENT(CCommDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
  190. //}}AFX_EVENTSINK_MAP
  191. END_EVENTSINK_MAP()
  192. void CCommDlg::OnComm() 
  193. {
  194. if(stop)return;
  195. VARIANT m_input1;
  196. COleSafeArray m_input2;
  197. long length,i;
  198. BYTE data[600];
  199. CString str;
  200. if(m_Comm.GetCommEvent()==2)//接收缓冲区内有字符
  201. {
  202. m_input1=m_Comm.GetInput();//读取缓冲区内的数据
  203. m_input2=m_input1;//将VARIANT型变量转换为ColeSafeArray型变量
  204. length=m_input2.GetOneDimSize();//确定数据长度
  205. for(i=0;i<length;i++)
  206. m_input2.GetElement(&i,data+i);//将数据转换为BYTE型数组
  207. for(i=0;i<length;i++)//将数组转换为Cstring型变量
  208. {
  209. CFile file;
  210. CString tempstr;
  211. BYTE a=* (char *)(data+i);
  212. BYTE b=* (char *)(data+(++i));
  213. float c=(float)a+(float)(b%10)/10;
  214. str.Format("%2.1f ",c);//将a格式化
  215. if (str.GetLength()<5) str="0"+str;
  216. str = str.Right(5);
  217. m_ReceiveData+=str;//将刚采集的数加到编辑框中
  218. LineHight[121]=a;
  219. Display();//对数据显示前的处理
  220. //将数据保存到文件中
  221. if (m_ReceiveData.GetLength()>=600)//600个字符刚好对应120个数据
  222. {
  223. CStdioFile readfile; 
  224. CString readstr,readstr2;
  225. if(readfile.Open(FilePath,CFile::modeRead))
  226. {
  227. while(readfile.ReadString(readstr))//逐行查询,至到文件尾
  228. {
  229. readstr2+=readstr;
  230. }
  231. readfile.Close();
  232. }
  233. if (file.Open(FilePath,CFile::modeCreate|CFile::modeReadWrite))
  234. {
  235. file.Write(readstr2,readstr2.GetLength());//原来的数据
  236.   file.Write(m_ReceiveData,600);//写入新的120个数据
  237. file.Close();
  238. m_ReceiveData="";//清除编辑框内容
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. UpdateData(FALSE);//更新编辑框内容
  245. }
  246. //开串口程序
  247. void CCommDlg::OnButton1() 
  248. {
  249. if( !m_Comm.GetPortOpen())
  250. {
  251. m_Comm.SetPortOpen(TRUE);//打开串口
  252. SetDlgItemText(IDC_BUTTON1,"关闭串口");
  253. }
  254. else
  255. {
  256. m_Comm.SetPortOpen(FALSE);
  257. SetDlgItemText(IDC_BUTTON1,"打开串口");
  258. }
  259. }
  260. //清除编辑框内容程序
  261. void CCommDlg::OnButton2() 
  262. {
  263. m_ReceiveData.Empty();//清除接收对话框中的数据
  264. UpdateData(FALSE);
  265. }
  266. //选择那一个串口程序
  267. void CCommDlg::OnComselect() 
  268. {
  269. if(m_Comm.GetPortOpen())
  270. m_Comm.SetPortOpen(FALSE);
  271. m_Comm.SetCommPort(m_com.GetCurSel()+1);
  272. }
  273. //选择波特率
  274. void CCommDlg::OnComspeed() 
  275. {
  276. CString temp;
  277. int i=m_speed.GetCurSel();
  278. switch(i)
  279. {
  280. case 0:
  281. speed=2400;
  282. break;
  283. case 1:
  284. speed=4800;
  285. break;
  286. case 2:
  287. speed=9600;
  288. break;
  289. case 3:
  290. speed=19200;
  291. break;
  292. case 4:
  293. speed=38400;
  294. break;
  295. }
  296. temp.Format("%d,n,8,1",speed);
  297. m_Comm.SetSettings(temp);
  298. }
  299. //停止/继续程序
  300. void CCommDlg::OnStoprecv()
  301. {
  302. if (stop==TRUE)
  303. {
  304. NextTime=gogo;
  305. m_ReceiveData=DTemp;
  306. for (int i=0;i<120;i++) LineHight[i]=LHTemp[i];
  307. }
  308. stop=!stop;
  309. if (stop==TRUE) SetDlgItemText(IDC_STOPRECV,"继续显示");
  310. else  SetDlgItemText(IDC_STOPRECV,"停止显示");
  311. }
  312. //对数据显示前的处理:将新数年据加到左边,原来的右移一位
  313. void CCommDlg::Display()
  314. {
  315. if (NextTime <= 600 && stop==FALSE)
  316. {
  317. LineHight[NextTime/5] = LineHight[121];
  318. }
  319. else
  320. {
  321. for (int i=0;i<120;i++)
  322. {
  323. LineHight[i] = LineHight[i+1];
  324. }
  325. LineHight[120]=LineHight[121];
  326. }
  327. NextTime = NextTime+5;
  328. this->Invalidate();
  329. return;
  330. }
  331. //以下四个函数是查看原来数据的程序
  332. void CCommDlg::OnButton5() 
  333. {
  334. if (stop==FALSE)
  335. {
  336. gogo=NextTime;
  337. DTemp=m_ReceiveData;
  338. for (int i =0;i<120;i++) LHTemp[i]=LineHight[i];
  339. }
  340. stop=TRUE;//关闭接收数据,使显示区和数据区用来为显示原来数据用
  341. SetDlgItemText(IDC_STOPRECV,"继续显示");
  342. //先取出文件中的数据,放入一个数组中
  343. CStdioFile file;
  344. if(file.Open(FilePath,CFile::modeRead))
  345. {
  346. CString str;
  347. CString instr;
  348. CString outstr;
  349. CString sss;
  350. int i;
  351. while(file.ReadString(str))//逐行查询,至到文件尾
  352. {
  353. instr+=str;
  354.     }
  355. if (NextTime>instr.GetLength()-600) NextTime=instr.GetLength()-600;
  356. for (i=0;i<120;i++)//取出起点后的120个数据
  357. {
  358. sss=instr[NextTime];
  359. sss+=instr[NextTime+1];
  360. outstr+=sss+instr[NextTime+2]+instr[NextTime+3]+" ";
  361. LineHight[i]=atoi(sss);
  362. NextTime=NextTime+5;
  363. }
  364. m_ReceiveData=outstr;
  365. UpdateData(FALSE);//更新编辑框内容
  366. this->Invalidate();//更新图相
  367. }
  368. }
  369. void CCommDlg::OnButton4() 
  370. {
  371. if (stop==FALSE)
  372. {
  373. gogo=NextTime;
  374. DTemp=m_ReceiveData;
  375. for (int i =0;i<120;i++) LHTemp[i]=LineHight[i];
  376. }
  377. stop=TRUE;
  378. SetDlgItemText(IDC_STOPRECV,"继续显示");
  379. NextTime=NextTime-1200;//向后
  380. if (NextTime<0) NextTime=0;
  381. CStdioFile file;
  382. if(file.Open(FilePath,CFile::modeRead))
  383. {
  384. CString str;
  385. CString instr;
  386. CString outstr;
  387. CString sss;
  388. int i;
  389. while(file.ReadString(str))//逐行查询,至到文件尾
  390. {
  391. instr+=str;
  392.     }
  393. for (i=0;i<120;i++)
  394. {
  395. sss=instr[NextTime];
  396. sss+=instr[NextTime+1];
  397. outstr+=sss+instr[NextTime+2]+instr[NextTime+3]+" ";
  398. LineHight[i]=atoi(sss);
  399. NextTime=NextTime+5;
  400. }
  401. m_ReceiveData=outstr;
  402. UpdateData(FALSE);
  403. this->Invalidate();
  404. }
  405. }
  406. void CCommDlg::OnButton6() 
  407. {
  408. if (stop==FALSE)
  409. {
  410. gogo=NextTime;
  411. DTemp=m_ReceiveData;
  412. for (int i =0;i<120;i++) LHTemp[i]=LineHight[i];
  413. }
  414. stop=TRUE;
  415. SetDlgItemText(IDC_STOPRECV,"继续显示");
  416. NextTime=NextTime-605;//向后
  417. if (NextTime<0) NextTime=0;
  418. CStdioFile file;
  419. if(file.Open(FilePath,CFile::modeRead))
  420. {
  421. CString str;
  422. CString instr;
  423. CString outstr;
  424. CString sss;
  425. int i;
  426. while(file.ReadString(str))//逐行查询,至到文件尾
  427. {
  428. instr+=str;
  429.     }
  430. for (i=0;i<120;i++)
  431. {
  432. sss=instr[NextTime];
  433. sss+=instr[NextTime+1];
  434. outstr+=sss+instr[NextTime+2]+instr[NextTime+3]+" ";
  435. LineHight[i]=atoi(sss);
  436. NextTime=NextTime+5;
  437. }
  438. m_ReceiveData=outstr;
  439. UpdateData(FALSE);
  440. this->Invalidate();
  441. }
  442. }
  443. void CCommDlg::OnButton7() 
  444. {
  445. if (stop==FALSE)
  446. {
  447. gogo=NextTime;
  448. DTemp=m_ReceiveData;
  449. for (int i =0;i<120;i++) LHTemp[i]=LineHight[i];
  450. }
  451. stop=TRUE;//关闭接收数据,使显示区和数据区用来为显示原来数据用
  452. if (NextTime==0) NextTime=595;
  453. NextTime=NextTime-595;
  454. SetDlgItemText(IDC_STOPRECV,"继续显示");
  455. //先取出文件中的数据,放入一个数组中
  456. CStdioFile file;
  457. if(file.Open(FilePath,CFile::modeRead))
  458. {
  459. CString str;
  460. CString instr;
  461. CString outstr;
  462. CString sss;
  463. int i;
  464. while(file.ReadString(str))//逐行查询,至到文件尾
  465. {
  466. instr+=str;
  467.     }
  468. if (instr.GetLength()>=600 && NextTime>instr.GetLength()-600) NextTime=instr.GetLength()-600;
  469. for (i=0;i<120;i++)//取出起点后的120个数据
  470. {
  471. sss=instr[NextTime];
  472. sss+=instr[NextTime+1];
  473. outstr+=sss+instr[NextTime+2]+instr[NextTime+3]+" ";
  474. LineHight[i]=atoi(sss);
  475. NextTime=NextTime+5;
  476. }
  477. m_ReceiveData=outstr;
  478. UpdateData(FALSE);//更新编辑框内容
  479. this->Invalidate();//更新图相
  480. }
  481. }
  482. void CCommDlg::OnButton8() 
  483. {
  484. if (stop==TRUE)
  485. {
  486. m_ReceiveData=DTemp;
  487. NextTime=gogo;
  488. if (NextTime<600) NextTime=600;
  489. for (int i=0;i<120;i++) LineHight[i]=LHTemp[i];
  490. UpdateData(FALSE);//更新编辑框内容
  491. this->Invalidate();//更新图相
  492. }
  493. }