分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
- 视图类,废话少说,看看带注释的源码
- #include "stdafx.h"
- #include "robot.h"
- #include"Label.h"
- #include "robotDoc.h"
- #include "robotView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /
- // CRobotView
- IMPLEMENT_DYNCREATE(CRobotView, CFormView)
- BEGIN_MESSAGE_MAP(CRobotView, CFormView)
- //{{AFX_MSG_MAP(CRobotView)
- ON_WM_CTLCOLOR()
- ON_WM_TIMER()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_COMMNOTIFY, OnCommNotify)
- END_MESSAGE_MAP()
- /
- // CRobotView construction/destruction
- CRobotView::CRobotView()
- : CFormView(CRobotView::IDD)
- {
- //{{AFX_DATA_INIT(CRobotView)
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- m_ncomand1=10;
- m_bflag=0;
- m_nsudu=1;
- m_ngzq=0;
- m_nguanjie;
- m_nfangxiang;
- m_nmsg[0]=0;
- m_njishu=0;
- m_nmsg[1]=0;
- m_nmsg[2]=0;
- m_nmsg[3]=0;
- m_nmsg[4]=0;
- m_nsum=0;
- m_ndelay=0;
- nLength=0;
- m_recflag=1;
- m_failflag=1;
- m_brush.CreateSolidBrush(RGB(40,98,122)); // 生成一绿色刷子
- }
- CRobotView::~CRobotView()
- {
- }
- void CRobotView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CRobotView)
- //DDX_Control(pDX, IDC_EDIT1, m_edit1);
- DDX_Control(pDX, IDC_STATIC_LINK, m_link);
- DDX_Control(pDX, IDC_STATIC_1, m_ml);
- DDX_Control(pDX, IDC_STATIC_4, m_workpart);
- DDX_Control(pDX, IDC_STATIC_COMMAND, m_command);
- DDX_Control(pDX, IDC_STATIC_STATUS, m_static);
- DDX_Control(pDX, IDC_STATIC_BUCHANG, m_buchangshu);
- DDX_Control(pDX, IDC_STATIC_5, m_buchang);
- DDX_Control(pDX, IDC_STATIC_3, m_sudu);
- DDX_Control(pDX, IDC_STATIC_2, m_zhuangtai);
- //}}AFX_DATA_MAP
- }
- BOOL CRobotView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- /
- // CRobotView diagnostics
- #ifdef _DEBUG
- void CRobotView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CRobotView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CRobotDoc* CRobotView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRobotDoc)));
- return (CRobotDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /
- // CRobotView message handlers
- void CRobotView::OnDraw(CDC* pDC)
- {
- // TODO: Add your specialized code here and/or call the base class
- SuduDisplay(m_nsudu);
- GzqDisplay(m_ngzq);
- }
- void CRobotView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- // TODO: Add your specialized code here and/or call the base class
- m_link.SetLink(TRUE)
- .SetTextColor(RGB(0,0,255))
- .SetFontUnderline(TRUE)
- .SetLinkCursor(AfxGetApp()->LoadCursor(IDC_ICON1));
- m_command.SetFontSize(24)
- .SetFontName("Terminal")
- .SetTextColor(RGB(240,240,40));
- m_static.SetFontSize(24)
- .SetTextColor(RGB(240,240,40));
- m_workpart.SetFontSize(24)
- .SetFontBold(TRUE)
- .SetTextColor(RGB(0,240,0));
- m_buchangshu.SetFontSize(24)
- .SetTextColor(RGB(240,240,40));
- m_buchang.SetFontSize(24)
- .SetFontBold(TRUE)
- .SetTextColor(RGB(0,240,0));
- m_sudu.SetFontSize(24)
- .SetFontBold(TRUE)
- .SetTextColor(RGB(0,240,0));
- m_zhuangtai.SetFontSize(24)
- .SetFontBold(TRUE)
- .SetTextColor(RGB(0,240,0));
- m_ml.SetFontSize(24)
- .SetLinkCursor(AfxGetApp()->LoadCursor(IDC_ICON1))
- .SetFontBold(TRUE)
- .SetTextColor(RGB(0,240,0));
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
- }
- LRESULT CRobotView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(message == (WM_USER + 1002))
- ProcessMessage(wParam, lParam);
- else if(message==(WM_USER+1003))
- ProcessMyMessage();
- return CFormView::WindowProc(message, wParam, lParam);
- }
- void CRobotView::ProcessMessage(WPARAM wParam, LPARAM lParam)
- {
- CRobotDoc* pDoc=GetDocument();
- char* l[57] = {"零","一","二",///012
- "三","四","五","六","七","八","九","执行","各关节归零","大臂正转",///12
- "大臂反转","小臂正转","小臂反转","手腕正转","手腕反转","手爪张开","手爪闭合","上升","下降","大臂运行",///13--22
- "大臂负运行","小臂运行","小臂负运行","手腕运行","手腕负运行","手爪运行","手爪负运行","升降运行",23---30
- "升降负运行","大臂归零","小臂归零","手腕归零","手爪归零","升降归零","速度加","原点记忆",///31---38
- "系统复位","示教盒","","","回原点","速度减","区号加","区号减","单步再现","周期再现",///39---48
- "连续再现","返回","继续","数据记忆","读取数据","设置串口","打开串口","关闭串口"};
- CString strcomand;
- CString strstatus;
- m_recflag=1;
- if(!pDoc->m_bConnected)
- {
- KillTimer(1);
- MessageBox("请先打开串口");
- return;
- }
- if(wParam==100)
- {
- KillTimer(1);
- if(!m_failflag)
- {
- CWnd* pWnd=GetDlgItem(IDC_STATIC_5);
- pWnd->SetWindowText("");
- pWnd=GetDlgItem(IDC_STATIC_BUCHANG);
- pWnd->SetWindowText("");
- pWnd=GetDlgItem(IDC_STATIC_COMMAND);
- pWnd->SetWindowText("请输入新的命令");
- pWnd=GetDlgItem(IDC_STATIC_STATUS);
- pWnd->SetWindowText("执行完毕");
- m_ncomand1=0;
- }
- else
- MessageBox("请检查连线是否正确或下位机是否打开","通信失败",MB_ICONWARNING);
- }
- else if(wParam>=11&&wParam<=56)
- {
- m_bflag=0;
- m_ncomand1=wParam;
- strcomand=l[m_ncomand1];
- CWnd* pWnd=GetDlgItem(IDC_STATIC_COMMAND);
- pWnd->SetWindowText(strcomand);
- pWnd=GetDlgItem(IDC_STATIC_STATUS);
- pWnd->SetWindowText("");
- pWnd=GetDlgItem(IDC_STATIC_5);
- pWnd->SetWindowText("");
- pWnd=GetDlgItem(IDC_STATIC_BUCHANG);
- pWnd->SetWindowText("");
- m_nsum=0;
- m_njishu=0;
- }
- else if(wParam>=0&&wParam<=9)
- {
- if(m_ncomand1>=12&&m_ncomand1<=21)
- {
- m_bflag=1;
- m_nmsg[m_njishu]=wParam;
- //pWnd=GetDlgItem(IDC_STATIC_COMMAND);
- //pWnd->SetWindowText(strcomand);
- CWnd* pWnd=GetDlgItem(IDC_STATIC_5);
- pWnd->SetWindowText("步长:");
- ++m_njishu;
- if(m_njishu==1)
- {
- CString m_stemp;
- m_nsum=m_nmsg[0];
- m_stemp.Format("%d",m_nsum);
- pWnd=GetDlgItem(IDC_STATIC_BUCHANG);
- pWnd->SetWindowText(m_stemp);
- }
- if(m_njishu==2)
- {
- CString m_stemp;
- m_nsum=m_nmsg[0]*10+m_nmsg[1];
- m_stemp.Format("%d",m_nsum);
- pWnd=GetDlgItem(IDC_STATIC_BUCHANG);
- pWnd->SetWindowText(m_stemp);
- }
- if(m_njishu==3)
- {
- CString m_stemp;
- m_nsum=m_nmsg[0]*100+m_nmsg[1]*10+m_nmsg[2];
- //m_nmsg[0]=0;
- //m_nmsg[1]=0
- // m_nmsg[2]=0;
- // m_njishu=0;
- m_stemp.Format("%d",m_nsum);
- pWnd=GetDlgItem(IDC_STATIC_BUCHANG);
- pWnd->SetWindowText(m_stemp);
- }
- if(m_njishu==4)
- {
- CString m_stemp;
- m_nsum=m_nmsg[0]*1000+m_nmsg[1]*100+m_nmsg[2]*10+m_nmsg[3];
- m_stemp.Format("%d",m_nsum);
- pWnd=GetDlgItem(IDC_STATIC_BUCHANG);
- pWnd->SetWindowText(m_stemp);
- }
- if(m_njishu>4)
- {
- m_njishu=1;
- m_nmsg[0]=m_nmsg[4];
- CString m_stemp;
- m_nsum=m_nmsg[0];
- m_stemp.Format("%d",m_nsum);
- pWnd=GetDlgItem(IDC_STATIC_BUCHANG);
- pWnd->SetWindowText(m_stemp);
- m_nmsg[1]=0;
- m_nmsg[2]=0;
- m_nmsg[3]=0;
- }
- }
- else
- ; //MessageBox("清先输入运动步数命令");
- }
- else if(wParam=10)
- { SetTimer(1,500,NULL);
- if(m_ncomand1>=11&&m_ncomand1<=56)
- {
- if(!m_bflag)
- {
- strstatus="正在执行命令,请稍后.....";
- CWnd* pWnd=GetDlgItem(IDC_STATIC_STATUS);
- pWnd->SetWindowText(strstatus);
- Execute(m_ncomand1);
- }
- else //if(m_ncomand1>=12&&m_ncomand1<=21)
- {
- unsigned char m_uhi,m_ulo;
- m_nmsg[0]=0;
- m_nmsg[1]=0;
- m_nmsg[2]=0;
- m_nmsg[3]=0;
- m_njishu=0;
- /// CWnd* pWnd=GetDlgItem(IDC_STATIC_STATUS);
- /// pWnd->SetWindowText("正在执行命令,请稍后.....");
- switch(m_ncomand1)
- {
- case(12):
- m_nguanjie=01;
- m_nfangxiang=00;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/m_nsudu;
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(13):
- m_nguanjie=0x01;
- m_nfangxiang=0xff;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/m_nsudu;
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(14):
- m_nguanjie=0x02;
- m_nfangxiang=0x00;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum)/(m_nsudu*3);
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(15):
- m_nguanjie=0x02;
- m_nfangxiang=0xff;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum)/(m_nsudu*3);
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(16):
- m_nguanjie=0x04;
- m_nfangxiang=0x00;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/m_nsudu;
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(17):
- m_nguanjie=0x04;
- m_nfangxiang=0xff;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/m_nsudu;
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(18):
- m_nguanjie=0x05;
- m_nfangxiang=0x00;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/m_nsudu;
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(19):
- m_nguanjie=0x05;
- m_nfangxiang=0xff;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/m_nsudu;
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(20):
- m_nguanjie=0x03;
- m_nfangxiang=0x00;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/(m_nsudu*2);
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- case(21):
- m_nguanjie=0x03;
- m_nfangxiang=0xff;
- m_uhi=(unsigned char)(m_nsum/256);
- m_ulo=(unsigned char)(m_nsum-m_uhi*256);
- m_ndelay=(m_nsum+500)/(m_nsudu*2);
- Execute_1(m_nguanjie,m_nfangxiang,m_uhi,m_ulo,m_ndelay);
- break;
- }
- }
- m_failflag=1;}
- else
- MessageBox("清先输入命令");
- }
- else
- MessageBox("不能识别,请重新输入命令");
- }
- void CRobotView::ProcessMyMessage()
- {
- CDC *PDc=GetDC();
- m_failflag=0;
- switch(m_ncomand1)
- {
- case(37):
- {
- m_nsudu++;
- if(m_nsudu==6)
- m_nsudu=1;
- OnDraw(PDc);
- }
- break;
- case(44):
- {
- m_nsudu--;
- if(m_nsudu==0)
- m_nsudu=5;
- OnDraw(PDc);
- }
- break;
- case(45):
- {
- m_ngzq++;
- if(m_ngzq==8)
- m_ngzq=0;
- OnDraw(PDc);
- }
- break;
- case(46):
- {
- m_ngzq--;
- if(m_ngzq==-1)
- m_ngzq=7;
- OnDraw(PDc);
- }
- break;
- }
- CWnd* pWnd=GetDlgItem(IDC_STATIC_COMMAND);
- pWnd->SetWindowText("请输入新的命令");
- pWnd=GetDlgItem(IDC_STATIC_STATUS);
- pWnd->SetWindowText("执行完毕");
- m_ncomand1=0;
- //}
- //else
- // MessageBox("请检查连线是否正确或下位机是否打开","通信失败",MB_ICONWARNING);
- }
- void CRobotView::SuduDisplay(int n)
- {
- CWnd* pWnd=GetDlgItem(IDC_STATIC_SUDU);
- CDC* pControlDC=pWnd->GetDC();
- pWnd->Invalidate();
- pWnd->UpdateWindow();
- pControlDC->SelectStockObject(BLACK_BRUSH);
- CBitmap mBit;
- CDC MemDC;
- MemDC.CreateCompatibleDC(NULL);
- switch(n)
- {
- case 0:
- mBit.LoadBitmap(IDB_BMP10);
- break;
- case 1:
- mBit.LoadBitmap(IDB_BMP11);
- break;
- case 2:
- mBit.LoadBitmap(IDB_BMP12);
- break;
- case 3:
- mBit.LoadBitmap(IDB_BMP13);
- break;
- case 4:
- mBit.LoadBitmap(IDB_BMP14);
- break;
- case 5:
- mBit.LoadBitmap(IDB_BMP15);
- break;
- default:
- //mBit.LoadBitmap(IDB_BMP0);
- MessageBox("error");
- }
- CBitmap *pOldBit=MemDC.SelectObject(&mBit);
- pControlDC->BitBlt(0,0,900,700,&MemDC,0,0,SRCCOPY);
- MemDC.SelectObject(pOldBit);
- }
- void CRobotView::GzqDisplay(int n)
- {
- CWnd* pWnd=GetDlgItem(IDC_STATIC_GONGZUOQU);
- CDC* pControlDC=pWnd->GetDC();
- pWnd->Invalidate();
- pWnd->UpdateWindow();
- pControlDC->SelectStockObject(BLACK_BRUSH);
- CBitmap mBit;
- CDC MemDC;
- MemDC.CreateCompatibleDC(NULL);
- switch(n)
- {
- case 0:
- mBit.LoadBitmap(IDB_BMP10);
- break;
- case 1:
- mBit.LoadBitmap(IDB_BMP11);
- break;
- case 2:
- mBit.LoadBitmap(IDB_BMP12);
- break;
- case 3:
- mBit.LoadBitmap(IDB_BMP13);
- break;
- case 4:
- mBit.LoadBitmap(IDB_BMP14);
- break;
- case 5:
- mBit.LoadBitmap(IDB_BMP15);
- break;
- case 6:
- mBit.LoadBitmap(IDB_BMP16);
- break;
- case 7:
- mBit.LoadBitmap(IDB_BMP17);
- break;
- default:
- //mBit.LoadBitmap(IDB_BMP0);
- MessageBox("error");
- }
- CBitmap *pOldBit=MemDC.SelectObject(&mBit);
- pControlDC->BitBlt(0,0,900,700,&MemDC,0,0,SRCCOPY);
- MemDC.SelectObject(pOldBit);
- }
- void CRobotView::Execute(int n)
- {
- CRobotDoc* pDoc=GetDocument();
- unsigned char send1;
- switch(n)
- {
- case(11):
- home();
- break;
- case(12):
- {//大臂正传
- unsigned char send[6]={0x53,0x01,0x00,0x00,0x07,0x18};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000/m_nsudu);
- break;
- }
- case(13):
- {
- unsigned char send[6]={0x53,0x01,0xff,0x00,0x07,0x18};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- // pDoc->ReadComm(&buf,1);
- // if(nLength)
- {
- // str.Format("%d",buf);
- // m_edit1.SetSel(-1, 0);
- // m_edit1.ReplaceSel(str); // 向编辑视图中插入收到的字符
- // }
- // else
- // MessageBox("fjdklfjsdklgjsdfkl");
- Sleep(2000/m_nsudu);
- break;
- }
- case(14)://xiaobizhengzhuan
- {
- unsigned char send[6]={0x53,0x02,0x00,0x00,0x0f,0xe3};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000/m_nsudu);
- break;
- }
- case(15)://小臂反转
- {
- unsigned char send[6]={0x53,0x02,0xff,0x00,0x0f,0xe3};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000/m_nsudu);
- break;
- }
- case(16)://手腕正转
- {
- unsigned char send[6]={0x53,0x04,0x00,0x00,0x00,0xff};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(1000/m_nsudu);
- break;
- }
- case(17)://shouwanfanzhuan
- {
- unsigned char send[6]={0x53,0x04,0xff,0x00,0x00,0xff};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(1000/m_nsudu);
- }
- break;
- case(18)://shouzhuazhangkai
- {
- unsigned char send[6]={0x53,0x05,0x00,0x00,0x00,0x43};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000/m_nsudu);
- }
- break;
- case(19):
- {
- unsigned char send[6]={0x53,0x05,0xff,0x00,0x00,0xf3};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000/m_nsudu);
- }
- break;
- case(20)://shangsheng
- {
- unsigned char send[6]={0x53,0x03,0x00,0x00,0x02,0xe3};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(1000/m_nsudu);
- }
- break;
- case(21):
- {
- unsigned char send[6]={0x53,0x03,0xff,0x00,0x02,0xe3};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(1000/m_nsudu);
- }
- break;
- /* case(22)://dabiyunxing
- {
- unsigned char send[3]={0x63,0x01,0x00};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(23):
- {
- unsigned char send[3]={0x63,0x01,0xff};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(24)://xiaobiyunxing
- {
- unsigned char send[3]={0x63,0x02,0x00};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(25):
- {
- unsigned char send[3]={0x63,0x02,0xff};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(26)://shouwanyunxing
- {
- unsigned char send[3]={0x63,0x04,0x00};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(27):
- {
- unsigned char send[3]={0x63,0x04,0xff};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(28)://shouzhuayunxing
- {
- unsigned char send[3]={0x63,0x05,0x00};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(29):
- {
- unsigned char send[3]={0x63,0x05,0xff};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(30)://shengjiangyunxing
- {
- unsigned char send[3]={0x63,0x03,0x00};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- case(31):
- {
- unsigned char send[3]={0x63,0x05,0xff};
- for(int i=0;i<=2;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(2000);
- }
- break;
- */
- case(32)://dabiguiling
- {
- unsigned char send[2]={0xa3,0x01};
- for(int i=0;i<=1;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(18000);
- }
- break;
- case(33)://xiaobiguiling
- {
- unsigned char send[2]={0xa3,0x02};
- for(int i=0;i<=1;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(15000);
- }
- break;
- case(34)://shouwanguiling
- {
- unsigned char send[2]={0xa3,0x04};
- for(int i=0;i<=1;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(6000);
- }
- break;
- case(35)://shouzhuaguiling
- {
- unsigned char send[2]={0xa3,0x05};
- for(int i=0;i<=1;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(6000);
- }
- break;
- case(36)://shengjiangguiling
- {
- unsigned char send[2]={0xa3,0x03};
- for(int i=0;i<=1;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(10000);
- }
- break;
- case(37)://sudujia
- {
- send1=0xb3;
- pDoc->WriteComm(&send1,1);
- }
- Sleep(1000);
- break;
- case(38)://yuandianjiyi
- send1=0x73;
- pDoc->WriteComm(&send1,1);
- break;
- case(39):
- send1=0xc3;
- pDoc->WriteComm(&send1,1);
- break;
- case(40):
- send1=0xf6;
- pDoc->WriteComm(&send1,1);
- break;
- case(41):
- send1=0x83;
- pDoc->WriteComm(&send1,1);
- break;
- case(42):
- send1=0x93;
- pDoc->WriteComm(&send1,1);
- break;
- case(43):
- // {
- // unsigned char send[2]={0x86,0x03};
- // for(int i=0;i<=1;i++)
- // pDoc->WriteComm(&send[i],1);
- // Sleep(4000);
- // }
- send1=0x86;
- pDoc->WriteComm(&send1,1);
- break;
- case(44)://sudujian
- {
- send1=0xe3;
- pDoc->WriteComm(&send1,1);
- }
- break;
- case(45):
- {
- send1=0x12;
- pDoc->WriteComm(&send1,1);
- }
- break;
- case(46):
- {
- send1=0x22;
- pDoc->WriteComm(&send1,1);
- }
- break;
- case(47): //danbuzaixian
- send1=0x13;
- pDoc->WriteComm(&send1,1);
- Sleep(4000);
- break;
- case(48): //zhouqizaixian
- send1=0x23;
- pDoc->WriteComm(&send1,1);
- Sleep(8000);
- break;
- case(49): //lianxuzaixian
- send1=0x33;
- pDoc->WriteComm(&send1,1);
- break;
- case(50):
- //send1=0x32;
- //pDoc->WriteComm(&send1,1);
- break;
- case(51):
- break;
- case(52):
- send1=0x43;
- pDoc->WriteComm(&send1,1);
- break;
- case(53):
- break;
- case(54):
- pDoc->OnSetupcomm();
- break;
- case(55):
- pDoc->OnOpencomm();
- break;
- default:
- pDoc->OnClosecomm();
- break;
- }
- }
- //void CRobotView::OnButton3()
- //{
- // TODO: Add your control notification handler code here
- // SendMessage(WM_USER + 1002,10,0);
- //}
- void CRobotView::Execute_1(unsigned char a,unsigned char b,unsigned char c,unsigned char d,int e)
- {
- CRobotDoc* pDoc=GetDocument();
- unsigned char send[6]={0x53,a,b,0x00,c,d};
- for(int i=0;i<=5;i++)
- pDoc->WriteComm(&send[i],1);
- Sleep(m_ndelay);
- }
- HBRUSH CRobotView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
- // TODO: Change any attributes of the DC here
- switch(nCtlColor)
- {
- case CTLCOLOR_DLG:
- {
- //pDC->SetBkMode(TRANSPARENT);
- return m_brush; //返加绿色刷子
- }
- case CTLCOLOR_STATIC:
- {
- //返加绿色刷子
- // pDC->SetBkMode(TRANSPARENT);
- return m_brush;
- }
- }
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }
- void CRobotView::home()
- {
- SendMessage(WM_USER+1002,32,0);
- SendMessage(WM_USER+1002,10,0);
- Sleep(1000);
- SendMessage(WM_USER+1002,33,0);
- SendMessage(WM_USER+1002,10,0);
- Sleep(1000);
- SendMessage(WM_USER+1002,34,0);
- SendMessage(WM_USER+1002,10,0);
- Sleep(1000);
- SendMessage(WM_USER+1002,35,0);
- SendMessage(WM_USER+1002,10,0);
- Sleep(1000);
- SendMessage(WM_USER+1002,36,0);
- SendMessage(WM_USER+1002,10,0);
- Sleep(1000);
- }
- LRESULT CRobotView::OnCommNotify(WPARAM wParam, LPARAM lParam)
- {
- unsigned char buf;
- CWnd* pWnd=GetDlgItem(IDC_STATIC_COMMAND);
- CString str1,str2;
- CRobotDoc* pDoc=GetDocument();
- m_recflag=0;
- if(!pDoc->m_bConnected ||
- (wParam & EV_RXCHAR)!=EV_RXCHAR) // 是否是EV_RXCHAR事件?
- {
- SetEvent(pDoc->m_hPostMsgEvent); // 允许发送下一个WM_COMMNOTIFY消息
- return 0L;
- }
- nLength=pDoc->ReadComm(&buf,1);
- if(nLength)
- {
- // str1.Format("%d",buf);
- //m_edit1.SetSel(-1, 0);
- // m_edit1.ReplaceSel(str1); // 向编辑视图中插入收到的字符
- str2="正在执行命令,请稍后.....";
- pWnd->SetWindowText(str2);
- SendMessage(WM_USER+1003);
- }
- SetEvent(pDoc->m_hPostMsgEvent); // 允许发送下一个WM_COMMNOTIFY消息
- return 0L;
- }
- void CRobotView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- SendMessage(WM_USER+1002,100);
- CFormView::OnTimer(nIDEvent);
- }
文档类
- #include "stdafx.h"
- #include "robot.h"
- #include"SetupDlg.h"
- #include "robotDoc.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /
- // CRobotDoc
- IMPLEMENT_DYNCREATE(CRobotDoc, CDocument)
- BEGIN_MESSAGE_MAP(CRobotDoc, CDocument)
- //{{AFX_MSG_MAP(CRobotDoc)
- ON_COMMAND(ID_SETUPCOMM, OnSetupcomm)
- ON_COMMAND(ID_CLOSECOMM, OnClosecomm)
- ON_COMMAND(ID_OPENCOMM, OnOpencomm)
- ON_UPDATE_COMMAND_UI(ID_OPENCOMM, OnUpdateOpencomm)
- ON_UPDATE_COMMAND_UI(ID_CLOSECOMM, OnUpdateClosecomm)
- ON_COMMAND(ID_ANJIANCTRL, OnAnjianctrl)
- ON_UPDATE_COMMAND_UI(ID_ANJIANCTRL, OnUpdateAnjianctrl)
- ON_COMMAND(ID_VOICECTRL, OnVoicectrl)
- ON_UPDATE_COMMAND_UI(ID_VOICECTRL, OnUpdateVoicectrl)
- ON_COMMAND(ID_HELP, OnHelp)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /
- // CRobotDoc construction/destruction
- CRobotDoc::CRobotDoc()
- {
- // TODO: add one-time construction code here
- m_bConnected=FALSE;
- m_pThread=NULL;
- m_nBaud =244;
- m_nDataBits = 8;
- m_nParity = 1;
- m_sPort = "COM1";
- m_nStopBits = 0;
- m_bctrlmodal=FALSE;
- m_uCurrentBtn=ID_CLOSECOMM;
- }
- CRobotDoc::~CRobotDoc()
- {
- if(m_bConnected)
- CloseConnection();
- // 删除事件句柄
- if(m_hPostMsgEvent)
- CloseHandle(m_hPostMsgEvent);
- if(m_osRead.hEvent)
- CloseHandle(m_osRead.hEvent);
- if(m_osWrite.hEvent)
- CloseHandle(m_osWrite.hEvent);
- }
- BOOL CRobotDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- // 为WM_COMMNOTIFY消息创建事件对象,手工重置,初始化为有信号的
- if((m_hPostMsgEvent=CreateEvent(NULL, TRUE, TRUE, NULL))==NULL)
- return FALSE;
- memset(&m_osRead, 0, sizeof(OVERLAPPED));
- memset(&m_osWrite, 0, sizeof(OVERLAPPED));
- // 为重叠读创建事件对象,手工重置,初始化为无信号的
- if((m_osRead.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
- return FALSE;
- // 为重叠写创建事件对象,手工重置,初始化为无信号的
- if((m_osWrite.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
- return FALSE;
- return TRUE;
- }
- /
- // CRobotDoc serialization
- void CRobotDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
- /
- // CRobotDoc diagnostics
- #ifdef _DEBUG
- void CRobotDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CRobotDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /
- // CRobotDoc commands
- void CRobotDoc::OnSetupcomm()
- {
- // TODO: Add your command handler code here
- CSetupDlg dlg;
- CString str;
- dlg.m_bConnected=m_bConnected;
- dlg.m_sPort=m_sPort;
- str.Format("%d",m_nBaud);
- dlg.m_sBaud=str;
- str.Format("%d",m_nDataBits);
- dlg.m_sDataBits=str;
- dlg.m_nParity=m_nParity;
- dlg.m_nStopBits=m_nStopBits;
- //dlg.m_nFlowCtrl=m_nFlowCtrl;
- //dlg.m_bEcho=m_bEcho;
- // dlg.m_bNewLine=m_bNewLine;
- if(dlg.DoModal()==IDOK)
- {
- m_sPort=dlg.m_sPort;
- m_nBaud=atoi(dlg.m_sBaud);
- m_nDataBits=atoi(dlg.m_sDataBits);
- m_nParity=dlg.m_nParity;
- m_nStopBits=dlg.m_nStopBits;
- // m_nFlowCtrl=dlg.m_nFlowCtrl;
- // m_bEcho=dlg.m_bEcho;
- // m_bNewLine=dlg.m_bNewLine;
- if(m_bConnected)
- if(!ConfigConnection())
- AfxMessageBox("Can't realize the settings!");
- }
- }
- void CRobotDoc::OnClosecomm()
- {
- // TODO: Add your command handler code here
- m_uCurrentBtn=ID_CLOSECOMM;
- CloseConnection();
- }
- void CRobotDoc::OnOpencomm()
- {
- // TODO: Add your command handler code here
- m_uCurrentBtn=ID_OPENCOMM;
- if(!OpenConnection())
- AfxMessageBox("Can't open connection");
- }
- void CRobotDoc::OnUpdateOpencomm(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- //pCmdUI->Enable(!m_bConnected);
- pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);
- }
- void CRobotDoc::OnUpdateClosecomm(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- // pCmdUI->Enable(m_bConnected);
- pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);
- }
- void CRobotDoc::OnAnjianctrl()
- {
- // TODO: Add your command handler code here
- }
- void CRobotDoc::OnUpdateAnjianctrl(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- }
- void CRobotDoc::OnVoicectrl()
- {
- // TODO: Add your command handler code here
- // WinExec(NULL,NULL,_T("dutty.exe"),NULL,_T("D://Program Files//Dutty//Dutty.exe"),NULL);
- WinExec(_T("D://Program Files//Dutty//Dutty.exe"),SW_SHOW);
- m_bctrlmodal=TRUE;
- //STARTUPINFO stinfo; //启动窗口的信息
- //PROCESSINFO procinfo; //进程的信息
- //CreateProcess(NULL,_T("dutty.exe"),NULL,NULL.FALSE, NORMAL_PRIORITY_ CLASS,NULL,NULL, &stinfo,&procinfo);
- }
- void CRobotDoc::OnUpdateVoicectrl(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->Enable(!m_bctrlmodal);
- }
- UINT CommProc(LPVOID pParam)
- {
- OVERLAPPED os;
- DWORD dwMask, dwTrans;
- COMSTAT ComStat;
- DWORD dwErrorFlags;
- CRobotDoc *pDoc=(CRobotDoc*)pParam;
- memset(&os, 0, sizeof(OVERLAPPED));
- os.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
- if(os.hEvent==NULL)
- {
- AfxMessageBox("Can't create event object!");
- return (UINT)-1;
- }
- while(pDoc->m_bConnected)
- {
- ClearCommError(pDoc->m_hCom,&dwErrorFlags,&ComStat);
- if(ComStat.cbInQue) //查询输入缓冲区中是否有字符 ,若有
- {
- // 等待WM_COMMNOTIFY消息被处理完
- WaitForSingleObject(pDoc->m_hPostMsgEvent, INFINITE);
- ResetEvent(pDoc->m_hPostMsgEvent);
- PostMessage(pDoc->m_hTermWnd, WM_COMMNOTIFY, EV_RXCHAR, 0);
- // 通知视图
- continue;
- }
- dwMask=0;
- if(!WaitCommEvent(pDoc->m_hCom, &dwMask, &os)) // 重叠操作
- {
- //通信事件
- if(GetLastError()==ERROR_IO_PENDING)
- // 无限等待重叠操作结果
- GetOverlappedResult(pDoc->m_hCom, &os, &dwTrans, TRUE);
- else
- {
- CloseHandle(os.hEvent);
- return (UINT)-1;
- }
- }
- }
- CloseHandle(os.hEvent);
- return 0;
- }
- BOOL CRobotDoc::OpenConnection()
- {
- COMMTIMEOUTS TimeOuts;
- POSITION firstViewPos;
- CView *pView;
- firstViewPos=GetFirstViewPosition();
- pView=GetNextView(firstViewPos);
- m_hTermWnd=pView->GetSafeHwnd();
- if(m_bConnected)
- return FALSE;
- m_hCom=CreateFile(m_sPort, GENERIC_READ|GENERIC_WRITE,0,NULL,
- OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
- NULL); // 重叠方式
- if(m_hCom==INVALID_HANDLE_VALUE)
- //AfxMessageBox("dfhksdjfhsjkfhks",MB_OK);
- return FALSE;
- SetupComm(m_hCom,1024,1024);
- SetCommMask(m_hCom, EV_RXCHAR);///!!!!!!!!!!!
- // 把间隔超时设为最大,把总超时设为0将导致ReadFile立即返回并完成操作
- TimeOuts.ReadIntervalTimeout=MAXDWORD;
- TimeOuts.ReadTotalTimeoutMultiplier=0;
- TimeOuts.ReadTotalTimeoutConstant=0;
- /* 设置写超时以指定WriteComm成员函数中的
- GetOverlappedResult函数的等待时间*/
- TimeOuts.WriteTotalTimeoutMultiplier=50;
- TimeOuts.WriteTotalTimeoutConstant=2000;
- SetCommTimeouts(m_hCom, &TimeOuts);
- if(ConfigConnection())
- {
- m_pThread=AfxBeginThread(CommProc, this, THREAD_PRIORITY_NORMAL,
- 0, CREATE_SUSPENDED, NULL); // 创建并挂起线程
- if(m_pThread==NULL)
- {
- CloseHandle(m_hCom);
- return FALSE;
- }
- else
- {
- m_bConnected=TRUE;
- m_pThread->ResumeThread(); // 恢复线程运行
- }
- }
- else
- {
- CloseHandle(m_hCom);
- return FALSE;
- }
- return TRUE;
- }
- void CRobotDoc::CloseConnection()
- {
- if(!m_bConnected) return;
- m_bConnected=FALSE;
- //结束CommProc线程中WaitSingleObject函数的等待
- SetEvent(m_hPostMsgEvent);
- //结束CommProc线程中WaitCommEvent的等待
- SetCommMask(m_hCom, 0);
- //等待辅助线程终止
- WaitForSingleObject(m_pThread->m_hThread, INFINITE);
- m_pThread=NULL;
- CloseHandle(m_hCom);
- }
- BOOL CRobotDoc::ConfigConnection()///设置DCB
- {
- DCB dcb;
- if(!GetCommState(m_hCom, &dcb))
- return FALSE;
- dcb.fBinary=TRUE;
- dcb.BaudRate=m_nBaud; // 波特率
- dcb.ByteSize=m_nDataBits; // 每字节位数
- dcb.fParity=TRUE;
- switch(m_nParity) // 校验设置
- {
- case 0: dcb.Parity=NOPARITY;
- break;
- case 1: dcb.Parity=EVENPARITY;
- break;
- case 2: dcb.Parity=ODDPARITY;
- break;
- default:;
- }
- switch(m_nStopBits) // 停止位
- {
- case 0: dcb.StopBits=ONESTOPBIT;
- break;
- case 1: dcb.StopBits=ONE5STOPBITS;
- break;
- case 2: dcb.StopBits=TWOSTOPBITS;
- break;
- default:;
- }
- // 硬件流控制设置
- // dcb.fOutxCtsFlow=m_nFlowCtrl==1;
- // dcb.fRtsControl=m_nFlowCtrl==1?RTS_CONTROL_HANDSHAKE:RTS_CONTROL_ENABLE;
- // XON/XOFF流控制设置
- // dcb.fInX=dcb.fOutX=m_nFlowCtrl==2;
- // dcb.XonChar=XON;
- // dcb.XoffChar=XOFF;
- // dcb.XonLim=50;
- // dcb.XoffLim=50;
- return SetCommState(m_hCom, &dcb);
- }
- DWORD CRobotDoc::ReadComm(unsigned char *buf,DWORD dwLength)
- {
- DWORD length=0;
- COMSTAT ComStat;
- DWORD dwErrorFlags;
- ClearCommError(m_hCom,&dwErrorFlags,&ComStat); // 清除错误标志
- length=min(dwLength, ComStat.cbInQue);
- ReadFile(m_hCom,buf,length,&length,&m_osRead);// 将指定数量的字符从串行口输出
- return length;
- }
- // 将指定数量的字符从串行口输出
- DWORD CRobotDoc::WriteComm(unsigned char *buf,DWORD dwLength)
- {
- BOOL fState;
- DWORD length=dwLength;
- COMSTAT ComStat;
- DWORD dwErrorFlags;
- ClearCommError(m_hCom,&dwErrorFlags,&ComStat); // 清除错误标志
- fState=WriteFile(m_hCom,buf,length,&length,&m_osWrite);
- if(!fState){
- if(GetLastError()==ERROR_IO_PENDING)
- {
- GetOverlappedResult(m_hCom,&m_osWrite,&length,TRUE);// 等待
- }
- else
- length=0;
- }
- return length;
- }
- // 工作者线程,负责监视串行口
- void CRobotDoc::OnHelp()
- {
- // TODO: Add your command handler code here
- //ShellExecute(NULL,NULL,_T("Jqrhelp.chm"),NULL,_T("e://机器人"),NULL);
- }