MSComm控件
Visual C++為我們提供了一種好用的ActiveX控件Microsoft Communications Control(即MSComm)來支持應用程序?qū)Υ诘脑L問,在應用程序中插入MSComm控件后就可以較為方便地實現(xiàn)對通過計算機串口收發(fā)數(shù)據(jù)。
要使用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類型的接口函數(shù)),下面我們摘取了頭文件的主要代碼并對其關鍵部分給出了注釋:
#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:打開,F(xiàn)ALSE:關閉
BOOL GetPortOpen();
//串口是否已打開,TRUE:打開,F(xiàn)ALSE:關閉
void SetRThreshold(short nNewValue);
//如果設置為1,表示一接收到字符就發(fā)送2號事件
short GetRThreshold();
void SetRTSEnable(BOOL bNewValue);
//硬件握手使能?
BOOL GetRTSEnable();
void SetSettings(LPCTSTR lpszNewValue);
//Settings由4部分組成,其格式為:"BBBB,P,D,S",即"波特率,是否奇偶校驗,數(shù)據(jù)位 //個數(shù),停止位",如設置為:"9600,n,8,1"
CString GetSettings();
void SetSThreshold(short nNewValue);
//如果保持缺省值0不變,則表示發(fā)送數(shù)據(jù)的過程中串口上不發(fā)生事件
short GetSThreshold();
void SetOutput(const VARIANT& newValue);
//一個非常重要的函數(shù),用于寫串口,注意其接收的輸入?yún)?shù)為VARIANT類型對象,
//我們需要將字符串轉(zhuǎn)化為VARIANT類型對象
VARIANT GetOutput();
void SetInput(const VARIANT& newValue);
VARIANT GetInput();
//一個非常重要的函數(shù),用于讀串口,注意其返回的是VARIANT類型對象,我們需要
//將其轉(zhuǎn)化為字符串
void SetCommEvent(short nNewValue);
short GetCommEvent();
//一個非常重要的函數(shù),獲得串口上剛發(fā)生的事件("事件"可以理解為軟件意義上的
//"消息"或硬件意義上的"中斷"),事件的發(fā)送會導致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
Visual C++為我們提供了一種好用的ActiveX控件Microsoft Communications Control(即MSComm)來支持應用程序?qū)Υ诘脑L問,在應用程序中插入MSComm控件后就可以較為方便地實現(xiàn)對通過計算機串口收發(fā)數(shù)據(jù)。
要使用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類型的接口函數(shù)),下面我們摘取了頭文件的主要代碼并對其關鍵部分給出了注釋:
#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:打開,F(xiàn)ALSE:關閉
BOOL GetPortOpen();
//串口是否已打開,TRUE:打開,F(xiàn)ALSE:關閉
void SetRThreshold(short nNewValue);
//如果設置為1,表示一接收到字符就發(fā)送2號事件
short GetRThreshold();
void SetRTSEnable(BOOL bNewValue);
//硬件握手使能?
BOOL GetRTSEnable();
void SetSettings(LPCTSTR lpszNewValue);
//Settings由4部分組成,其格式為:"BBBB,P,D,S",即"波特率,是否奇偶校驗,數(shù)據(jù)位 //個數(shù),停止位",如設置為:"9600,n,8,1"
CString GetSettings();
void SetSThreshold(short nNewValue);
//如果保持缺省值0不變,則表示發(fā)送數(shù)據(jù)的過程中串口上不發(fā)生事件
short GetSThreshold();
void SetOutput(const VARIANT& newValue);
//一個非常重要的函數(shù),用于寫串口,注意其接收的輸入?yún)?shù)為VARIANT類型對象,
//我們需要將字符串轉(zhuǎn)化為VARIANT類型對象
VARIANT GetOutput();
void SetInput(const VARIANT& newValue);
VARIANT GetInput();
//一個非常重要的函數(shù),用于讀串口,注意其返回的是VARIANT類型對象,我們需要
//將其轉(zhuǎn)化為字符串
void SetCommEvent(short nNewValue);
short GetCommEvent();
//一個非常重要的函數(shù),獲得串口上剛發(fā)生的事件("事件"可以理解為軟件意義上的
//"消息"或硬件意義上的"中斷"),事件的發(fā)送會導致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

