您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > VS2005和Win7系统下编写基于控件的MFC串口程序
VS2005和Win7系统下编写基于控件的MFC串口程序在VS2005中不带串口控件,那么第一步就是去下载或者复制一个mscomm32.ocx文件。如下图所示,将其拷贝到System32目录下,我的是D:\Windows\System32,如果你的win7系统在C盘,请注意目录位置。第二步:在“开始”-“运行”-输入“regsvr32mscomm32.ocx”以进行控件注册。第三步:用VS2005建立一个基于MFC对话框的应用程序,工程名叫MFCComm。在资源对话框点鼠标右键,插入ActiveX控件,找到刚才注册成功的控件MicrosoftCommunicationsControl,version6.0.给插入的控件关联一个成员变量m_Comm。点控件-右键-添加变量,如下图所示同时点控件-右键-添加事件处理程序,如下图,点击添加编辑即可。该事件用于接受数据用,生成的代码为voidCMFCCommDlg::OnCommMscomm1()。最终设计的Dlg对话框如下,在接受编辑框内关联一个CString的数值变量m_strRecvData.好了,现在开始编写程序,//MFCCommDlg.h:头文件//#pragmaonce#includemscomm1.h//CMFCCommDlg对话框classCMFCCommDlg:publicCDialog{//构造public:CMFCCommDlg(CWnd*pParent=NULL);//标准构造函数//对话框数据enum{IDD=IDD_MFCCOMM_DIALOG};protected:virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持//实现protected:HICONm_hIcon;//生成的消息映射函数virtualBOOLOnInitDialog();afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);afx_msgvoidOnPaint();afx_msgHCURSOROnQueryDragIcon();afx_msgvoidOnTimer(UINT_PTRnIDEvent);afx_msgvoidOnDestroy();virtualBOOLPreTranslateMessage(MSG*pMsg);DECLARE_MESSAGE_MAP()public:DECLARE_EVENTSINK_MAP()voidOnCommMscomm1();afx_msgvoidOnBnClickedSend();//初始化串口voidInitSerialPort();intString2Hex(CStringstr,CByteArray&senddata);charConvertHexChar(charch);public:CMscomm1m_Comm;CStringm_strRecvData;};//MFCCommDlg.cpp:实现文件//#includestdafx.h#includeMFCComm.h#includeMFCCommDlg.h#ifdef_DEBUG#definenewDEBUG_NEW#endif//用于应用程序“关于”菜单项的CAboutDlg对话框classCAboutDlg:publicCDialog{public:CAboutDlg();//对话框数据enum{IDD=IDD_ABOUTBOX};protected:virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持//实现protected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialog(CAboutDlg::IDD){}voidCAboutDlg::DoDataExchange(CDataExchange*pDX){CDialog::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)END_MESSAGE_MAP()//CMFCCommDlg对话框CMFCCommDlg::CMFCCommDlg(CWnd*pParent/*=NULL*/):CDialog(CMFCCommDlg::IDD,pParent),m_strRecvData(_T()){m_hIcon=AfxGetApp()-LoadIcon(IDR_MAINFRAME);}voidCMFCCommDlg::DoDataExchange(CDataExchange*pDX){CDialog::DoDataExchange(pDX);DDX_Control(pDX,IDC_MSCOMM1,m_Comm);DDX_Text(pDX,IDC_EDIT3,m_strRecvData);}BEGIN_MESSAGE_MAP(CMFCCommDlg,CDialog)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()//}}AFX_MSG_MAPON_WM_TIMER()ON_WM_DESTROY()ON_BN_CLICKED(IDC_SEND,&CMFCCommDlg::OnBnClickedSend)END_MESSAGE_MAP()//CMFCCommDlg消息处理程序BOOLCMFCCommDlg::OnInitDialog(){CDialog::OnInitDialog();//将“关于...”菜单项添加到系统菜单中。//IDM_ABOUTBOX必须在系统命令范围内。ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX0xF000);CMenu*pSysMenu=GetSystemMenu(FALSE);if(pSysMenu!=NULL){CStringstrAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if(!strAboutMenu.IsEmpty()){pSysMenu-AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);}}//设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动//执行此操作SetIcon(m_hIcon,TRUE);//设置大图标SetIcon(m_hIcon,FALSE);//设置小图标//TODO:在此添加额外的初始化代码InitSerialPort();returnTRUE;//除非将焦点设置到控件,否则返回TRUE}voidCMFCCommDlg::OnSysCommand(UINTnID,LPARAMlParam){if((nID&0xFFF0)==IDM_ABOUTBOX){CAboutDlgdlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID,lParam);}}//如果向对话框添加最小化按钮,则需要下面的代码//来绘制该图标。对于使用文档/视图模型的MFC应用程序,//这将由框架自动完成。voidCMFCCommDlg::OnPaint(){if(IsIconic()){CPaintDCdc(this);//用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND,reinterpret_castWPARAM(dc.GetSafeHdc()),0);//使图标在工作矩形中居中intcxIcon=GetSystemMetrics(SM_CXICON);intcyIcon=GetSystemMetrics(SM_CYICON);CRectrect;GetClientRect(&rect);intx=(rect.Width()-cxIcon+1)/2;inty=(rect.Height()-cyIcon+1)/2;//绘制图标dc.DrawIcon(x,y,m_hIcon);}else{CDialog::OnPaint();}}//当用户拖动最小化窗口时系统调用此函数取得光标显示。//HCURSORCMFCCommDlg::OnQueryDragIcon(){returnstatic_castHCURSOR(m_hIcon);}voidCMFCCommDlg::OnDestroy(){m_Comm.put_PortOpen(FALSE);CDialog::OnDestroy();//TODO:在此处添加消息处理程序代码}/*-----------------------------------------------------------------控制部分-----------------------------------------------------------------*/voidCMFCCommDlg::OnTimer(UINT_PTRnIDEvent){//TODO:在此添加消息处理程序代码和/或调用默认值CDialog::OnTimer(nIDEvent);}voidCMFCCommDlg::OnBnClickedSend(){//TODO:在此添加控件通知处理程序代码//发送指令1:AA09200000000001020655if(m_Comm.get_PortOpen()){CStringstrInput;GetDlgItem(IDC_EDIT1)-GetWindowText(strInput);//将CString转换成十六进制数字CByteArrayhexdata;String2Hex(strInput,hexdata);m_Comm.put_Output(COleVariant(hexdata));//发送数据}}BOOLCMFCCommDlg::PreTranslateMessage(MSG*pMsg){//TODO:在此添加专用代码和/或调用基类if(pMsg-message==WM_KEYDOWN){switch(pMsg-wParam){case48:break;}}returnCDialog::PreTranslateMessage(pMsg);}/*-----------------------------------------------------------------串口程序相关-----------------------------------------------------------------*/BEGIN_EVENTSINK_MAP(CMFCCommDlg,CDialog)ON_EVENT(CMFCCommDlg,IDC_MSCOMM1,1,CMFCCommDlg::OnCommMscomm1,VTS_NONE)END_EVENTSINK_MAP()voidCMFCCommDlg::OnCommMscomm1(){//串口事件响应函数VARIANTvariant_inp;COleSafeArraysafearray_inp;LONGlen,k;BYTErxdata[2048];//设置BYTE数组An8-bitintegerthatisnotsigned.CStringstrtemp;if(m_Comm.get_CommEvent()==2)//事件值为2表示接收缓冲区内有字符{////////以下你可以根据自己的通信协议加入处理代码variant_inp=m_Comm.get_Input();//读缓冲区safearray_inp=variant_inp;//VARIANT型变量转换为ColeSafeArray型变量
本文标题:VS2005和Win7系统下编写基于控件的MFC串口程序
链接地址:https://www.777doc.com/doc-2855228 .html