通信网络设计课程设计-利用MSCOMM串口通信控件进行异步串行通信的VC程序设计.doc_第1页
通信网络设计课程设计-利用MSCOMM串口通信控件进行异步串行通信的VC程序设计.doc_第2页
通信网络设计课程设计-利用MSCOMM串口通信控件进行异步串行通信的VC程序设计.doc_第3页
通信网络设计课程设计-利用MSCOMM串口通信控件进行异步串行通信的VC程序设计.doc_第4页
通信网络设计课程设计-利用MSCOMM串口通信控件进行异步串行通信的VC程序设计.doc_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

通信网络设计课程设计(w7): 利用mscomm串口通信控件进行异步串行通信的vc程序设计设计日期20081229 -20090110 利用mscomm串口通信控件进行异步串行通信的vc程序设计一 课程设计题目 利用mscomm串口通信控件进行异步串行通信的vc程序设计二 课程设计要求 环境要求:windows95/98/2000/xp功能要求:能将键盘上输入的数据发送到另一台计算机上; 能将接收到的数据显示到屏幕窗口内;了解常用通信网络设备的操作和应用,掌握常用的通信协议的工作原理,能利用visual c+、开发工具完成相应的通信程序的设计工作。学习简明地介绍自己的设计工作。三 课程设计目的 用visual c+编程工具主机提供的接口,解决网络用户之间的交互式对话问题。进一步深入掌握网络设计和通信程序的设计原理。使学生对计算机通信网络的设计实现有较深的了解,培养较高的通信网络设计能力。四 程序思路硬件连接方面:数据的发送和接收由软件控制,不进行硬件握手,其连接方法如图真正需要互相连接的是rxd、txd和gnd;软件方面:串行端口的本质功能是作为cpu和串行设备间的编码转换器。当数据从 cpu经过串行端口发送出去时,字节数据转换为串行的位。在接收数据时,串行的位被转换为字节数据在windows环境(windows nt、win98、windows2000)下,串口是系统资源的一部分。应用程序要使用串口进行通信,必须在使用之前向操作系统提出资源申请要求(打开串口),通信完成后必须释放资源(关闭串口)。vc+ 6.0提供的mscomm控件通过串行端口发送和接收数据,为应用程序提供串行通信功能。使用非常方便。在当前的workspace中插入mscomm控件。 project菜单-add to project-components and controls-registered activex controls-选择components: microsoft communications control, version 6.0 插入到当前的workspace中。 结果添加了类cmscomm(及相应文件:mscomm.h和mscomm.cpp )。在mscomm.h中加入mscomm控件。protected: cmscomm m_comport;在mscomm.cpp:oncreare()中:dword style=ws_visible|ws_child; if (!m_comport.create(null,style,crect(0,0,0,0),this,id_commctrl)trace0(failed to create ole communications controln);return -1; / fail to create.打开并初始化串口void ccomdlg:onopencom() if(m_com.getportopen() /打开通信端口m_com.setportopen(false);m_com.setcommport(1); /选择com1if( !m_com.getportopen()m_com.setportopen(true);/打开串口messagebox(串口已经打开);elsemessagebox(不能打开串口!);m_com.setsettings(9600,n,8,1); /波特率9600,无校验,8个数据位,1个停止位 /以字符串的形式设置并返回波特率、奇偶校验、数据位、停止位m_com.setinputmode(1); / 定义input属性获取数据的方式(为0:文本方式;为1:二进制方式)。m_com.setrthreshold(1); /为1表示有一个字符引发一个事件m_com.setinputlen(0); /设置每次input读入的字符个数,缺省值为0,表明读取接收缓冲区中的全部内容m_com.getinput();m_opened=true;捕捉串口事项。mscomm控件可以采用轮询或事件驱动的方法从端口获取数据。我们介绍比较使用的事件驱动方法:有事件(如接收到数据)时通知程序。在程序中需要捕获并处理这些通讯事件。在mainfrm.h中:protected:afx_msg void oncommmscomm();declare_eventsink_map()在comdlg.cpp中:begin_eventsink_map(ccomdlg, cdialog) /afx_eventsink_map(ccomdlg)on_event(ccomdlg, idc_mscomm2, 1 /* oncomm */, oncomm, vts_none)/afx_eventsink_mapend_eventsink_map() / 映射activex控件串口读写.完成读写的函数的确很简单,getinput()和setoutput()就可。两个函数的原型是:variant getinput();及 void setoutput(const variant& newvalue);都要使用variant类型(所有idispatch:invoke的参数和返回值在内部都是作为variant对象处理的)。无论是在pc机读取上传数据时还是在pc机发送下行命令时,我们都习惯于使用字符串的形式(也可以说是数组形式)。查阅variant文档知道,可以用bstr表示字符串,但遗憾的是所有的bstr都是包含宽字符,即使我们没有定义_unicode_unicode也是这样! winnt支持宽字符, 而win95并不支持。为解决上述问题,我们在实际工作中使用cbytearray,给出相应的部分程序如下:void ccomdlg:oncomm() / todo: add your control notification handler code herevariant variant_inp; colesafearray safearray_inp; long len,k; byte rxdata2048; cstring strtemp; if(m_com.getcommevent()=2) variant_inp=m_com.getinput(); safearray_inp=variant_inp; len=safearray_inp.getonedimsize(); for(k=0;klen;k+) safearray_inp.getelement(&k,rxdata+k); for(k=0;kloadicon(idr_mainframe);void ccomdlg:dodataexchange(cdataexchange* pdx)cdialog:dodataexchange(pdx);ddx_control(pdx, idc_mscomm2, m_com);ddx_text(pdx, idc_recv, m_recv);ddx_text(pdx, idc_send, m_send);begin_message_map(ccomdlg, cdialog)on_wm_syscommand()on_wm_paint()on_wm_querydragicon()on_bn_clicked(idc_opencom, onopencom)on_bn_clicked(idc_onsend, ononsend)end_message_map()/ ccomdlg message handlersbool ccomdlg:oninitdialog()cdialog:oninitdialog();assert(idm_aboutbox & 0xfff0) = idm_aboutbox);assert(idm_aboutbox appendmenu(mf_separator);psysmenu-appendmenu(mf_string, idm_aboutbox, straboutmenu);seticon(m_hicon, true);seticon(m_hicon, false);return true; void ccomdlg:onsyscommand(uint nid, lparam lparam)if (nid & 0xfff0) = idm_aboutbox)caboutdlg dlgabout;dlgabout.domodal();elsecdialog:onsyscommand(nid, lparam);void ccomdlg:onpaint() if (isiconic()cpaintdc dc(this);sendmessage(wm_iconerasebkgnd, (wparam) dc.getsafehdc(), 0)int cxicon = getsystemmetrics(sm_cxicon);int cyicon = getsystemmetrics(sm_cyicon);crect rect;getclientrect(&rect);int x = (rect.width() - cxicon + 1) / 2;int y = (rect.height() - cyicon + 1) / 2;dc.drawicon(x, y, m_hicon);elsecdialog:onpaint();hcursor ccomdlg:onquerydragicon()return (hcursor) m_hicon;void ccomdlg:onopencom() if(m_com.getportopen() /打开通信端口m_com.setportopen(false);m_com.setcommport(1); /选择com1if( !m_com.getportopen()m_com.setportopen(true);/打开串口messagebox(串口已经打开);elsemessagebox(不能打开串口!);m_com.setsettings(9600,n,8,1);m_com.setinputmode(1); m_com.setrthreshold(1); m_com.setinputlen(0); m_com.getinput();m_opened=true;begin_eventsink_map(ccomdlg, cdialog)on_event(ccomdlg, idc_mscomm2, 1 /* oncomm */, oncomm, vts_none)end_eventsink_map()void ccomdlg:oncomm() variant variant_inp; colesafearray safearray_inp; long len,k; byte rxdata2048; cstring strtemp; if(m_com.getcommevent()=2) variant_inp=m_com.getinput(); safearray_inp=variant_inp; len=safearray_inp.getonedimsize(); for(k=0;klen;k+) safearray_inp.getelement(&k,rxdata+k); for(k=0;kgetwindowtext(m_send);if(m_send!=) updatedata(true);m_com.setoutput(colevariant(m_send);m_send= ;updatedata(false);elsemessagebox(不能发空数据);elsemessagebox(请打开串口);mscomm.cpp#include stdafx.h#include mscomm.h/ cmscommimplement_dyncreate(cmscomm, cwnd)/ cmscomm properties/ cmscomm operationsvoid cmscomm:setcdholding(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x1, dispatch_propertyput, vt_empty, null, parms, bnewvalue);bool cmscomm:getcdholding()bool result;invokehelper(0x1, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setcommid(long nnewvalue)static byte parms =vts_i4;invokehelper(0x3, dispatch_propertyput, vt_empty, null, parms, nnewvalue);long cmscomm:getcommid()long result;invokehelper(0x3, dispatch_propertyget, vt_i4, (void*)&result, null);return result;void cmscomm:setcommport(short nnewvalue)static byte parms =vts_i2;invokehelper(0x4, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getcommport()short result;invokehelper(0x4, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setctsholding(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x5, dispatch_propertyput, vt_empty, null, parms,bnewvalue);bool cmscomm:getctsholding()bool result;invokehelper(0x5, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setdsrholding(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x7, dispatch_propertyput, vt_empty, null, parms, bnewvalue);bool cmscomm:getdsrholding()bool result;invokehelper(0x7, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setdtrenable(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x9, dispatch_propertyput, vt_empty, null, parms,bnewvalue);bool cmscomm:getdtrenable()bool result;invokehelper(0x9, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:sethandshaking(long nnewvalue)static byte parms =vts_i4;invokehelper(0xa, dispatch_propertyput, vt_empty, null, parms, nnewvalue);long cmscomm:gethandshaking()long result;invokehelper(0xa, dispatch_propertyget, vt_i4, (void*)&result, null);return result;void cmscomm:setinbuffersize(short nnewvalue)static byte parms =vts_i2;invokehelper(0xb, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getinbuffersize()short result;invokehelper(0xb, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setinbuffercount(short nnewvalue)static byte parms =vts_i2;invokehelper(0xc, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getinbuffercount()short result;invokehelper(0xc, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setbreak(bool bnewvalue)static byte parms =vts_bool;invokehelper(0xd, dispatch_propertyput, vt_empty, null, parms, bnewvalue);bool cmscomm:getbreak()bool result;invokehelper(0xd, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setinputlen(short nnewvalue)static byte parms =vts_i2;invokehelper(0xe, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getinputlen()short result;invokehelper(0xe, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setnulldiscard(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x10, dispatch_propertyput, vt_empty, null, parms, bnewvalue);bool cmscomm:getnulldiscard()bool result;invokehelper(0x10, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setoutbuffersize(short nnewvalue)static byte parms =vts_i2;invokehelper(0x11, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getoutbuffersize()short result;invokehelper(0x11, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setoutbuffercount(short nnewvalue)static byte parms =vts_i2;invokehelper(0x12, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getoutbuffercount()short result;invokehelper(0x12, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setparityreplace(lpctstr lpsznewvalue)static byte parms =vts_bstr;invokehelper(0x13, dispatch_propertyput, vt_empty, null, parms, lpsznewvalue);cstring cmscomm:getparityreplace()cstring result;invokehelper(0x13, dispatch_propertyget, vt_bstr, (void*)&result, null);return result;void cmscomm:setportopen(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x14, dispatch_propertyput, vt_empty, null, parms, bnewvalue);bool cmscomm:getportopen()bool result;invokehelper(0x14, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setrthreshold(short nnewvalue)static byte parms =vts_i2;invokehelper(0x15, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getrthreshold()short result;invokehelper(0x15, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setrtsenable(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x16, dispatch_propertyput, vt_empty, null, parms, bnewvalue);bool cmscomm:getrtsenable()bool result;invokehelper(0x16, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setsettings(lpctstr lpsznewvalue)static byte parms =vts_bstr;invokehelper(0x17, dispatch_propertyput, vt_empty, null, parms, lpsznewvalue);cstring cmscomm:getsettings()cstring result;invokehelper(0x17, dispatch_propertyget, vt_bstr, (void*)&result, null);return result;void cmscomm:setsthreshold(short nnewvalue)static byte parms =vts_i2;invokehelper(0x18, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getsthreshold()short result;invokehelper(0x18, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:setoutput(const variant& newvalue)static byte parms =vts_variant;invokehelper(0x19, dispatch_propertyput, vt_empty, null, parms, &newvalue);variant cmscomm:getoutput()variant result;invokehelper(0x19, dispatch_propertyget, vt_variant, (void*)&result, null);return result;void cmscomm:setinput(const variant& newvalue)static byte parms =vts_variant;invokehelper(0x1a, dispatch_propertyput, vt_empty, null, parms, &newvalue);variant cmscomm:getinput()variant result;invokehelper(0x1a, dispatch_propertyget, vt_variant, (void*)&result, null);return result;void cmscomm:setcommevent(short nnewvalue)static byte parms =vts_i2;invokehelper(0x1b, dispatch_propertyput, vt_empty, null, parms, nnewvalue);short cmscomm:getcommevent()short result;invokehelper(0x1b, dispatch_propertyget, vt_i2, (void*)&result, null);return result;void cmscomm:seteofenable(bool bnewvalue)static byte parms =vts_bool;invokehelper(0x1c, dispatch_propertyput, vt_empty, null, parms,bnewvalue);bool cmscomm:geteofenable()bool result;invokehelper(0x1c, dispatch_propertyget, vt_bool, (void*)&result, null);return result;void cmscomm:setinputmode(long nnewvalue)static byte parms =vts_i4;invokehelper(0x1d, dispatch_propertyput, vt_empty, null, parms, nnewvalue);long cmscomm:getinputmode()long result;invokehelper(0x1d, dispatch_propertyget, vt_i4, (void*)&result, null);return result;/ stdafx.cpp :#include stdafx.h/ com.h#if !defined(afx_com_h_cc3de67a_a6d3_4e95_b3a1_21b971a17c64_included_)#define afx_com_h_cc3de67a_a6d3_4e95_b3a1_21b971a17c64_included_#if _msc_ver 1000#pragma once#endif#ifndef _afxwin_h_#error include stdafx.h before including this file for pch#endif#include resource.h/ ccomapp:class ccomapp : public cwinapppublic:ccomapp();public:virtual bool initinstance();declare_message_map();#endif#if !defined(afx_comdlg_h_2c5d9f44_ab0a_4501_a2b7_7bca6eab1b92_included_)#define afx_comdlg_h_2c5d9f44_ab0a_4501_a2b7_7bca6eab1b92_included_#if _msc_ver 1000#pragma once#endif / _msc_ver 1000/ ccomdlg dialogclass ccomdlg : public cdialogpublic:bool m_opened;ccomdlg(cwnd* pparent = null); enum idd = idd_com_dialog ;cmscommm_com;cstringm_recv;cstringm_send;protected:virtual void dodataexchange(cdataexchange* pdx);protected:hicon m_hicon;virtual bool oninitdialog();afx_msg void onsyscommand(uint nid, lparam lparam);afx_msg void onpaint();afx_msg hcursor onquerydragicon();afx_msg void onopencom();afx_msg void oncomm();afx_msg void ononsend();declare_eventsink_map()declare_message_map();#endif#if _msc_ver 1000#pragma once#endifclass cmscomm : public cwndprotected:declare_dyncreate(cmscomm)public:clsid const& getclsid()static clsid const clsid= 0x648a5600, 0x2c6e, 0x101b, 0x82, 0xb6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14 ;return clsid;virtual bool create(lpctstr lpszclassname,lpctstr lpszwindowname, dword dwstyle,const rect& rect,cwnd* pparentwnd, uint nid,ccreatecontext* pcontext = null) return createcontrol(getclsid(), lpszwindowname, dwstyle, rect, pparentwnd, nid); bool create(lpctstr lpszwindowname, dword dwstyle,const rect& rect, cwnd* pparentwnd, uint nid,cfile* ppersist = null, bool bstorage = false,bstr bstrlickey = null) retur

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论