Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2012786
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2015-10-17 19:41:27


点击(此处)折叠或打开

  1. // vc_netGraphDlg.cpp : implementation file
  2. //

  3. #include "stdafx.h"
  4. #include "vc_netGraph.h"
  5. #include "vc_netGraphDlg.h"
  6. #include "math.h"
  7. #include <windows.h>

  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif



  13. CRITICAL_SECTION g_cs;
  14. CString strComPort;
  15. CString strComBaud;
  16. BOOL bComIsopen=FALSE;
  17. float xstart=0;

  18. /////////////////////////////////////////////////////////////////////////////
  19. // CAboutDlg dialog used for App About

  20. class CAboutDlg : public CDialog
  21. {
  22. public:
  23.     CAboutDlg();

  24. // Dialog Data
  25.     //{{AFX_DATA(CAboutDlg)
  26.     enum { IDD = IDD_ABOUTBOX };
  27.     //}}AFX_DATA

  28.     // ClassWizard generated virtual function overrides
  29.     //{{AFX_VIRTUAL(CAboutDlg)
  30.     protected:
  31.     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  32.     //}}AFX_VIRTUAL

  33. // Implementation
  34. protected:
  35.     //{{AFX_MSG(CAboutDlg)
  36.     //}}AFX_MSG
  37.     DECLARE_MESSAGE_MAP()
  38. };

  39. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  40. {
  41.     //{{AFX_DATA_INIT(CAboutDlg)
  42.     //}}AFX_DATA_INIT
  43. }

  44. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  45. {
  46.     CDialog::DoDataExchange(pDX);
  47.     //{{AFX_DATA_MAP(CAboutDlg)
  48.     //}}AFX_DATA_MAP
  49. }

  50. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  51.     //{{AFX_MSG_MAP(CAboutDlg)
  52.         // No message handlers
  53.     //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()

  55. /////////////////////////////////////////////////////////////////////////////
  56. // CVc_netGraphDlg dialog

  57. CVc_netGraphDlg::CVc_netGraphDlg(CWnd* pParent /*=NULL*/)
  58.     : CDialog(CVc_netGraphDlg::IDD, pParent)
  59. {
  60.     //{{AFX_DATA_INIT(CVc_netGraphDlg)
  61.     //}}AFX_DATA_INIT
  62.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  63.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  64. }

  65. void CVc_netGraphDlg::DoDataExchange(CDataExchange* pDX)
  66. {
  67.     CDialog::DoDataExchange(pDX);
  68.     //{{AFX_DATA_MAP(CVc_netGraphDlg)
  69.     DDX_Control(pDX, IDC_NTGRAPHCTRL1, m_Graph);
  70.     //}}AFX_DATA_MAP
  71. }

  72. BEGIN_MESSAGE_MAP(CVc_netGraphDlg, CDialog)
  73.     //{{AFX_MSG_MAP(CVc_netGraphDlg)
  74.     ON_WM_SYSCOMMAND()
  75.     ON_WM_PAINT()
  76.     ON_WM_QUERYDRAGICON()
  77.     ON_BN_CLICKED(IDC_BUTTON_COMOPEN, OnButtonComopen)
  78.     ON_MESSAGE(WM_USERMSG,OnMsg)
  79.     //}}AFX_MSG_MAP
  80. END_MESSAGE_MAP()

  81. /////////////////////////////////////////////////////////////////////////////
  82. // CVc_netGraphDlg message handlers

  83. BOOL CVc_netGraphDlg::OnInitDialog()
  84. {
  85.     CDialog::OnInitDialog();

  86.     // Add "About..." menu item to system menu.

  87.     // IDM_ABOUTBOX must be in the system command range.
  88.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  89.     ASSERT(IDM_ABOUTBOX < 0xF000);

  90.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  91.     if (pSysMenu != NULL)
  92.     {
  93.         CString strAboutMenu;
  94.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  95.         if (!strAboutMenu.IsEmpty())
  96.         {
  97.             pSysMenu->AppendMenu(MF_SEPARATOR);
  98.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  99.         }
  100.     }
  101.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(0,"COM1");
  102.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(1,"COM2");
  103.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(2,"COM3");
  104.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(3,"COM4");
  105.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(4,"COM5");
  106.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(5,"COM6");
  107.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(6,"COM7");
  108.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(7,"COM8");
  109.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->InsertString(8,"COM9");
  110.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->SetCurSel(3);
  111. //    ((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT))->SetCurSel(0);

  112.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD))->InsertString(0,"2400");
  113.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD))->InsertString(1,"4800");
  114.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD))->InsertString(2,"9600");
  115.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD))->InsertString(3,"19200");
  116.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD))->InsertString(4,"38400");
  117.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD))->InsertString(5,"115200");
  118.     ((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD))->SetCurSel(5);

  119. #define RED        RGB(0XCD,0X33,0X33)
  120. #define YELLOW        RGB(0X8B,0X00,0X00)
  121. #define PURPLE        RGB(0X4B,0X00,0X82)
  122. #define BLACK        RGB(0X2B,0X2B,0X2B)
  123. #define     BLUE RGB(0X00,0X00,0XEE)
  124. #define     ZS            RGB(0X8B,0X45,0X13)
  125.     m_Graph.ClearGraph();

  126.     m_Graph.SetElementLineColor(RED);
  127.     m_Graph.SetElementWidth(2);
  128.     m_Graph.AddElement();
  129.     m_Graph.SetElementLineColor(YELLOW);
  130.     m_Graph.SetElementWidth(2);
  131.     m_Graph.AddElement();
  132.     m_Graph.SetElementLineColor(PURPLE);
  133.     m_Graph.SetElementWidth(2);
  134.     m_Graph.AddElement();
  135.     m_Graph.SetElementLineColor(BLACK);
  136.     m_Graph.SetElementWidth(2);
  137.     m_Graph.AddElement();
  138.     m_Graph.SetElementLineColor(BLUE);
  139.     m_Graph.SetElementWidth(2);
  140.     m_Graph.AddElement();
  141.     m_Graph.SetElementLineColor(ZS);
  142.     m_Graph.SetElementWidth(2);

  143.     m_Graph.SetElementIdentify(FALSE);
  144.     m_Graph.SetShowGrid(TRUE);
  145.     m_Graph.SetXGridNumber(10);
  146.     m_Graph.SetYGridNumber(10);



  147.     //m_Graph.SetCaption("超好用的图形控件");
  148.     m_Graph.SetXLabel("X轴");
  149.     m_Graph.SetYLabel("Y轴");

  150.     m_Graph.SetRange (0,500,-360,360);

  151.     m_Graph.SetPlotAreaColor(0x006400);//0x006400
  152.     bComIsopen=FALSE;
  153.     SetIcon(m_hIcon, TRUE);            // Set big icon
  154.     SetIcon(m_hIcon, FALSE);        // Set small icon
  155.     
  156.     InitializeCriticalSection(&m_cs);
  157.     // TODO: Add extra initialization here
  158.     
  159.     return TRUE; // return TRUE unless you set the focus to a control
  160. }


  161. void CVc_netGraphDlg::OnMsg()
  162. {
  163.     static DWORD dwcnt=0;
  164.     dwcnt++;
  165.     CString str;
  166.     str.Format("%d",dwcnt);
  167.     GetDlgItem(IDC_STATIC_MSG)->SetWindowText(str);
  168. }

  169. void CVc_netGraphDlg::NetGraph_Init()
  170. {
  171.         m_Graph.ClearGraph();

  172.     m_Graph.SetElementLineColor(RED);
  173.     m_Graph.SetElementWidth(2);
  174.     m_Graph.AddElement();
  175.     m_Graph.SetElementLineColor(YELLOW);
  176.     m_Graph.SetElementWidth(2);
  177.     m_Graph.AddElement();
  178.     m_Graph.SetElementLineColor(PURPLE);
  179.     m_Graph.SetElementWidth(2);
  180.     m_Graph.AddElement();
  181.     m_Graph.SetElementLineColor(BLACK);
  182.     m_Graph.SetElementWidth(2);
  183.     m_Graph.AddElement();
  184.     m_Graph.SetElementLineColor(BLUE);
  185.     m_Graph.SetElementWidth(2);
  186.     m_Graph.AddElement();
  187.     m_Graph.SetElementLineColor(ZS);
  188.     m_Graph.SetElementWidth(2);

  189.     m_Graph.SetElementIdentify(FALSE);
  190.     m_Graph.SetShowGrid(TRUE);
  191.     m_Graph.SetXGridNumber(10);
  192.     m_Graph.SetYGridNumber(10);



  193.     //m_Graph.SetCaption("超好用的图形控件");
  194.     m_Graph.SetXLabel("X轴");
  195.     m_Graph.SetYLabel("Y轴");

  196.     m_Graph.SetRange (0,500,-360,360);

  197.     m_Graph.SetPlotAreaColor(0x006400);//0x006400    
  198. }
  199. void CVc_netGraphDlg::OnSysCommand(UINT nID, LPARAM lParam)
  200. {
  201.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  202.     {
  203.         CAboutDlg dlgAbout;
  204.         dlgAbout.DoModal();
  205.     }
  206.     else
  207.     {
  208.         CDialog::OnSysCommand(nID, lParam);
  209.     }
  210. }

  211. // If you add a minimize button to your dialog, you will need the code below
  212. // to draw the icon. For MFC applications using the document/view model,
  213. // this is automatically done for you by the framework.

  214. void CVc_netGraphDlg::OnPaint()
  215. {
  216.     if (IsIconic())
  217.     {
  218.         CPaintDC dc(this); // device context for painting

  219.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  220.         // Center icon in client rectangle
  221.         int cxIcon = GetSystemMetrics(SM_CXICON);
  222.         int cyIcon = GetSystemMetrics(SM_CYICON);
  223.         CRect rect;
  224.         GetClientRect(&rect);
  225.         int x = (rect.Width() - cxIcon + 1) / 2;
  226.         int y = (rect.Height() - cyIcon + 1) / 2;

  227.         // Draw the icon
  228.         dc.DrawIcon(x, y, m_hIcon);
  229.     }
  230.     else
  231.     {
  232.         CDialog::OnPaint();
  233.     }
  234. }
  235. WORD getCRC16(volatile BYTE *ptr,BYTE len)
  236. {
  237.     unsigned char i;
  238.     unsigned short crc = 0xFFFF;
  239.     if(len==0)
  240.     {
  241.         len = 1;
  242.     }
  243.     while(len--)
  244.     {
  245.         crc ^= *ptr;
  246.         for(i=0; i<8; i++)
  247.         {
  248.             if(crc&1)
  249.             {
  250.                 crc >>= 1;
  251.                 crc ^= 0xA001;
  252.             }
  253.             else
  254.             {
  255.                 crc >>= 1;
  256.             }
  257.         }
  258.         ptr++;
  259.     }
  260.     return(crc);
  261. }
  262. // The system calls this to obtain the cursor to display while the user drags
  263. // the minimized window.
  264. HCURSOR CVc_netGraphDlg::OnQueryDragIcon()
  265. {
  266.     return (HCURSOR) m_hIcon;
  267. }
  268. int EnumAllCommPort(CString* pStr)
  269. {
  270.         int i = 0;
  271.         CHAR Name[25];
  272.         UCHAR szPortName[25];
  273.         LONG Status;
  274.         DWORD dwIndex = 0;
  275.         DWORD dwName;
  276.         DWORD dwSizeofPortName;
  277.         DWORD Type;
  278.         HKEY hKey;
  279.         LPCTSTR data_Set = "HARDWARE\\DEVICEMAP\\SERIALCOMM\\";
  280.         long ret0 = (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, &hKey));

  281.         do
  282.         {
  283.                 dwName = sizeof(Name);
  284.                 dwSizeofPortName = sizeof(szPortName);
  285.                 Status = RegEnumValue(hKey, dwIndex++, Name, &dwName, NULL, &Type,
  286.                         szPortName, &dwSizeofPortName);

  287.                 if ((Status == ERROR_SUCCESS) || (Status == ERROR_MORE_DATA))
  288.                 {
  289.                         pStr[i] = CString(szPortName); // 串口字符串保存
  290.                         i++;// 串口计数
  291.                 }
  292.         }
  293.         while ((Status == ERROR_SUCCESS) || (Status == ERROR_MORE_DATA));

  294.         RegCloseKey(hKey);
  295.         return i;
  296. }
  297. UINT CVc_netGraphDlg::ThreadComReceive(void *param)
  298. {

  299.     CVc_netGraphDlg *dlg=(CVc_netGraphDlg *)param;
  300.     CListBox *list=(CListBox *)dlg->GetDlgItem(IDC_LIST_COMRX);

  301.     DWORD dwComRxLength;
  302.     COMSTAT ComStat;
  303.     DWORD dwErrFlags;
  304.     CString sTmp,sComRecv;
  305.     BYTE lpbuf[1000];
  306.     static DWORD dwLoop=0;
  307.     memset(lpbuf,0,1000);
  308.     if (dlg->m_hcom!=NULL)
  309.     {
  310.         PurgeComm(dlg->m_hcom,PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
  311.     }
  312.     while (TRUE)
  313.     {
  314.         Sleep(1);
  315.         //EnterCriticalSection(&dlg->m_cs);
  316.         ClearCommError(dlg->m_hcom,&dwErrFlags,&ComStat);
  317.         //LeaveCriticalSection(&dlg->m_cs);

  318.         if (ComStat.cbInQue==0)
  319.         {
  320.             //beak out when byte have been read
  321.             continue;
  322.         }
  323.         dwComRxLength=min(ComStat.cbInQue,210);
  324.         ReadFile(dlg->m_hcom,lpbuf,dwComRxLength,&dwComRxLength,NULL);
  325.         sComRecv="";
  326.         sTmp="";
  327.         if (dwComRxLength>=21)
  328.         {
  329.             for (dwLoop=0;dwLoop<dwComRxLength;dwLoop++)
  330.             {
  331.                 sTmp.Format("%02x ",lpbuf[dwLoop]);
  332.                 sComRecv+=sTmp;

  333.             }
  334.             WORD wCrcCalc=getCRC16(lpbuf,19);
  335.             WORD wCrcGet=(lpbuf[20]<<8) | (lpbuf[19]);
  336.             if (wCrcGet!=wCrcCalc)
  337.             {
  338.                 continue;
  339.             }
  340.             static short sAccelLatch;
  341.         
  342.             dlg->m_GyroX= (lpbuf[7]<<8) | (lpbuf[8]);
  343.             dlg->m_GyroY= (lpbuf[9]<<8) | (lpbuf[10]);
  344.             dlg->m_GyroZ= (lpbuf[11]<<8) | (lpbuf[12]);
  345.             dlg->m_AccelX=(lpbuf[13]<<8) | (lpbuf[14]);
  346.             dlg->m_AccelY=(lpbuf[15]<<8) | (lpbuf[16]);
  347.             dlg->m_AccelZ=(lpbuf[17]<<8) | (lpbuf[18]);
  348.             CString strTmp;
  349.             //显示x轴角度
  350.             strTmp.Format("%d",dlg->m_AccelX);
  351.             dlg->GetDlgItem(IDC_EDIT_ACCELX)->SetWindowText(strTmp);
  352.             strTmp.Format("%d",dlg->m_AccelY);
  353.             dlg->GetDlgItem(IDC_EDIT_ACCELY)->SetWindowText(strTmp);
  354.             strTmp.Format("%d",dlg->m_AccelZ);
  355.             dlg->GetDlgItem(IDC_EDIT_ACCELZ)->SetWindowText(strTmp);
  356.             strTmp.Format("%d",dlg->m_GyroX);
  357.             dlg->GetDlgItem(IDC_EDIT_GYROX)->SetWindowText(strTmp);
  358.             strTmp.Format("%d",dlg->m_GyroY);
  359.             dlg->GetDlgItem(IDC_EDIT_GYROY)->SetWindowText(strTmp);
  360.             strTmp.Format("%d",dlg->m_GyroZ);
  361.             dlg->GetDlgItem(IDC_EDIT_GYROZ)->SetWindowText(strTmp);
  362.             if(xstart>2000)
  363.             {
  364.                 xstart=0;
  365.                 dlg->m_Graph.ClearGraph();
  366.                 dlg->m_Graph.SetElementLineColor(RED);
  367.                 dlg->m_Graph.AddElement();
  368.                 dlg->m_Graph.SetElementLineColor(YELLOW);
  369.                 dlg->m_Graph.AddElement();
  370.                 dlg->m_Graph.SetElementLineColor(PURPLE);
  371.                 dlg->m_Graph.AddElement();
  372.                 dlg->m_Graph.SetElementLineColor(BLACK);
  373.                 dlg->m_Graph.AddElement();
  374.                 dlg->m_Graph.SetElementLineColor(BLUE);
  375.                 dlg->m_Graph.AddElement();
  376.                 dlg->m_Graph.SetElementLineColor(ZS);

  377.                 dlg->m_Graph.SetRange(0,500,-360,360);
  378.         
  379.             }

  380.             //向主线程发送消息
  381.             ::SendMessage(dlg->GetSafeHwnd(),WM_USERMSG,0,0);
  382.             //::SendMessage()
  383.             if (xstart>500)
  384.             {
  385.                 dlg->m_Graph.SetRange(xstart-500,xstart,-360,360);
  386.             }
  387.             double fHBFilter = dlg->m_AccelX + (((sAccelLatch-dlg->m_AccelX)*0.9 + dlg->m_GyroY)*0.01);
  388.             sAccelLatch=dlg->m_AccelX;
  389.     
  390.             if (((CButton *)dlg->GetDlgItem(IDC_CHECK_ACCELX))->GetCheck())
  391.             {
  392.                 dlg->m_Graph.PlotXY(xstart, dlg->m_AccelX,0);
  393.             }
  394.             if (((CButton *)dlg->GetDlgItem(IDC_CHECK_ACCELY))->GetCheck())
  395.             {
  396.                 dlg->m_Graph.PlotXY(xstart, dlg->m_AccelY,1);
  397.             }
  398.             if (((CButton *)dlg->GetDlgItem(IDC_CHECK_ACCELZ))->GetCheck())
  399.             {
  400.                 //dlg->m_Graph.PlotXY(x, dlg->m_AccelZ,2);
  401.                 dlg->m_Graph.PlotXY(xstart, fHBFilter,2);
  402.             }
  403.             if (((CButton *)dlg->GetDlgItem(IDC_CHECK_GYROX))->GetCheck())
  404.             {
  405.                 dlg->m_Graph.PlotXY(xstart, dlg->m_GyroX,3);
  406.             }
  407.             if (((CButton *)dlg->GetDlgItem(IDC_CHECK_GYROY))->GetCheck())
  408.             {
  409.                 dlg->m_Graph.PlotXY(xstart, dlg->m_GyroY,4);
  410.             }
  411.             if (((CButton *)dlg->GetDlgItem(IDC_CHECK_GYROZ))->GetCheck())
  412.             {
  413.                 dlg->m_Graph.PlotXY(xstart, dlg->m_GyroZ,5);
  414.             }
  415.             //dlg->m_Graph.PlotXY(x, fHBFilter,2);
  416.         //    EnterCriticalSection(&g_cs);
  417.         //    LeaveCriticalSection(&g_cs);
  418.             xstart=xstart+2;
  419.             
  420.             
  421.         
  422.         
  423.         
  424.             //在编辑框显示当前接收到的数据帧
  425.             dlg->GetDlgItem(IDC_EDIT_COMRX)->SetWindowText(sComRecv);
  426.             //将当前接收到的数据帧添加到列表框中
  427.             list->InsertString(list->GetCount(),sComRecv);

  428.             if(list->GetCount()>=200)
  429.                 list->ResetContent();

  430.             list->SetCurSel(list->GetCount()-1);

  431.             
  432.         }
  433.         
  434.     
  435.     }
  436.     /*
  437.     CProgressCtrl *prg=(CProgressCtrl *)param;

  438.     
  439.     for (int x=0;x<10;x++)
  440.     {
  441.         prg->StepIt();
  442.         Sleep(500);
  443.     }
  444.     */
  445.     return TRUE;
  446. }

  447. void CVc_netGraphDlg::OnButtonComopen()
  448. {
  449.     // TODO: Add your control notification handler code here
  450.     int i=0;
  451.     char szTmp[200];
  452.     CString sComPort,sComBaud;
  453.     CComboBox *box1=((CComboBox *)GetDlgItem(IDC_COMBO_COMPORT));
  454.     box1->GetLBText(box1->GetCurSel(),sComPort);
  455.     CComboBox *box2=((CComboBox *)GetDlgItem(IDC_COMBO_COMBAUD));
  456.     box2->GetLBText(box2->GetCurSel(),sComBaud);
  457. ///    ((CEdit *)GetDlgItem(IDC_EDIT_TESTBAUD))->SetWindowText(strComBaud);
  458.     if (bComIsopen==FALSE)
  459.     {
  460.         bComIsopen=TRUE;
  461.         m_hcom=CreateFile(sComPort,GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  462.         if (m_hcom==(HANDLE)-1)
  463.         {
  464.             m_hcom=NULL;
  465.             AfxMessageBox("串口无效或者被占用");
  466.             return;    
  467.         }
  468.         DWORD dwComPort,dwComBaud;
  469.         strcpy(szTmp,sComPort);
  470.         i=atoi(szTmp+3);
  471.         dwComPort=(DWORD)i;
  472.     
  473.         strcpy(szTmp,sComBaud);
  474.         i=atoi(szTmp);
  475.         dwComBaud=(DWORD)i;

  476.         GetCommState(m_hcom,&dcb);
  477.         dcb.BaudRate=dwComBaud;
  478.         dcb.ByteSize=8;
  479.         dcb.Parity=NOPARITY;//NOPARITY EVENPARITY
  480.         dcb.StopBits=ONESTOPBIT;
  481.         SetCommState(m_hcom,&dcb);
  482.         SetupComm(m_hcom,1024,1024);
  483.         dwComtimeouts.ReadIntervalTimeout=200;
  484.         dwComtimeouts.ReadTotalTimeoutConstant=100;
  485.         dwComtimeouts.ReadTotalTimeoutMultiplier=5000;
  486.         dwComtimeouts.WriteTotalTimeoutConstant=1000;
  487.         dwComtimeouts.WriteTotalTimeoutMultiplier=5000;
  488.         SetCommTimeouts(m_hcom,&dwComtimeouts);
  489.         PurgeComm(m_hcom,PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
  490.         //PurgeComm(m_hcom,PURGE_RXCLEAR | PURGE_TXCLEAR);
  491.         xstart=0;
  492.         NetGraph_Init();
  493.         AfxBeginThread(ThreadComReceive,this);
  494.         GetDlgItem(IDC_BUTTON_COMOPEN)->SetWindowText("断开");
  495.     
  496.     }
  497.     else
  498.     {
  499.     //    EnterCriticalSection(&m_cs);//临界区锁定共享资源
  500.         //    m_Graph.ClearGraph();
  501.         bComIsopen=FALSE;
  502.         ////SetEvent(m_hShutdownEvent);
  503.         GetDlgItem(IDC_BUTTON_COMOPEN)->SetWindowText("打开");
  504.         TerminateThread(ThreadComReceive,0);//关闭线程
  505.         if (m_hcom!=NULL)
  506.         {
  507.             CloseHandle(m_hcom);
  508.             m_hcom=NULL;
  509.         }    
  510.         
  511.     //    LeaveCriticalSection(&m_cs);//解锁
  512.     }
  513.     
  514. }


阅读(1308) | 评论(0) | 转发(0) |
0

上一篇:tcp客户端

下一篇:wincap 之发送接收解析

给主人留下些什么吧!~~