📄 深入浅出vc++串口编程之基于控件_qqread_com.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0042)http://www.qqread.com/vc/t642917200_2.html -->
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>深入浅出VC++串口编程之基于控件_QQread.com</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN>
<META content=深入浅出VC++串口编程之基于控件 name=description><LINK
href="深入浅出VC++串口编程之基于控件_QQread_com_files/qqread2007style.css" type=text/css
rel=stylesheet>
<SCRIPT language=javascript
src="深入浅出VC++串口编程之基于控件_QQread_com_files/article.js"></SCRIPT>
<META content="MSHTML 6.00.6000.16414" name=GENERATOR></HEAD>
<BODY>
<DIV class=top>
<DIV class=top_1><SPAN class=font_1>频道直达</SPAN> - <A title=专题频道
href="http://www.qqread.com/z/" target=_blank>专题</A> - <A title=新闻频道
href="http://www.qqread.com/news/" target=_blank>新闻</A> - <A title=电脑技巧
href="http://www.qqread.com/skill/" target=_blank>技巧</A> - <A title=组网建网
href="http://www.qqread.com/network/" target=_blank>组网</A> - <A title=程序开发
href="http://www.qqread.com/program/" target=_blank>开发</A> - <A title=安全技术
href="http://www.qqread.com/safe/" target=_blank>安全</A> - <A title=编程频道
href="http://www.qqread.com/netprogram/" target=_blank>web编程</A> - <A title=图像处理
href="http://www.qqread.com/photohandle/" target=_blank>图像</A> - <A title=操作系统
href="http://www.qqread.com/os/" target=_blank>操作系统</A> - <A title=数据库
href="http://www.qqread.com/database/" target=_blank>数据库</A> - <A title=巧巧教育
href="http://edu.qqread.com/" target=_blank>教育</A> - <A title=巧巧旅游
href="http://travel.qqread.com/" target=_blank>旅游</A> - <A title=巧巧健康
href="http://health.qqread.com/" target=_blank>健康</A> - <A title=巧巧时尚
href="http://fashion.qqread.com/" target=_blank>时尚</A> - <A title=驱动下载
href="http://down.qqread.com/" target=_blank>驱动</A> - <A title=巧巧软件园
href="http://down.qqread.com/soft/" target=_blank>软件</A> - <A title=网络游戏
href="http://www.qqread.com/netgame/" target=_blank>游戏</A> - <A title=多媒体
href="http://www.qqread.com/multimedia/" target=_blank>多媒体</A> - <A title=企业解决方案
href="http://www.qqread.com/erp/" target=_blank>ERP</A> - <A title=技术讨论组
href="http://group.qqread.com/" target=_blank>讨论组</A></DIV>
<DIV class=top_2>
<DIV class=top_2_1 id=logo><A href="http://www.qqread.com/"><IMG height=28
alt=巧巧电脑IT频道Logo91764 src="深入浅出VC++串口编程之基于控件_QQread_com_files/QQReadTeach.gif"
width=145 border=0></A></DIV>
<DIV class=top_2_2 id=loc><A href="http://www.qqread.com/">首页</A> > <A
href="http://www.qqread.com/">电脑教程</A> > <A title=程序开发
href="http://www.qqread.com/program/index.html">程序开发</A> > <A
title="Visual C++/MFC" href="http://www.qqread.com/vc/index.html">Visual
C++/MFC</A> > 正文</DIV>
<DIV class=top_2_3><A href="http://www.qqread.com/vc/rank/"
target=_blank>阅读排行榜</A> | <A style="CURSOR: pointer"
onclick=window.external.addFavorite(document.location.href,document.title)>收藏此文</A>
| <A title=巧巧读书首页 style="CURSOR: pointer"
onclick="window.external.addFavorite('http://www.qqread.com/','巧巧读书首页')">收藏本站</A>
| <A
onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.qqread.com/');return(false);"
href="http://www.qqread.com/">设为首页</A></DIV></DIV></DIV>
<DIV class=main>
<DIV class=left>
<DIV>
<SCRIPT src="深入浅出VC++串口编程之基于控件_QQread_com_files/travel.js"
type=text/javascript></SCRIPT>
</DIV>
<DIV class=left_1>
<H1 id=w_tt>深入浅出VC++串口编程之基于控件</H1>来源:天极开发 作者:宋宝华 出处:巧巧读书 <SPAN
id=rdate>2006-03-22</SPAN> <A title=技术论坛 href="http://group.qqread.com/"
target=_blank>进入讨论组</A></DIV>
<DIV class=left_2><SPAN class=font_3>关 键 词:</SPAN><A class=link_keywords
href="http://www.qqread.com/keys/cpp/index.html"
target=_blank>c++</A> <A class=link_keywords
href="http://www.qqread.com/keys/mfc/index.html"
target=_blank>mfc</A> <A class=link_keywords
href="http://www.qqread.com/keys/opera/index.html"
target=_blank>opera</A> <A class=link_keywords
href="http://www.qqread.com/keys/os/index.html"
target=_blank>os</A> <A class=link_keywords
href="http://www.qqread.com/keys/vc/index.html" target=_blank>vc</A>
<DIV id=gglink></DIV></DIV>
<DIV class=left_4><A
href="http://www.qqread.com/vc/t642917200.html">上一页</A> <A
href="http://www.qqread.com/vc/t642917200.html">1</A> <FONT
color=#ff0000>2</FONT> <A
href="http://www.qqread.com/vc/t642917200_3.html">3</A> <A
href="http://www.qqread.com/vc/t642917200_3.html">下一页</A> </DIV>
<DIV class=left_3 id=artcnt>
<DIV class=left_6_1>
<UL>·<A title=深入浅出VC++串口编程之短信应用开发
href="http://www.qqread.com/vc/t042926200.html"
target=_blank>深入浅出VC++串口编程之短信应用开发</A><BR>·<A title=深入浅出VC++串口编程之第三方类
href="http://www.qqread.com/vc/t802369200.html"
target=_blank>深入浅出VC++串口编程之第三方类</A><BR>·<A title=深入浅出VC++串口编程之基本概念
href="http://www.qqread.com/vc/t882369200.html"
target=_blank>深入浅出VC++串口编程之基本概念</A><BR>·<A title=深入浅出VC++串口编程之DOS的串口编程
href="http://www.qqread.com/vc/t132360200.html"
target=_blank>深入浅出VC++串口编程之DOS的串口编程</A><BR>·<A title=深入浅出SQL之左连接、右连接和全连接
href="http://www.qqread.com/sqlserver/2006/11/t282500.html"
target=_blank>深入浅出SQL之左连接、右连接和全连接</A><BR>·<A title=深入浅出话多态(上)—具体而微
href="http://www.qqread.com/vcdotnet/t845130002.html"
target=_blank>深入浅出话多态(上)—具体而微</A><BR>·<A title="深入浅出SQL教程之Group by和Having"
href="http://www.qqread.com/sqlserver/2006/11/t278500.html"
target=_blank>深入浅出SQL教程之Group by和Having</A><BR>·<A title=深入浅出Linux设备驱动编程之内核模块
href="http://www.qqread.com/data-structure/t242692.html"
target=_blank>深入浅出Linux设备驱动编程之内核模块</A><BR>·<A title=深入浅出MFC文档/视图架构之实例剖析
href="http://www.qqread.com/vc/t412921200.html"
target=_blank>深入浅出MFC文档/视图架构之实例剖析</A><BR>·<A title=深入浅出HOOKS(上)
href="http://www.qqread.com/vc/t276230.html"
target=_blank>深入浅出HOOKS(上)</A></UL></DIV>
<DIV class=left_6_1
id=w_hzh></DIV><BR> 2.例程<BR><BR> 程序的功能和界面(如下图)都与本文连载三中《基于WIN32
API的串口编程》相同,不同的只是连载三的串口<A href="http://www.qqread.com/z/telecom/hot/index.html"
target=_blank>通信</A>以API实现,而本节的串口通信则以MSComm控件实现。<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG alt=深入浅出VC++串口编程之基于控件(图三)
src="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_3.jpg"
border=0></DIV></TD></TR></TBODY></TABLE><BR> 使用第1节的方法将控件添加入工程并添加mscomm.h和mscomm.cpp文件后,为了使用控件,我们将控件拖入对话框内任意一个位置(运行时"电话"图标会隐藏),其操作如下图:<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><A href="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_4.jpg"
target=_blank><IMG alt=深入浅出VC++串口编程之基于控件(图四)
src="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_4.jpg"
border=0></A></DIV></TD></TR></TBODY></TABLE><BR> 有趣而极富人性化的是我们可以直接右键单击这个"电话",来设置串口的属性,如下图:<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><A href="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_5.jpg"
target=_blank><IMG alt=深入浅出VC++串口编程之基于控件(图五)
src="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_5.jpg"
border=0></A></DIV></TD></TR></TBODY></TABLE><BR> 接着,我们需要为控件添加一个对应的成员变量m_mscom,其对应的变量类型为CMSComm,如下图:<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><A href="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_6.jpg"
target=_blank><IMG alt=深入浅出VC++串口编程之基于控件(图六)
src="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_6.jpg" border=0></A><IMG
alt=深入浅出VC++串口编程之基于控件(图七)
src="深入浅出VC++串口编程之基于控件_QQread_com_files/vz61_7.jpg"
border=0></DIV></TD></TR></TBODY></TABLE><BR> 这样就建立了m_mscom和IDC_MSCOMM1控件的相互映射:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>void CSerialPortActivexDlg::DoDataExchange(CDataExchange*
pDX)<BR>{<BR> CDialog::DoDataExchange(pDX);<BR> //{{AFX_DATA_MAP(CSerialPortActivexDlg)<BR> DDX_Text(pDX,
IDC_RECV_EDIT, m_recv);<BR> DDX_Text(pDX, IDC_SEND_EDIT,
m_send);<BR> DDX_Control(pDX, IDC_MSCOMM1,
m_mscom);<BR> //}}AFX_DATA_MAP<BR>}</TD></TR></TBODY></TABLE><BR> 同时,在对话框的头文件也会由"MFC类向导"自动定义CSerialPortActivexDlg类的CMSComm型成员变量m_mscom:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>CMSComm
m_mscom;</TD></TR></TBODY></TABLE><BR> 在对话框初始化时(即在CSerialPortActivexDlg::OnInitDialog函数中)打开串口1:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>BOOL
CSerialPortActivexDlg::OnInitDialog()<BR>{<BR> CDialog::OnInitDialog();<BR><BR> //
Add "About..." menu item to system menu.<BR><BR> // IDM_ABOUTBOX must be
in the system command range.<BR> ASSERT((IDM_ABOUTBOX & 0xFFF0) ==
IDM_ABOUTBOX);<BR> ASSERT(IDM_ABOUTBOX < 0xF000);<BR><BR> CMenu*
pSysMenu = GetSystemMenu(FALSE);<BR> if (pSysMenu !=
NULL)<BR> {<BR> CString
strAboutMenu;<BR> strAboutMenu.LoadString(IDS_ABOUTBOX);<BR> if
(!strAboutMenu.IsEmpty())<BR> {<BR> pSysMenu->AppendMenu(MF_SEPARATOR);<BR> pSysMenu->AppendMenu(MF_STRING,
IDM_ABOUTBOX, <BR> strAboutMenu);<BR> }<BR> }<BR><BR> // Set the icon
for this dialog. The framework does this automatically<BR> // when the
application's main window is not a dialog<BR> SetIcon(m_hIcon, TRUE); //
Set big icon<BR> SetIcon(m_hIcon, FALSE); // Set small icon<BR><BR> //
TODO: Add extra initialization here<BR> m_mscom.SetCommPort(1);
//串口1<BR> m_mscom.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes
<BR> m_mscom.SetOutBufferSize(512); //设置输入缓冲区的大小,Bytes
<BR> if(!m_mscom.GetPortOpen())
//打开串口<BR> {<BR> m_mscom.SetPortOpen(true);
<BR> }<BR> m_mscom.SetInputMode(1); //设置输入方式为二进制方式
<BR> m_mscom.SetSettings("9600,n,8,1"); //设置波特率等参数
<BR> m_mscom.SetRThreshold(1); //为1表示有一个字符即引发事件
<BR> m_mscom.SetInputLen(0);<BR><BR> return TRUE; // return TRUE unless
you set the focus to a
control<BR>}</TD></TR></TBODY></TABLE><BR> 最核心的发送串口数据函数("发送"按钮单击事件)如下:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>void CSerialPortActivexDlg::OnSendButton() <BR>{<BR> // TODO: Add your
control notification handler code
here<BR> UpdateData(true);<BR><BR> CByteArray sendArr; <BR> <A
href="http://www.qqread.com/keys/word/index.html" target=_blank>WORD</A>
wLength;<BR><BR> wLength =
m_send.GetLength();<BR> sendArr.SetSize(wLength);<BR> for(int i =0;
i<wLength; i++)<BR> {<BR> sendArr.SetAt(i,
m_send.GetAt(i));<BR> }<BR> m_mscom.SetOutput(COleVariant(sendArr));<BR>}</TD></TR></TBODY></TABLE><IMG
height=9 alt=更多文章 src="深入浅出VC++串口编程之基于控件_QQread_com_files/dian_1.gif" width=11>
更多内容请看<A href="http://www.qqread.com/z/p/java/index.html" target=_blank><FONT
color=#ff0033>Java编程开发手册</FONT></A>专题,或<A class=font_12 title=在这里你会得到众多高手的技术帮助
href="http://group.qqread.com/" target=_blank>进入讨论组</A>讨论。
<DIV class=left_6><IMG height=9 alt=更多专题
src="深入浅出VC++串口编程之基于控件_QQread_com_files/dian_1.gif" width=11> <SPAN
class=font_4>【深 度 阅 读】</SPAN> 相 关 文 章
<DIV class=left_6_1_1>
<LI><A title="深入浅出 MSconfig程序应用攻略"
href="http://www.qqread.com/skill/t325824.html" target=_blank><FONT
color=#ff0000>深入浅出 MSconfig程序应用攻略</FONT></A>
<LI><A title=Log4j深入浅出 href="http://www.qqread.com/j2ee/y220439.html"
target=_blank>Log4j深入浅出</A>
<LI><A title=深入浅出MFC文档/视图架构之框架 href="http://www.qqread.com/vc/t052926200.html"
target=_blank>深入浅出MFC文档/视图架构之框架</A>
<LI><A title=深入浅出HOOKS(下) href="http://www.qqread.com/vc/t276231.html"
target=_blank>深入浅出HOOKS(下)</A></LI></DIV></DIV></DIV>
<DIV class=left_4><A
href="http://www.qqread.com/vc/t642917200.html">上一页</A> <A
href="http://www.qqread.com/vc/t642917200.html">1</A> <FONT
color=#ff0000>2</FONT> <A
href="http://www.qqread.com/vc/t642917200_3.html">3</A> <A
href="http://www.qqread.com/vc/t642917200_3.html">下一页</A> </DIV>
<DIV class=left_12>【<A style="CURSOR: pointer"
onclick=window.external.addFavorite(document.location.href,document.title)>收藏此文</A>】【<A
href="javascript:doZoom(16)">大</A> <A href="javascript:doZoom(14)">中</A> <A
href="javascript:doZoom(12)">小</A>】【<A href="javascript:doPrint()">打印</A>】【<A
href="javascript:window.close()">关闭</A>】</DIV>
<DIV class=left_11><SPAN class=font_8>较早的文章:</SPAN><A title=深入浅出MFC文档/视图架构之相互关系
href="http://www.qqread.com/vc/t612917200.html"
target=_blank>深入浅出MFC文档/视图架构之相互关系</A><BR><BR><SPAN class=font_8>较新的文章:</SPAN><A
title=如何在C语言中巧用正则表达式 href="http://www.qqread.com/vc/s332918200.html"
target=_blank>如何在C语言中巧用正则表达式</A><BR></DIV>
<DIV class=left_11 align=center>
<SCRIPT src="深入浅出VC++串口编程之基于控件_QQread_com_files/adtitle.js"
type=text/javascript></SCRIPT>
</DIV>
<DIV class=left_10>
<DIV class=left_7><SPAN class=font_10>相关图文阅读</SPAN></DIV>
<DIV class=left_8><IFRAME marginWidth=0 marginHeight=0
src="深入浅出VC++串口编程之基于控件_QQread_com_files/travel8.htm" frameBorder=0 width=568
scrolling=no height=118></IFRAME></DIV>
<DIV class=left_7><SPAN class=font_10>频道图文推荐</SPAN></DIV>
<DIV class=left_picasa id=picasa_pic></DIV>
<DIV class=left_9>
<DIV class=left_9_1><SPAN class=font_3>健 康 咨 询</SPAN></DIV>
<DIV class=left_9_2>
<SCRIPT src="深入浅出VC++串口编程之基于控件_QQread_com_files/jk.js"
type=text/javascript></SCRIPT>
</DIV></DIV>
<DIV class=left_9>
<DIV class=left_9_1><SPAN class=font_3>时 尚 咨 询</SPAN></DIV>
<DIV class=left_9_2>
<SCRIPT src="深入浅出VC++串口编程之基于控件_QQread_com_files/lux.js"
type=text/javascript></SCRIPT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -