《利用MSCOMM串口通信控件进行异步串行通信的VC程序设计.doc》由会员分享,可在线阅读,更多相关《利用MSCOMM串口通信控件进行异步串行通信的VC程序设计.doc(28页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、利用MSCOMM串口通信控件进行异步串行通信的VC程序设计一 课程设计题目 利用MSCOMM串口通信控件进行异步串行通信的VC程序设计二 课程设计要求 环境要求:Windows95/98/2000/XP功能要求:能将键盘上输入的数据发送到另一台计算机上; 能将接收到的数据显示到屏幕窗口内;了解常用通信网络设备的操作和应用,掌握常用的通信协议的工作原理,能利用Visual C+、开发工具完成相应的通信程序的设计工作。学习简明地介绍自己的设计工作。三 课程设计目的 用Visual C+编程工具主机提供的接口,解决网络用户之间的交互式对话问题。进一步深入掌握网络设计和通信程序的设计原理。使学生对计算
2、机通信网络的设计实现有较深的了解,培养较高的通信网络设计能力。四 程序思路硬件连接方面:数据的发送和接收由软件控制,不进行硬件握手,其连接方法如图真正需要互相连接的是RXD、TXD和GND;软件方面:串行端口的本质功能是作为CPU和串行设备间的编码转换器。当数据从 CPU经过串行端口发送出去时,字节数据转换为串行的位。在接收数据时,串行的位被转换为字节数据在Windows环境(Windows NT、Win98、Windows2000)下,串口是系统资源的一部分。应用程序要使用串口进行通信,必须在使用之前向操作系统提出资源申请要求(打开串口),通信完成后必须释放资源(关闭串口)。VC+ 6.0提
3、供的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:
4、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.SetPortOpe
5、n(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
6、); /为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
7、_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类型(所有Idispa
8、tch:Invoke的参数和返回值在内部都是作为VARIANT对象处理的)。无论是在PC机读取上传数据时还是在PC机发送下行命令时,我们都习惯于使用字符串的形式(也可以说是数组形式)。查阅VARIANT文档知道,可以用BSTR表示字符串,但遗憾的是所有的BSTR都是包含宽字符,即使我们没有定义_UNICODE_UNICODE也是这样! WinNT支持宽字符, 而Win95并不支持。为解决上述问题,我们在实际工作中使用CbyteArray,给出相应的部分程序如下:void CComDlg:OnComm() / TODO: Add your control notification handler
9、 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(I
10、DR_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_OPENC
11、OM, 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(
12、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, (WP
13、ARAM) 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
14、()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.SetRThres
15、hold(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;
16、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);e
17、lseMessageBox(不能发空数据);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, parm
18、s, 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()
19、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, DISPAT
20、CH_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*)&r
21、esult, 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
22、 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 nNewV
23、alue)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;
24、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_
25、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, parm
26、s, 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
27、()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(0x
28、10, 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_PROPERTYG
29、ET, 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*)&res
30、ult, 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, NUL
31、L);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:Set
32、RThreshold(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)stati
33、c 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;Inv
34、okeHelper(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, DISPATC
35、H_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);