码迷,mamicode.com
首页 > 其他好文 > 详细

VC 串口控件 MSComm控件

时间:2015-01-05 10:56:32      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

MSComm控件

  Visual C++为我们提供了一种好用的ActiveX控件Microsoft Communications Control(即MSComm)来支持应用程序对串口的访问,在应用程序中插入MSComm控件后就可以较为方便地实现对通过计算机串口收发数据。

  要使用ActiveX控件MSComm,程序员必须将其添加入工程,其方法是:

  (1)单击主菜单project的子菜单Add To project的Components and Controls选项;

  (2)在弹出的"Components and Controls Gallery"对话框中选择Registered ActiveX Controls文件夹中的"Microsoft Communications Control,version 6.0"选项,如下图:

技术分享


  单击其中的"Insert"按钮,MSComm控件就被增加到工程中了。与此同时,类CMSComm的相关文件mscomm.h和mscomm.cpp也一并被加入Project的Header Files和Source Files中。当然,程序员可以自己修改文件名,如下图:

技术分享


  直接分析mscomm.h头文件就可以完备地获取这个控件的使用方法(主要是public类型的接口函数),下面我们摘取了头文件的主要代码并对其关键部分给出了注释:

#if !defined(AFX_MSCOMM_H__) #define AFX_MSCOMM_H__  #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // Machine generated IDispatch wrapper class(es) created by Microsoft Visual C++
// NOTE: Do not modify the contents of this file. If this class is regenerated by // Microsoft Visual C++, your modifications will be overwritten.
///////////////////////////////////////////////////////////////////////////// // CMSComm wrapper class
class CMSComm : public CWnd { protected:  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)  { return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID,   pPersist, bStorage, bstrLicKey); }
  // Attributes  public:
  // Operations  public:   void SetCDHolding(BOOL bNewValue);   BOOL GetCDHolding();   void SetCommID(long nNewValue);   long GetCommID();   void SetCommPort(short nNewValue);   //设置端口号,如nNewValue =1表示COM1   short GetCommPort();   void SetCTSHolding(BOOL bNewValue);   BOOL GetCTSHolding();   void SetDSRHolding(BOOL bNewValue);   BOOL GetDSRHolding();   void SetDTREnable(BOOL bNewValue);   BOOL GetDTREnable();   void SetHandshaking(long nNewValue);   long GetHandshaking();   void SetInBufferSize(short nNewValue);   short GetInBufferSize();   void SetInBufferCount(short nNewValue);   short GetInBufferCount();   void SetBreak(BOOL bNewValue);   BOOL GetBreak();   void SetInputLen(short nNewValue);   short GetInputLen();   void SetNullDiscard(BOOL bNewValue);   BOOL GetNullDiscard();   void SetOutBufferSize(short nNewValue);   short GetOutBufferSize();   void SetOutBufferCount(short nNewValue);   short GetOutBufferCount();   void SetParityReplace(LPCTSTR lpszNewValue);   CString GetParityReplace();   void SetPortOpen(BOOL bNewValue);   //打开或关闭串口,TRUE:打开,FALSE:关闭   BOOL GetPortOpen();   //串口是否已打开,TRUE:打开,FALSE:关闭   void SetRThreshold(short nNewValue);   //如果设置为1,表示一接收到字符就发送2号事件   short GetRThreshold();   void SetRTSEnable(BOOL bNewValue);   //硬件握手使能?   BOOL GetRTSEnable();   void SetSettings(LPCTSTR lpszNewValue);   //Settings由4部分组成,其格式为:"BBBB,P,D,S",即"波特率,是否奇偶校验,数据位 //个数,停止位",如设置为:"9600,n,8,1"   CString GetSettings();   void SetSThreshold(short nNewValue);    //如果保持缺省值0不变,则表示发送数据的过程中串口上不发生事件   short GetSThreshold();   void SetOutput(const VARIANT& newValue);   //一个非常重要的函数,用于写串口,注意其接收的输入参数为VARIANT类型对象,    //我们需要将字符串转化为VARIANT类型对象   VARIANT GetOutput();   void SetInput(const VARIANT& newValue);   VARIANT GetInput();   //一个非常重要的函数,用于读串口,注意其返回的是VARIANT类型对象,我们需要   //将其转化为字符串   void SetCommEvent(short nNewValue);   short GetCommEvent();   //一个非常重要的函数,获得串口上刚发生的事件("事件"可以理解为软件意义上的   //"消息"或硬件意义上的"中断"),事件的发送会导致OnComm消息的诞生!   void SetEOFEnable(BOOL bNewValue);   BOOL GetEOFEnable();   void SetInputMode(long nNewValue);   long GetInputMode();  };
 //{{AFX_INSERT_LOCATION}}  // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif


  分析上述源代码可知,基本上,MSComm的诸多接口可以分为如下几类:

  (1)打开与设置串口接口函数;

  (2)获得串口设置和串口状态接口函数;

  (3)设置串口发送数据方式、缓冲区接口及发送数据接口函数;

  (4)设置串口接收数据方式、缓冲区接口及接收数据接口函数;

  (5)设置与获取串口上发生的事件接口函数。

VC 串口控件 MSComm控件

标签:

原文地址:http://www.cnblogs.com/alex502/p/4202894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!