一套基于模板匹配的语音识别技术。提取语音的特征,并建立模板库,可以将语音识别技术应用于机器人

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
  1. 视图类,废话少说,看看带注释的源码
  2. #include "stdafx.h"    
  3. #include "robot.h"    
  4. #include"Label.h"    
  5.    
  6. #include "robotDoc.h"    
  7. #include "robotView.h"    
  8.    
  9. #ifdef _DEBUG    
  10. #define new DEBUG_NEW    
  11. #undef THIS_FILE    
  12. static char THIS_FILE[] = __FILE__;   
  13. #endif    
  14.    
  15. /////////////////////////////////////////////////////////////////////////////    
  16. // CRobotView    
  17.    
  18. IMPLEMENT_DYNCREATE(CRobotView, CFormView)   
  19.    
  20. BEGIN_MESSAGE_MAP(CRobotView, CFormView)   
  21.     //{{AFX_MSG_MAP(CRobotView)    
  22.     ON_WM_CTLCOLOR()   
  23.     ON_WM_TIMER()   
  24.     //}}AFX_MSG_MAP    
  25.     ON_MESSAGE(WM_COMMNOTIFY, OnCommNotify)   
  26. END_MESSAGE_MAP()   
  27.    
  28. /////////////////////////////////////////////////////////////////////////////    
  29. // CRobotView construction/destruction    
  30.    
  31. CRobotView::CRobotView()   
  32.     : CFormView(CRobotView::IDD)   
  33. {   
  34.     //{{AFX_DATA_INIT(CRobotView)    
  35.     //}}AFX_DATA_INIT    
  36.     // TODO: add construction code here    
  37.     m_ncomand1=10;   
  38.     m_bflag=0;   
  39.     m_nsudu=1;   
  40.     m_ngzq=0;   
  41.     m_nguanjie;   
  42.     m_nfangxiang;   
  43.     m_nmsg[0]=0;   
  44.     m_njishu=0;   
  45.     m_nmsg[1]=0;   
  46.     m_nmsg[2]=0;   
  47.     m_nmsg[3]=0;   
  48.     m_nmsg[4]=0;   
  49.     m_nsum=0;   
  50.     m_ndelay=0;   
  51.     nLength=0;   
  52.     m_recflag=1;   
  53.     m_failflag=1;   
  54.     m_brush.CreateSolidBrush(RGB(40,98,122)); // 生成一绿色刷子     
  55.    
  56. }   
  57.    
  58. CRobotView::~CRobotView()   
  59. {   
  60. }   
  61.    
  62. void CRobotView::DoDataExchange(CDataExchange* pDX)   
  63. {   
  64.     CFormView::DoDataExchange(pDX);   
  65.     //{{AFX_DATA_MAP(CRobotView)    
  66.     //DDX_Control(pDX, IDC_EDIT1, m_edit1);    
  67.     DDX_Control(pDX, IDC_STATIC_LINK, m_link);   
  68.     DDX_Control(pDX, IDC_STATIC_1, m_ml);   
  69.     DDX_Control(pDX, IDC_STATIC_4, m_workpart);   
  70.     DDX_Control(pDX, IDC_STATIC_COMMAND, m_command);   
  71.     DDX_Control(pDX, IDC_STATIC_STATUS, m_static);   
  72.     DDX_Control(pDX, IDC_STATIC_BUCHANG, m_buchangshu);   
  73.     DDX_Control(pDX, IDC_STATIC_5, m_buchang);   
  74.     DDX_Control(pDX, IDC_STATIC_3, m_sudu);   
  75.     DDX_Control(pDX, IDC_STATIC_2, m_zhuangtai);   
  76.     //}}AFX_DATA_MAP    
  77. }   
  78.    
  79. BOOL CRobotView::PreCreateWindow(CREATESTRUCT& cs)   
  80. {   
  81.     // TODO: Modify the Window class or styles here by modifying    
  82.     //  the CREATESTRUCT cs    
  83.    
  84.     return CFormView::PreCreateWindow(cs);   
  85. }   
  86.    
  87.    
  88.    
  89. /////////////////////////////////////////////////////////////////////////////    
  90. // CRobotView diagnostics    
  91.    
  92. #ifdef _DEBUG    
  93. void CRobotView::AssertValid() const   
  94. {   
  95.     CFormView::AssertValid();   
  96. }   
  97.    
  98. void CRobotView::Dump(CDumpContext& dc) const   
  99. {   
  100.     CFormView::Dump(dc);   
  101. }   
  102.    
  103. CRobotDoc* CRobotView::GetDocument() // non-debug version is inline    
  104. {   
  105.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRobotDoc)));   
  106.     return (CRobotDoc*)m_pDocument;   
  107. }   
  108. #endif //_DEBUG    
  109.    
  110. /////////////////////////////////////////////////////////////////////////////    
  111. // CRobotView message handlers    
  112.    
  113. void CRobotView::OnDraw(CDC* pDC)    
  114. {   
  115.     // TODO: Add your specialized code here and/or call the base class    
  116.     SuduDisplay(m_nsudu);   
  117.     GzqDisplay(m_ngzq);   
  118. }   
  119.    
  120. void CRobotView::OnInitialUpdate()    
  121. {   
  122.     CFormView::OnInitialUpdate();   
  123.        
  124.     // TODO: Add your specialized code here and/or call the base class    
  125.     m_link.SetLink(TRUE)   
  126.         .SetTextColor(RGB(0,0,255))   
  127.         .SetFontUnderline(TRUE)   
  128.         .SetLinkCursor(AfxGetApp()->LoadCursor(IDC_ICON1));   
  129.     m_command.SetFontSize(24)   
  130.         .SetFontName("Terminal")   
  131.               .SetTextColor(RGB(240,240,40));   
  132.     m_static.SetFontSize(24)   
  133.               .SetTextColor(RGB(240,240,40));   
  134.     m_workpart.SetFontSize(24)   
  135.         .SetFontBold(TRUE)   
  136.               .SetTextColor(RGB(0,240,0));   
  137.     m_buchangshu.SetFontSize(24)   
  138.               .SetTextColor(RGB(240,240,40));   
  139.     m_buchang.SetFontSize(24)   
  140.             .SetFontBold(TRUE)   
  141.               .SetTextColor(RGB(0,240,0));   
  142.     m_sudu.SetFontSize(24)   
  143.             .SetFontBold(TRUE)   
  144.               .SetTextColor(RGB(0,240,0));   
  145.     m_zhuangtai.SetFontSize(24)   
  146.             .SetFontBold(TRUE)   
  147.               .SetTextColor(RGB(0,240,0));   
  148.     m_ml.SetFontSize(24)   
  149.             .SetLinkCursor(AfxGetApp()->LoadCursor(IDC_ICON1))   
  150.             .SetFontBold(TRUE)   
  151.              .SetTextColor(RGB(0,240,0));   
  152.     GetParentFrame()->RecalcLayout();   
  153.     ResizeParentToFit();   
  154. }   
  155.    
  156. LRESULT CRobotView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)    
  157. {   
  158.     // TODO: Add your specialized code here and/or call the base class    
  159.     if(message == (WM_USER + 1002))   
  160.         ProcessMessage(wParam, lParam);   
  161.     else if(message==(WM_USER+1003))   
  162.         ProcessMyMessage();   
  163.     return CFormView::WindowProc(message, wParam, lParam);   
  164. }   
  165. void CRobotView::ProcessMessage(WPARAM wParam, LPARAM lParam)   
  166. {   
  167.        
  168.     CRobotDoc* pDoc=GetDocument();   
  169.     char* l[57] = {"零","一","二",///012    
  170.         "三","四","五","六","七","八","九","执行","各关节归零","大臂正转",///12    
  171.         "大臂反转","小臂正转","小臂反转","手腕正转","手腕反转","手爪张开","手爪闭合","上升","下降","大臂运行",///13--22    
  172.         "大臂负运行","小臂运行","小臂负运行","手腕运行","手腕负运行","手爪运行","手爪负运行","升降运行",////23---30    
  173.         "升降负运行","大臂归零","小臂归零","手腕归零","手爪归零","升降归零","速度加","原点记忆",///31---38    
  174.         "系统复位","示教盒","","","回原点","速度减","区号加","区号减","单步再现","周期再现",///39---48    
  175.         "连续再现","返回","继续","数据记忆","读取数据","设置串口","打开串口","关闭串口"};      
  176.     CString    strcomand;   
  177.     CString    strstatus;   
  178.     m_recflag=1;   
  179.     if(!pDoc->m_bConnected)   
  180.     {   
  181.         KillTimer(1);   
  182.         MessageBox("请先打开串口");   
  183.         return;   
  184.     }   
  185.     if(wParam==100)   
  186.     {   
  187.         KillTimer(1);   
  188.         if(!m_failflag)   
  189.             {   
  190.                     CWnd* pWnd=GetDlgItem(IDC_STATIC_5);   
  191.                     pWnd->SetWindowText("");   
  192.                     pWnd=GetDlgItem(IDC_STATIC_BUCHANG);   
  193.                     pWnd->SetWindowText("");   
  194.                     pWnd=GetDlgItem(IDC_STATIC_COMMAND);   
  195.                     pWnd->SetWindowText("请输入新的命令");   
  196.                     pWnd=GetDlgItem(IDC_STATIC_STATUS);   
  197.                     pWnd->SetWindowText("执行完毕");   
  198.                     m_ncomand1=0;   
  199.                        
  200.                 }   
  201.                 else   
  202.                     MessageBox("请检查连线是否正确或下位机是否打开","通信失败",MB_ICONWARNING);   
  203.                 }   
  204.     else if(wParam>=11&&wParam<=56)   
  205.     {   
  206.         m_bflag=0;   
  207.         m_ncomand1=wParam;   
  208.         strcomand=l[m_ncomand1];   
  209.         CWnd* pWnd=GetDlgItem(IDC_STATIC_COMMAND);   
  210.         pWnd->SetWindowText(strcomand);   
  211.         pWnd=GetDlgItem(IDC_STATIC_STATUS);   
  212.         pWnd->SetWindowText("");   
  213.         pWnd=GetDlgItem(IDC_STATIC_5);   
  214.         pWnd->SetWindowText("");   
  215.         pWnd=GetDlgItem(IDC_STATIC_BUCHANG);   
  216.         pWnd->SetWindowText("");   
  217.         m_nsum=0;   
  218.         m_njishu=0;   
  219.     }   
  220.     else if(wParam>=0&&wParam<=9)   
  221.     {      
  222.         if(m_ncomand1>=12&&m_ncomand1<=21)           
  223.         {   
  224.             m_bflag=1;   
  225.             m_nmsg[m_njishu]=wParam;   
  226.            //pWnd=GetDlgItem(IDC_STATIC_COMMAND);    
  227.             //pWnd->SetWindowText(strcomand);    
  228.             CWnd* pWnd=GetDlgItem(IDC_STATIC_5);   
  229.             pWnd->SetWindowText("步长:");   
  230.             ++m_njishu;   
  231.             if(m_njishu==1)   
  232.             {   
  233.                 CString m_stemp;   
  234.                 m_nsum=m_nmsg[0];   
  235.                 m_stemp.Format("%d",m_nsum);   
  236.                 pWnd=GetDlgItem(IDC_STATIC_BUCHANG);   
  237.                 pWnd->SetWindowText(m_stemp);   
  238.             }   
  239.             if(m_njishu==2)   
  240.             {   
  241.                 CString m_stemp;   
  242.                 m_nsum=m_nmsg[0]*10+m_nmsg[1];   
  243.                 m_stemp.Format("%d",m_nsum);   
  244.                 pWnd=GetDlgItem(IDC_STATIC_BUCHANG);   
  245.                 pWnd->SetWindowText(m_stemp);   
  246.             }   
  247.             if(m_njishu==3)   
  248.             {   
  249.                 CString m_stemp;   
  250.                 m_nsum=m_nmsg[0]*100+m_nmsg[1]*10+m_nmsg[2];   
  251.                 //m_nmsg[0]=0;    
  252.                 //m_nmsg[1]=0    
  253.                 //  m_nmsg[2]=0;    
  254.                 //  m_njishu=0;    
  255.                 m_stemp.Format("%d",m_nsum);   
  256.                 pWnd=GetDlgItem(IDC_STATIC_BUCHANG);   
  257.                 pWnd->SetWindowText(m_stemp);   
  258.             }   
  259.             if(m_njishu==4)   
  260.             {   
  261.                 CString m_stemp;   
  262.                 m_nsum=m_nmsg[0]*1000+m_nmsg[1]*100+m_nmsg[2]*10+m_nmsg[3];   
  263.                 m_stemp.Format("%d",m_nsum);   
  264.                 pWnd=GetDlgItem(IDC_STATIC_BUCHANG);   
  265.                 pWnd->SetWindowText(m_stemp);   
  266.             }   
  267.             if(m_njishu>4)   
  268.             {   
  269.                 m_njishu=1;   
  270.                 m_nmsg[0]=m_nmsg[4];   
  271.                 CString m_stemp;   
  272.                 m_nsum=m_nmsg[0];   
  273.                 m_stemp.Format("%d",m_nsum);   
  274.                 pWnd=GetDlgItem(IDC_STATIC_BUCHANG);   
  275.                 pWnd->SetWindowText(m_stemp);   
  276.                 m_nmsg[1]=0;   
  277.                 m_nmsg[2]=0;   
  278.                 m_nmsg[3]=0;   
  279.             }   
  280.         }          
  281.         else   
  282.             ;   //MessageBox("清先输入运动步数命令");    
  283.     }   
  284.     else if(wParam=10)   
  285.     {    SetTimer(1,500,NULL);   
  286.         if(m_ncomand1>=11&&m_ncomand1<=56)   
  287.         {   
  288.             if(!m_bflag)   
  289.             {   
  290.                 strstatus="正在执行命令,请稍后.....";   
  291.                 CWnd* pWnd=GetDlgItem(IDC_STATIC_STATUS);   
  292.                 pWnd->SetWindowText(strstatus);   
  293.                 Execute(m_ncomand1);   
  294.                
  295.             }      
  296.             else //if(m_ncomand1>=12&&m_ncomand1<=21)    
  297.             {   
  298.                 unsigned char m_uhi,m_ulo;   
  299.                 m_nmsg[0]=0;   
  300.                 m_nmsg[1]=0;   
  301.                 m_nmsg[2]=0;   
  302.                 m_nmsg[3]=0;   
  303.                 m_njishu=0;   
  304.             /// CWnd* pWnd=GetDlgItem(IDC_STATIC_STATUS);    
  305.                /// pWnd->SetWindowText("正在执行命令,请稍后.....");    
  306.                 switch(m_ncomand1)   
  307.                 {   
  308.                 case(12):   
  309.                     m_nguanjie=01;   
  310.                     m_nfangxiang=00;   
  311.                     m_uhi=(unsigned char)(m_nsum/256);   
  312.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  313.                     m_ndelay=(m_nsum+500)/m_nsudu;   
  314.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  315.                     break;   
  316.                     case(13):   
  317.                     m_nguanjie=0x01;   
  318.                     m_nfangxiang=0xff;   
  319.                     m_uhi=(unsigned char)(m_nsum/256);   
  320.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  321.                     m_ndelay=(m_nsum+500)/m_nsudu;   
  322.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  323.                     break;   
  324.                     case(14):   
  325.                     m_nguanjie=0x02;   
  326.                     m_nfangxiang=0x00;   
  327.                     m_uhi=(unsigned char)(m_nsum/256);   
  328.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  329.                     m_ndelay=(m_nsum)/(m_nsudu*3);   
  330.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  331.                     break;   
  332.                     case(15):   
  333.                     m_nguanjie=0x02;   
  334.                     m_nfangxiang=0xff;   
  335.                     m_uhi=(unsigned char)(m_nsum/256);   
  336.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  337.                     m_ndelay=(m_nsum)/(m_nsudu*3);   
  338.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  339.                     break;   
  340.                     case(16):   
  341.                     m_nguanjie=0x04;   
  342.                     m_nfangxiang=0x00;   
  343.                     m_uhi=(unsigned char)(m_nsum/256);   
  344.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  345.                     m_ndelay=(m_nsum+500)/m_nsudu;   
  346.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  347.                     break;   
  348.                     case(17):   
  349.                     m_nguanjie=0x04;   
  350.                     m_nfangxiang=0xff;   
  351.                     m_uhi=(unsigned char)(m_nsum/256);   
  352.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  353.                     m_ndelay=(m_nsum+500)/m_nsudu;   
  354.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  355.                     break;   
  356.                     case(18):   
  357.                     m_nguanjie=0x05;   
  358.                     m_nfangxiang=0x00;   
  359.                     m_uhi=(unsigned char)(m_nsum/256);   
  360.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  361.                     m_ndelay=(m_nsum+500)/m_nsudu;   
  362.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  363.                     break;   
  364.                     case(19):   
  365.                     m_nguanjie=0x05;   
  366.                     m_nfangxiang=0xff;   
  367.                     m_uhi=(unsigned char)(m_nsum/256);   
  368.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  369.                     m_ndelay=(m_nsum+500)/m_nsudu;   
  370.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  371.                     break;   
  372.                     case(20):   
  373.                     m_nguanjie=0x03;   
  374.                     m_nfangxiang=0x00;   
  375.                     m_uhi=(unsigned char)(m_nsum/256);   
  376.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  377.                     m_ndelay=(m_nsum+500)/(m_nsudu*2);   
  378.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  379.                     break;   
  380.                     case(21):   
  381.                     m_nguanjie=0x03;   
  382.                     m_nfangxiang=0xff;   
  383.                     m_uhi=(unsigned char)(m_nsum/256);   
  384.                     m_ulo=(unsigned char)(m_nsum-m_uhi*256);   
  385.                     m_ndelay=(m_nsum+500)/(m_nsudu*2);   
  386.                     Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);   
  387.                     break;   
  388.                 }   
  389.                    
  390.            }   
  391.             m_failflag=1;}   
  392.        else   
  393.            MessageBox("清先输入命令");   
  394.      }   
  395.      else   
  396.          MessageBox("不能识别,请重新输入命令");   
  397. }   
  398. void CRobotView::ProcessMyMessage()   
  399. {   
  400.     CDC *PDc=GetDC();   
  401.     m_failflag=0;   
  402.         switch(m_ncomand1)   
  403.         {   
  404.         case(37):   
  405.             {   
  406.                 m_nsudu++;   
  407.                 if(m_nsudu==6)   
  408.                     m_nsudu=1;   
  409.                 OnDraw(PDc);   
  410.             }   
  411.             break;   
  412.         case(44):   
  413.             {   
  414.                 m_nsudu--;   
  415.                 if(m_nsudu==0)   
  416.                     m_nsudu=5;   
  417.                 OnDraw(PDc);   
  418.             }   
  419.             break;   
  420.         case(45):   
  421.             {   
  422.                 m_ngzq++;   
  423.                 if(m_ngzq==8)   
  424.                     m_ngzq=0;   
  425.                 OnDraw(PDc);   
  426.             }   
  427.             break;   
  428.         case(46):   
  429.             {   
  430.                 m_ngzq--;   
  431.                 if(m_ngzq==-1)   
  432.                     m_ngzq=7;   
  433.                 OnDraw(PDc);   
  434.             }   
  435.             break;   
  436.         }   
  437.         CWnd* pWnd=GetDlgItem(IDC_STATIC_COMMAND);   
  438.         pWnd->SetWindowText("请输入新的命令");   
  439.         pWnd=GetDlgItem(IDC_STATIC_STATUS);   
  440.         pWnd->SetWindowText("执行完毕");   
  441.         m_ncomand1=0;   
  442.     //}    
  443.     //else    
  444.     //  MessageBox("请检查连线是否正确或下位机是否打开","通信失败",MB_ICONWARNING);    
  445. }   
  446. void CRobotView::SuduDisplay(int n)   
  447. {   
  448.     CWnd* pWnd=GetDlgItem(IDC_STATIC_SUDU);   
  449.     CDC* pControlDC=pWnd->GetDC();   
  450.     pWnd->Invalidate();   
  451.     pWnd->UpdateWindow();   
  452.     pControlDC->SelectStockObject(BLACK_BRUSH);   
  453.     CBitmap mBit;   
  454.     CDC MemDC;   
  455.     MemDC.CreateCompatibleDC(NULL);   
  456.     switch(n)   
  457.     {   
  458.     case 0:   
  459.     mBit.LoadBitmap(IDB_BMP10);   
  460.     break;   
  461.     case 1:   
  462.     mBit.LoadBitmap(IDB_BMP11);   
  463.     break;   
  464.     case 2:   
  465.     mBit.LoadBitmap(IDB_BMP12);   
  466.     break;   
  467.     case 3:   
  468.     mBit.LoadBitmap(IDB_BMP13);   
  469.     break;   
  470.     case 4:   
  471.     mBit.LoadBitmap(IDB_BMP14);   
  472.     break;   
  473.     case 5:   
  474.     mBit.LoadBitmap(IDB_BMP15);   
  475.     break;   
  476.     default:   
  477.         //mBit.LoadBitmap(IDB_BMP0);    
  478.         MessageBox("error");   
  479.     }   
  480.     CBitmap *pOldBit=MemDC.SelectObject(&mBit);   
  481.     pControlDC->BitBlt(0,0,900,700,&MemDC,0,0,SRCCOPY);   
  482.     MemDC.SelectObject(pOldBit);   
  483. }   
  484. void CRobotView::GzqDisplay(int n)   
  485. {   
  486.     CWnd* pWnd=GetDlgItem(IDC_STATIC_GONGZUOQU);   
  487.     CDC* pControlDC=pWnd->GetDC();   
  488.     pWnd->Invalidate();   
  489.     pWnd->UpdateWindow();   
  490.     pControlDC->SelectStockObject(BLACK_BRUSH);   
  491.     CBitmap mBit;   
  492.     CDC MemDC;   
  493.     MemDC.CreateCompatibleDC(NULL);   
  494.     switch(n)   
  495.     {   
  496.     case 0:   
  497.     mBit.LoadBitmap(IDB_BMP10);   
  498.     break;   
  499.     case 1:   
  500.     mBit.LoadBitmap(IDB_BMP11);   
  501.     break;   
  502.     case 2:   
  503.     mBit.LoadBitmap(IDB_BMP12);   
  504.     break;   
  505.     case 3:   
  506.     mBit.LoadBitmap(IDB_BMP13);   
  507.     break;   
  508.     case 4:   
  509.     mBit.LoadBitmap(IDB_BMP14);   
  510.     break;   
  511.     case 5:   
  512.     mBit.LoadBitmap(IDB_BMP15);   
  513.     break;   
  514.     case 6:   
  515.     mBit.LoadBitmap(IDB_BMP16);   
  516.     break;   
  517.     case 7:   
  518.     mBit.LoadBitmap(IDB_BMP17);   
  519.     break;   
  520.     default:   
  521.         //mBit.LoadBitmap(IDB_BMP0);    
  522.         MessageBox("error");   
  523.     }   
  524.     CBitmap *pOldBit=MemDC.SelectObject(&mBit);   
  525.     pControlDC->BitBlt(0,0,900,700,&MemDC,0,0,SRCCOPY);   
  526.     MemDC.SelectObject(pOldBit);   
  527. }   
  528. void CRobotView::Execute(int n)   
  529. {   
  530.     CRobotDoc* pDoc=GetDocument();   
  531.    
  532.     unsigned char send1;   
  533.    
  534.     switch(n)   
  535.     {   
  536.     case(11):   
  537.         home();   
  538.         break;   
  539.     case(12):   
  540.         {//大臂正传    
  541.         unsigned char send[6]={0x53,0x01,0x00,0x00,0x07,0x18};   
  542.     for(int i=0;i<=5;i++)   
  543.     pDoc->WriteComm(&send[i],1);   
  544.     Sleep(2000/m_nsudu);   
  545.     break;   
  546.             }   
  547.     case(13):   
  548.         {   
  549.     unsigned char send[6]={0x53,0x01,0xff,0x00,0x07,0x18};   
  550.     for(int i=0;i<=5;i++)   
  551.     pDoc->WriteComm(&send[i],1);   
  552. //  pDoc->ReadComm(&buf,1);    
  553. //  if(nLength)    
  554. ////    {    
  555. //      str.Format("%d",buf);    
  556. //      m_edit1.SetSel(-1, 0);    
  557. //      m_edit1.ReplaceSel(str); // 向编辑视图中插入收到的字符    
  558. //  }    
  559. //  else    
  560. //  MessageBox("fjdklfjsdklgjsdfkl");    
  561.     Sleep(2000/m_nsudu);   
  562.     break;   
  563.         }   
  564.     case(14)://xiaobizhengzhuan    
  565.         {   
  566.         unsigned char send[6]={0x53,0x02,0x00,0x00,0x0f,0xe3};   
  567.     for(int i=0;i<=5;i++)   
  568.     pDoc->WriteComm(&send[i],1);   
  569.     Sleep(2000/m_nsudu);   
  570.             break;   
  571.         }   
  572.        
  573.     case(15)://小臂反转    
  574.         {   
  575.         unsigned char send[6]={0x53,0x02,0xff,0x00,0x0f,0xe3};   
  576.     for(int i=0;i<=5;i++)   
  577.     pDoc->WriteComm(&send[i],1);   
  578.     Sleep(2000/m_nsudu);   
  579.     break;     
  580.         }      
  581.     case(16)://手腕正转    
  582.             {   
  583.         unsigned char send[6]={0x53,0x04,0x00,0x00,0x00,0xff};   
  584.     for(int i=0;i<=5;i++)   
  585.     pDoc->WriteComm(&send[i],1);   
  586.     Sleep(1000/m_nsudu);   
  587.     break;   
  588.             }   
  589.     case(17)://shouwanfanzhuan    
  590.         {   
  591.         unsigned char send[6]={0x53,0x04,0xff,0x00,0x00,0xff};   
  592.     for(int i=0;i<=5;i++)   
  593.     pDoc->WriteComm(&send[i],1);   
  594.     Sleep(1000/m_nsudu);   
  595.         }   
  596.         break;   
  597.     case(18)://shouzhuazhangkai    
  598.             {   
  599.         unsigned char send[6]={0x53,0x05,0x00,0x00,0x00,0x43};   
  600.     for(int i=0;i<=5;i++)   
  601.     pDoc->WriteComm(&send[i],1);   
  602.     Sleep(2000/m_nsudu);   
  603.         }   
  604.         break;   
  605.     case(19):   
  606.         {   
  607.         unsigned char send[6]={0x53,0x05,0xff,0x00,0x00,0xf3};   
  608.     for(int i=0;i<=5;i++)   
  609.     pDoc->WriteComm(&send[i],1);   
  610.     Sleep(2000/m_nsudu);   
  611.         }   
  612.         break;   
  613.     case(20)://shangsheng    
  614.             {   
  615.         unsigned char send[6]={0x53,0x03,0x00,0x00,0x02,0xe3};   
  616.     for(int i=0;i<=5;i++)   
  617.     pDoc->WriteComm(&send[i],1);   
  618.     Sleep(1000/m_nsudu);   
  619.         }   
  620.         break;   
  621.     case(21):   
  622.         {   
  623.         unsigned char send[6]={0x53,0x03,0xff,0x00,0x02,0xe3};   
  624.     for(int i=0;i<=5;i++)   
  625.     pDoc->WriteComm(&send[i],1);   
  626.     Sleep(1000/m_nsudu);   
  627.         }   
  628.         break;   
  629. /*  case(22)://dabiyunxing  
  630.         {  
  631.         unsigned char send[3]={0x63,0x01,0x00};  
  632.     for(int i=0;i<=2;i++)  
  633.     pDoc->WriteComm(&send[i],1);  
  634.     Sleep(2000);  
  635.         }  
  636.         break;  
  637.     case(23):  
  638.             {  
  639.         unsigned char send[3]={0x63,0x01,0xff};  
  640.     for(int i=0;i<=2;i++)  
  641.     pDoc->WriteComm(&send[i],1);  
  642.     Sleep(2000);  
  643.         }  
  644.         break;  
  645.     case(24)://xiaobiyunxing  
  646.         {  
  647.         unsigned char send[3]={0x63,0x02,0x00};  
  648.     for(int i=0;i<=2;i++)  
  649.     pDoc->WriteComm(&send[i],1);  
  650.     Sleep(2000);  
  651.         }  
  652.         break;  
  653.     case(25):  
  654.         {  
  655.         unsigned char send[3]={0x63,0x02,0xff};  
  656.     for(int i=0;i<=2;i++)  
  657.     pDoc->WriteComm(&send[i],1);  
  658.     Sleep(2000);  
  659.         }  
  660.         break;  
  661.     case(26)://shouwanyunxing  
  662.         {  
  663.         unsigned char send[3]={0x63,0x04,0x00};  
  664.     for(int i=0;i<=2;i++)  
  665.     pDoc->WriteComm(&send[i],1);  
  666.     Sleep(2000);  
  667.         }  
  668.         break;  
  669.     case(27):  
  670.         {  
  671.         unsigned char send[3]={0x63,0x04,0xff};  
  672.     for(int i=0;i<=2;i++)  
  673.     pDoc->WriteComm(&send[i],1);  
  674.     Sleep(2000);  
  675.         }  
  676.         break;  
  677.     case(28)://shouzhuayunxing  
  678.         {  
  679.         unsigned char send[3]={0x63,0x05,0x00};  
  680.     for(int i=0;i<=2;i++)  
  681.     pDoc->WriteComm(&send[i],1);  
  682.     Sleep(2000);  
  683.         }  
  684.         break;  
  685.     case(29):  
  686.             {  
  687.         unsigned char send[3]={0x63,0x05,0xff};  
  688.     for(int i=0;i<=2;i++)  
  689.     pDoc->WriteComm(&send[i],1);  
  690.     Sleep(2000);  
  691.         }  
  692.         break;  
  693.     case(30)://shengjiangyunxing  
  694.             {  
  695.         unsigned char send[3]={0x63,0x03,0x00};  
  696.     for(int i=0;i<=2;i++)  
  697.     pDoc->WriteComm(&send[i],1);  
  698.     Sleep(2000);  
  699.         }  
  700.         break;  
  701.     case(31):  
  702.             {  
  703.         unsigned char send[3]={0x63,0x05,0xff};  
  704.     for(int i=0;i<=2;i++)  
  705.     pDoc->WriteComm(&send[i],1);  
  706.     Sleep(2000);  
  707.         }  
  708.         break;  
  709. */   
  710.     case(32)://dabiguiling    
  711.             {   
  712.         unsigned char send[2]={0xa3,0x01};   
  713.     for(int i=0;i<=1;i++)   
  714.     pDoc->WriteComm(&send[i],1);   
  715.     Sleep(18000);   
  716.         }   
  717.         break;   
  718.     case(33)://xiaobiguiling    
  719.             {   
  720.         unsigned char send[2]={0xa3,0x02};   
  721.     for(int i=0;i<=1;i++)   
  722.     pDoc->WriteComm(&send[i],1);   
  723.     Sleep(15000);   
  724.         }   
  725.         break;   
  726.     case(34)://shouwanguiling    
  727.             {   
  728.         unsigned char send[2]={0xa3,0x04};   
  729.     for(int i=0;i<=1;i++)   
  730.     pDoc->WriteComm(&send[i],1);   
  731.     Sleep(6000);   
  732.         }   
  733.         break;   
  734.     case(35)://shouzhuaguiling    
  735.             {   
  736.         unsigned char send[2]={0xa3,0x05};   
  737.     for(int i=0;i<=1;i++)   
  738.     pDoc->WriteComm(&send[i],1);   
  739.     Sleep(6000);   
  740.         }   
  741.         break;   
  742.     case(36)://shengjiangguiling    
  743.             {   
  744.         unsigned char send[2]={0xa3,0x03};   
  745.     for(int i=0;i<=1;i++)   
  746.     pDoc->WriteComm(&send[i],1);   
  747.     Sleep(10000);   
  748.         }   
  749.         break;   
  750.     case(37)://sudujia    
  751.         {      
  752.         send1=0xb3;   
  753.         pDoc->WriteComm(&send1,1);   
  754.         }   
  755.             Sleep(1000);   
  756.         break;   
  757.     case(38)://yuandianjiyi    
  758.         send1=0x73;   
  759.         pDoc->WriteComm(&send1,1);   
  760.         break;   
  761.     case(39):   
  762.         send1=0xc3;   
  763.         pDoc->WriteComm(&send1,1);   
  764.         break;   
  765.     case(40):   
  766.         send1=0xf6;   
  767.         pDoc->WriteComm(&send1,1);   
  768.         break;   
  769.     case(41):   
  770.         send1=0x83;   
  771.         pDoc->WriteComm(&send1,1);   
  772.         break;   
  773.     case(42):   
  774.         send1=0x93;   
  775.         pDoc->WriteComm(&send1,1);   
  776.         break;   
  777.     case(43):   
  778.     //  {    
  779.     //  unsigned char send[2]={0x86,0x03};    
  780.     //  for(int i=0;i<=1;i++)    
  781.     //  pDoc->WriteComm(&send[i],1);    
  782.     //  Sleep(4000);    
  783.     //  }    
  784.         send1=0x86;   
  785.         pDoc->WriteComm(&send1,1);   
  786.         break;   
  787.     case(44)://sudujian    
  788.         {   
  789.         send1=0xe3;   
  790.         pDoc->WriteComm(&send1,1);   
  791.         }   
  792.         break;   
  793.     case(45):   
  794.         {   
  795.         send1=0x12;   
  796.         pDoc->WriteComm(&send1,1);   
  797.         }   
  798.         break;   
  799.     case(46):   
  800.         {   
  801.         send1=0x22;   
  802.         pDoc->WriteComm(&send1,1);   
  803.         }   
  804.         break;   
  805.     case(47):       //danbuzaixian    
  806.         send1=0x13;   
  807.         pDoc->WriteComm(&send1,1);   
  808.         Sleep(4000);   
  809.         break;   
  810.     case(48):       //zhouqizaixian    
  811.         send1=0x23;   
  812.         pDoc->WriteComm(&send1,1);   
  813.         Sleep(8000);   
  814.         break;   
  815.     case(49):       //lianxuzaixian    
  816.         send1=0x33;   
  817.         pDoc->WriteComm(&send1,1);   
  818.         break;   
  819.     case(50):   
  820.         //send1=0x32;    
  821.         //pDoc->WriteComm(&send1,1);    
  822.         break;   
  823.     case(51):   
  824.         break;   
  825.     case(52):   
  826.         send1=0x43;   
  827.         pDoc->WriteComm(&send1,1);   
  828.         break;   
  829.     case(53):   
  830.         break;   
  831.     case(54):   
  832.         pDoc->OnSetupcomm();   
  833.         break;   
  834.     case(55):   
  835.         pDoc->OnOpencomm();   
  836.         break;   
  837.     default:   
  838.         pDoc->OnClosecomm();   
  839.         break;   
  840.     }   
  841.    
  842. }   
  843. //void CRobotView::OnButton3()     
  844. //{    
  845.     // TODO: Add your control notification handler code here    
  846. //  SendMessage(WM_USER + 1002,10,0);    
  847. //}    
  848. void CRobotView::Execute_1(unsigned char a,unsigned char b,unsigned char c,unsigned char d,int e)   
  849. {   
  850.     CRobotDoc* pDoc=GetDocument();   
  851.     unsigned char send[6]={0x53,a,b,0x00,c,d};   
  852.     for(int i=0;i<=5;i++)   
  853.     pDoc->WriteComm(&send[i],1);   
  854.     Sleep(m_ndelay);   
  855. }   
  856.    
  857.    
  858. HBRUSH CRobotView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)    
  859. {   
  860.     HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);   
  861.        
  862.     // TODO: Change any attributes of the DC here    
  863.         switch(nCtlColor)   
  864.         {   
  865.     case CTLCOLOR_DLG:   
  866.     {   
  867.     //pDC->SetBkMode(TRANSPARENT);    
  868.     return m_brush;   //返加绿色刷子    
  869.     }   
  870.     case CTLCOLOR_STATIC:   
  871.     {   
  872.        //返加绿色刷子    
  873.         //  pDC->SetBkMode(TRANSPARENT);    
  874.                 return m_brush;   
  875.     }   
  876.         }   
  877.     // TODO: Return a different brush if the default is not desired    
  878.     return hbr;   
  879. }   
  880. void CRobotView::home()    
  881. {   
  882.     SendMessage(WM_USER+1002,32,0);   
  883.     SendMessage(WM_USER+1002,10,0);   
  884.     Sleep(1000);   
  885.     SendMessage(WM_USER+1002,33,0);   
  886.     SendMessage(WM_USER+1002,10,0);   
  887.     Sleep(1000);   
  888.    
  889.     SendMessage(WM_USER+1002,34,0);   
  890.     SendMessage(WM_USER+1002,10,0);   
  891.     Sleep(1000);   
  892.     SendMessage(WM_USER+1002,35,0);   
  893.     SendMessage(WM_USER+1002,10,0);   
  894.     Sleep(1000);   
  895.     SendMessage(WM_USER+1002,36,0);   
  896.     SendMessage(WM_USER+1002,10,0);   
  897.     Sleep(1000);   
  898. }   
  899. LRESULT CRobotView::OnCommNotify(WPARAM wParam, LPARAM lParam)   
  900. {   
  901.     unsigned char buf;   
  902.     CWnd* pWnd=GetDlgItem(IDC_STATIC_COMMAND);   
  903.     CString str1,str2;   
  904.     CRobotDoc* pDoc=GetDocument();   
  905.     m_recflag=0;   
  906.     if(!pDoc->m_bConnected ||    
  907.         (wParam & EV_RXCHAR)!=EV_RXCHAR) // 是否是EV_RXCHAR事件?    
  908.     {   
  909.         SetEvent(pDoc->m_hPostMsgEvent); // 允许发送下一个WM_COMMNOTIFY消息    
  910.         return 0L;   
  911.     }   
  912.     nLength=pDoc->ReadComm(&buf,1);   
  913.     if(nLength)   
  914.     {   
  915.     //  str1.Format("%d",buf);    
  916.         //m_edit1.SetSel(-1, 0);    
  917.     //  m_edit1.ReplaceSel(str1); // 向编辑视图中插入收到的字符    
  918.         str2="正在执行命令,请稍后.....";   
  919.         pWnd->SetWindowText(str2);   
  920.         SendMessage(WM_USER+1003);   
  921.     }   
  922.     SetEvent(pDoc->m_hPostMsgEvent); // 允许发送下一个WM_COMMNOTIFY消息    
  923.     return 0L;   
  924. }   
  925.    
  926. void CRobotView::OnTimer(UINT nIDEvent)    
  927. {   
  928.     // TODO: Add your message handler code here and/or call default    
  929.     SendMessage(WM_USER+1002,100);   
  930.    
  931.     CFormView::OnTimer(nIDEvent);   
  932. }   

文档类

  1. #include "stdafx.h"    
  2. #include "robot.h"    
  3. #include"SetupDlg.h"    
  4. #include "robotDoc.h"    
  5. #ifdef _DEBUG    
  6. #define new DEBUG_NEW    
  7. #undef THIS_FILE    
  8. static char THIS_FILE[] = __FILE__;   
  9. #endif    
  10.    
  11. /////////////////////////////////////////////////////////////////////////////    
  12. // CRobotDoc    
  13.    
  14. IMPLEMENT_DYNCREATE(CRobotDoc, CDocument)   
  15.    
  16. BEGIN_MESSAGE_MAP(CRobotDoc, CDocument)   
  17.     //{{AFX_MSG_MAP(CRobotDoc)    
  18.     ON_COMMAND(ID_SETUPCOMM, OnSetupcomm)   
  19.     ON_COMMAND(ID_CLOSECOMM, OnClosecomm)   
  20.     ON_COMMAND(ID_OPENCOMM, OnOpencomm)   
  21.     ON_UPDATE_COMMAND_UI(ID_OPENCOMM, OnUpdateOpencomm)   
  22.     ON_UPDATE_COMMAND_UI(ID_CLOSECOMM, OnUpdateClosecomm)   
  23.     ON_COMMAND(ID_ANJIANCTRL, OnAnjianctrl)   
  24.     ON_UPDATE_COMMAND_UI(ID_ANJIANCTRL, OnUpdateAnjianctrl)   
  25.     ON_COMMAND(ID_VOICECTRL, OnVoicectrl)   
  26.     ON_UPDATE_COMMAND_UI(ID_VOICECTRL, OnUpdateVoicectrl)   
  27.     ON_COMMAND(ID_HELP, OnHelp)   
  28.     //}}AFX_MSG_MAP    
  29. END_MESSAGE_MAP()   
  30.    
  31. /////////////////////////////////////////////////////////////////////////////    
  32. // CRobotDoc construction/destruction    
  33.    
  34. CRobotDoc::CRobotDoc()   
  35. {   
  36.     // TODO: add one-time construction code here    
  37.     m_bConnected=FALSE;   
  38.     m_pThread=NULL;   
  39.     m_nBaud =244;   
  40.     m_nDataBits = 8;   
  41.     m_nParity = 1;   
  42.     m_sPort = "COM1";   
  43.     m_nStopBits = 0;   
  44.     m_bctrlmodal=FALSE;   
  45.     m_uCurrentBtn=ID_CLOSECOMM;   
  46. }   
  47.    
  48. CRobotDoc::~CRobotDoc()   
  49. {   
  50.     if(m_bConnected)   
  51.     CloseConnection();   
  52.     // 删除事件句柄    
  53.     if(m_hPostMsgEvent)   
  54.     CloseHandle(m_hPostMsgEvent);   
  55.     if(m_osRead.hEvent)   
  56.     CloseHandle(m_osRead.hEvent);   
  57.     if(m_osWrite.hEvent)   
  58.     CloseHandle(m_osWrite.hEvent);   
  59. }   
  60.    
  61. BOOL CRobotDoc::OnNewDocument()   
  62. {   
  63.     if (!CDocument::OnNewDocument())   
  64.         return FALSE;   
  65.    
  66.     // TODO: add reinitialization code here    
  67.     // (SDI documents will reuse this document)    
  68.     // 为WM_COMMNOTIFY消息创建事件对象,手工重置,初始化为有信号的    
  69.     if((m_hPostMsgEvent=CreateEvent(NULL, TRUE, TRUE, NULL))==NULL)   
  70.         return FALSE;   
  71.     memset(&m_osRead, 0, sizeof(OVERLAPPED));   
  72.     memset(&m_osWrite, 0, sizeof(OVERLAPPED));   
  73.     // 为重叠读创建事件对象,手工重置,初始化为无信号的    
  74.     if((m_osRead.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)   
  75.         return FALSE;   
  76.     // 为重叠写创建事件对象,手工重置,初始化为无信号的    
  77.     if((m_osWrite.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)   
  78.         return FALSE;   
  79.     return TRUE;   
  80. }   
  81.    
  82.    
  83.    
  84. /////////////////////////////////////////////////////////////////////////////    
  85. // CRobotDoc serialization    
  86.    
  87. void CRobotDoc::Serialize(CArchive& ar)   
  88. {   
  89.     if (ar.IsStoring())   
  90.     {   
  91.         // TODO: add storing code here    
  92.     }   
  93.     else   
  94.     {   
  95.         // TODO: add loading code here    
  96.     }   
  97. }   
  98.    
  99. /////////////////////////////////////////////////////////////////////////////    
  100. // CRobotDoc diagnostics    
  101.    
  102. #ifdef _DEBUG    
  103. void CRobotDoc::AssertValid() const   
  104. {   
  105.     CDocument::AssertValid();   
  106. }   
  107.    
  108. void CRobotDoc::Dump(CDumpContext& dc) const   
  109. {   
  110.     CDocument::Dump(dc);   
  111. }   
  112. #endif //_DEBUG    
  113.    
  114. /////////////////////////////////////////////////////////////////////////////    
  115. // CRobotDoc commands    
  116.    
  117. void CRobotDoc::OnSetupcomm()    
  118. {   
  119.     // TODO: Add your command handler code here    
  120.     CSetupDlg dlg;   
  121.     CString str;   
  122.     dlg.m_bConnected=m_bConnected;   
  123.     dlg.m_sPort=m_sPort;   
  124.     str.Format("%d",m_nBaud);   
  125.     dlg.m_sBaud=str;   
  126.     str.Format("%d",m_nDataBits);   
  127.     dlg.m_sDataBits=str;   
  128.     dlg.m_nParity=m_nParity;   
  129.     dlg.m_nStopBits=m_nStopBits;   
  130.     //dlg.m_nFlowCtrl=m_nFlowCtrl;    
  131.     //dlg.m_bEcho=m_bEcho;    
  132. //  dlg.m_bNewLine=m_bNewLine;    
  133.     if(dlg.DoModal()==IDOK)   
  134.     {   
  135.         m_sPort=dlg.m_sPort;   
  136.         m_nBaud=atoi(dlg.m_sBaud);   
  137.         m_nDataBits=atoi(dlg.m_sDataBits);   
  138.         m_nParity=dlg.m_nParity;   
  139.         m_nStopBits=dlg.m_nStopBits;   
  140.     //  m_nFlowCtrl=dlg.m_nFlowCtrl;    
  141.     //  m_bEcho=dlg.m_bEcho;    
  142.     //  m_bNewLine=dlg.m_bNewLine;    
  143.         if(m_bConnected)   
  144.             if(!ConfigConnection())   
  145.                 AfxMessageBox("Can't realize the settings!");   
  146.     }   
  147. }   
  148.    
  149. void CRobotDoc::OnClosecomm()    
  150. {   
  151.     // TODO: Add your command handler code here    
  152.     m_uCurrentBtn=ID_CLOSECOMM;   
  153.     CloseConnection();   
  154. }   
  155.    
  156. void CRobotDoc::OnOpencomm()    
  157. {   
  158.     // TODO: Add your command handler code here    
  159.     m_uCurrentBtn=ID_OPENCOMM;   
  160.     if(!OpenConnection())   
  161.         AfxMessageBox("Can't open connection");   
  162. }   
  163.    
  164. void CRobotDoc::OnUpdateOpencomm(CCmdUI* pCmdUI)    
  165. {   
  166.     // TODO: Add your command update UI handler code here    
  167.     //pCmdUI->Enable(!m_bConnected);    
  168.         pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);   
  169. }   
  170.    
  171. void CRobotDoc::OnUpdateClosecomm(CCmdUI* pCmdUI)    
  172. {   
  173.     // TODO: Add your command update UI handler code here    
  174. //  pCmdUI->Enable(m_bConnected);    
  175.     pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);   
  176. }   
  177.    
  178. void CRobotDoc::OnAnjianctrl()    
  179. {   
  180.     // TODO: Add your command handler code here    
  181.        
  182. }   
  183.    
  184. void CRobotDoc::OnUpdateAnjianctrl(CCmdUI* pCmdUI)    
  185. {   
  186.     // TODO: Add your command update UI handler code here    
  187.        
  188. }   
  189.    
  190. void CRobotDoc::OnVoicectrl()    
  191. {   
  192.     // TODO: Add your command handler code here    
  193. //  WinExec(NULL,NULL,_T("dutty.exe"),NULL,_T("D://Program Files//Dutty//Dutty.exe"),NULL);    
  194.     WinExec(_T("D://Program Files//Dutty//Dutty.exe"),SW_SHOW);   
  195.     m_bctrlmodal=TRUE;   
  196.     //STARTUPINFO stinfo;   //启动窗口的信息     
  197.       //PROCESSINFO procinfo;  //进程的信息        
  198.        //CreateProcess(NULL,_T("dutty.exe"),NULL,NULL.FALSE, NORMAL_PRIORITY_ CLASS,NULL,NULL, &stinfo,&procinfo);     
  199. }   
  200.    
  201. void CRobotDoc::OnUpdateVoicectrl(CCmdUI* pCmdUI)    
  202. {   
  203.     // TODO: Add your command update UI handler code here    
  204.     pCmdUI->Enable(!m_bctrlmodal);   
  205. }   
  206. UINT CommProc(LPVOID pParam)   
  207. {   
  208.     OVERLAPPED os;   
  209.     DWORD dwMask, dwTrans;   
  210.     COMSTAT ComStat;   
  211.     DWORD dwErrorFlags;   
  212.     CRobotDoc *pDoc=(CRobotDoc*)pParam;   
  213.     memset(&os, 0, sizeof(OVERLAPPED));   
  214.     os.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);   
  215.     if(os.hEvent==NULL)   
  216.     {   
  217.         AfxMessageBox("Can't create event object!");   
  218.         return (UINT)-1;   
  219.     }   
  220.     while(pDoc->m_bConnected)   
  221.     {   
  222.         ClearCommError(pDoc->m_hCom,&dwErrorFlags,&ComStat);   
  223.         if(ComStat.cbInQue)         //查询输入缓冲区中是否有字符 ,若有    
  224.         {   
  225.             // 等待WM_COMMNOTIFY消息被处理完    
  226.             WaitForSingleObject(pDoc->m_hPostMsgEvent, INFINITE);   
  227.             ResetEvent(pDoc->m_hPostMsgEvent);   
  228.             PostMessage(pDoc->m_hTermWnd, WM_COMMNOTIFY, EV_RXCHAR, 0);   
  229.             // 通知视图    
  230.             continue;   
  231.         }   
  232.         dwMask=0;   
  233.         if(!WaitCommEvent(pDoc->m_hCom, &dwMask, &os)) // 重叠操作    
  234.         {   
  235.             //通信事件    
  236.             if(GetLastError()==ERROR_IO_PENDING)   
  237.                 // 无限等待重叠操作结果    
  238.                 GetOverlappedResult(pDoc->m_hCom, &os, &dwTrans, TRUE);   
  239.             else   
  240.             {   
  241.                 CloseHandle(os.hEvent);   
  242.                 return (UINT)-1;   
  243.             }   
  244.         }   
  245.     }   
  246.     CloseHandle(os.hEvent);   
  247.     return 0;   
  248. }   
  249. BOOL CRobotDoc::OpenConnection()   
  250. {   
  251.     COMMTIMEOUTS TimeOuts;   
  252.     POSITION firstViewPos;   
  253.     CView *pView;   
  254.     firstViewPos=GetFirstViewPosition();   
  255.     pView=GetNextView(firstViewPos);   
  256.     m_hTermWnd=pView->GetSafeHwnd();   
  257.     if(m_bConnected)   
  258.         return FALSE;   
  259.     m_hCom=CreateFile(m_sPort, GENERIC_READ|GENERIC_WRITE,0,NULL,   
  260.         OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,    
  261.         NULL); // 重叠方式    
  262.     if(m_hCom==INVALID_HANDLE_VALUE)   
  263.         //AfxMessageBox("dfhksdjfhsjkfhks",MB_OK);    
  264.         return FALSE;   
  265.     SetupComm(m_hCom,1024,1024);   
  266.     SetCommMask(m_hCom, EV_RXCHAR);///////////////////!!!!!!!!!!!    
  267.     // 把间隔超时设为最大,把总超时设为0将导致ReadFile立即返回并完成操作    
  268.     TimeOuts.ReadIntervalTimeout=MAXDWORD;    
  269.     TimeOuts.ReadTotalTimeoutMultiplier=0;    
  270.     TimeOuts.ReadTotalTimeoutConstant=0;    
  271.     /* 设置写超时以指定WriteComm成员函数中的  
  272.     GetOverlappedResult函数的等待时间*/   
  273.     TimeOuts.WriteTotalTimeoutMultiplier=50;    
  274.     TimeOuts.WriteTotalTimeoutConstant=2000;   
  275.     SetCommTimeouts(m_hCom, &TimeOuts);   
  276.     if(ConfigConnection())   
  277.     {   
  278.         m_pThread=AfxBeginThread(CommProc, this, THREAD_PRIORITY_NORMAL,    
  279.             0, CREATE_SUSPENDED, NULL); // 创建并挂起线程    
  280.         if(m_pThread==NULL)   
  281.         {   
  282.             CloseHandle(m_hCom);   
  283.             return FALSE;   
  284.         }   
  285.         else   
  286.         {   
  287.             m_bConnected=TRUE;   
  288.             m_pThread->ResumeThread(); // 恢复线程运行    
  289.         }   
  290.     }   
  291.     else   
  292.     {   
  293.         CloseHandle(m_hCom);   
  294.         return FALSE;   
  295.     }   
  296.     return TRUE;   
  297. }   
  298. void CRobotDoc::CloseConnection()   
  299. {   
  300.     if(!m_bConnected) return;   
  301.     m_bConnected=FALSE;   
  302.     //结束CommProc线程中WaitSingleObject函数的等待    
  303.     SetEvent(m_hPostMsgEvent);    
  304.     //结束CommProc线程中WaitCommEvent的等待    
  305.     SetCommMask(m_hCom, 0);    
  306.     //等待辅助线程终止    
  307.     WaitForSingleObject(m_pThread->m_hThread, INFINITE);   
  308.     m_pThread=NULL;   
  309.     CloseHandle(m_hCom);   
  310. }   
  311. BOOL CRobotDoc::ConfigConnection()///设置DCB    
  312. {   
  313.     DCB dcb;   
  314.     if(!GetCommState(m_hCom, &dcb))   
  315.         return FALSE;   
  316.     dcb.fBinary=TRUE;   
  317.     dcb.BaudRate=m_nBaud; // 波特率    
  318.     dcb.ByteSize=m_nDataBits; // 每字节位数    
  319.     dcb.fParity=TRUE;   
  320.     switch(m_nParity) // 校验设置    
  321.     {   
  322.     case 0: dcb.Parity=NOPARITY;   
  323.         break;   
  324.     case 1: dcb.Parity=EVENPARITY;   
  325.         break;   
  326.     case 2: dcb.Parity=ODDPARITY;   
  327.         break;   
  328.     default:;   
  329.     }   
  330.     switch(m_nStopBits) // 停止位    
  331.     {   
  332.     case 0: dcb.StopBits=ONESTOPBIT;   
  333.         break;   
  334.     case 1: dcb.StopBits=ONE5STOPBITS;   
  335.         break;   
  336.     case 2: dcb.StopBits=TWOSTOPBITS;   
  337.         break;   
  338.     default:;   
  339.     }   
  340.     // 硬件流控制设置    
  341. //  dcb.fOutxCtsFlow=m_nFlowCtrl==1;    
  342. //  dcb.fRtsControl=m_nFlowCtrl==1?RTS_CONTROL_HANDSHAKE:RTS_CONTROL_ENABLE;    
  343.     // XON/XOFF流控制设置    
  344. //  dcb.fInX=dcb.fOutX=m_nFlowCtrl==2;    
  345. //  dcb.XonChar=XON;    
  346. //  dcb.XoffChar=XOFF;    
  347. //  dcb.XonLim=50;    
  348. //  dcb.XoffLim=50;    
  349.     return SetCommState(m_hCom, &dcb);   
  350. }   
  351. DWORD CRobotDoc::ReadComm(unsigned char *buf,DWORD dwLength)   
  352. {   
  353.     DWORD length=0;   
  354.     COMSTAT ComStat;   
  355.     DWORD dwErrorFlags;   
  356.     ClearCommError(m_hCom,&dwErrorFlags,&ComStat); // 清除错误标志    
  357.     length=min(dwLength, ComStat.cbInQue);   
  358.     ReadFile(m_hCom,buf,length,&length,&m_osRead);// 将指定数量的字符从串行口输出    
  359.     return length;   
  360. }   
  361. // 将指定数量的字符从串行口输出    
  362. DWORD CRobotDoc::WriteComm(unsigned char *buf,DWORD dwLength)   
  363. {   
  364.     BOOL fState;   
  365.     DWORD length=dwLength;   
  366.     COMSTAT ComStat;   
  367.     DWORD dwErrorFlags;   
  368.     ClearCommError(m_hCom,&dwErrorFlags,&ComStat); // 清除错误标志    
  369.     fState=WriteFile(m_hCom,buf,length,&length,&m_osWrite);   
  370.     if(!fState){   
  371.         if(GetLastError()==ERROR_IO_PENDING)   
  372.         {   
  373.             GetOverlappedResult(m_hCom,&m_osWrite,&length,TRUE);// 等待    
  374.         }   
  375.         else   
  376.             length=0;   
  377.     }   
  378.     return length;   
  379. }   
  380. // 工作者线程,负责监视串行口    
  381.    
  382. void CRobotDoc::OnHelp()    
  383. {   
  384.     // TODO: Add your command handler code here    
  385.       //ShellExecute(NULL,NULL,_T("Jqrhelp.chm"),NULL,_T("e://机器人"),NULL);    
  386. }   
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/fdgugfv/article/details/83587788