⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 在vc++6_0中用win32 api实现串行通信-软件-vc-天极yesky111.htm

📁 串口编程
💻 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=(0038)http://soft.yesky.com/50/2214050.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>在VC++6.0中用Win32 API实现串行通信-软件-VC-天极Yesky</TITLE>
<META content="在VC++6.0中用Win32 API实现串行通信," name=description>
<META content="在VC++6.0中用Win32 API实现串行通信," name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all 
href="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky111.files/y_mobile_content.css" 
type=text/css rel=stylesheet>
<SCRIPT 
src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky111.files/showlay.js"></SCRIPT>

<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV>
<SCRIPT 
src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky111.files/kingsoft-kuanban-071105.js"></SCRIPT>
</DIV>
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A 
href="http://product.yesky.com/">产品报价</A>| <A 
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A> 
| <A href="http://digital.yesky.com/">数码</A> | <A 
href="http://notebook.yesky.com/">笔记本</A> | <A 
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A> 
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A> 
| <A href="http://dh.yesky.com/">数字家庭</A> | <A 
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> | 
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A> 
| <A href="http://pic.yesky.com/">图片</A> | <A 
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> | 
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A> 
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户 
src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky111.files/logo.gif" 
border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get 
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select() 
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden 
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A 
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广州</A> <A 
href="http://sz.yesky.com/">深圳</A><BR><A href="http://cd.yesky.com/">成都</A> <A 
href="http://cq.yesky.com/">重庆</A> <A href="http://sy.yesky.com/">沈阳</A> <A 
href="http://xa.yesky.com/">西安</A> </DIV>
<DIV class=rightarea_right><A 
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT 
src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky111.files/content_text_ad.js" 
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky111.files/flash.js" 
type=text/javascript></SCRIPT>

<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道&gt;</A><A 
href="http://design.yesky.com/">设计</A><A href="http://tools.yesky.com/">工具</A><A 
href="http://os.yesky.com/">系统</A><A href="http://dev.yesky.com/">开发</A><A 
href="http://soft.yesky.com/security/">安全</A><A 
href="http://soft.yesky.com/office/">办公</A><A 
href="http://homepage.yesky.com/">陶吧</A><A 
href="http://edu.yesky.com/">IT教育</A><A 
href="http://vista.yesky.com/">Vista频道</A>|<A 
href="http://soft.yesky.com/lesson/">软件频道产品中心</A><A 
href="http://q.yesky.com/group/search.do?cid=661">群乐</A><A 
href="http://www.mydown.com/soft/">软件下载</A><A 
href="http://q.yesky.com/group/202019"><FONT color=#ff0000>Vista群乐</FONT></A><A 
href="http://www.ibm.com/developerworks/cn/edu/j-dw-java-junit4.html?S_TACT=105AGX52&amp;S_CMP=top-yesky">深入探索JUnit 
4</A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/" target=_self>天极网</A> 
&gt; <A href="http://soft.yesky.com/" target=_self>软件频道</A> &gt; 在VC++6.0中用Win32 
API实现串行通信</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://soft.yesky.com/50/2214050.shtml#pls" 
target=_self>评论</A></DIV>
<DIV class=kr></DIV><!-- Error -->
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
<!-- Error --></DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>在VC++6.0中用Win32 API实现串行通信</H1></DIV>
<DIV id=conauthor><SPAN>2005-11-29 13:08 </SPAN><SPAN>作者: 刘涛 </SPAN><SPAN>出处: 
天极开发 </SPAN><SPAN>责任编辑:<A title=向本编辑提问 
href="http://comments.yesky.com/t/·&frac12;&Ouml;&Ucirc;/6,324/2214050.shtml" target=_blank>方舟</A> 
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN 
id=contentAdv></SPAN></DIV>  串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛。我们可以利用Windows 
API 提供的通信函数编写出高可移植性的串行通信程序。本实例介绍在Visual C++6.0下如何利用Win32 API 
实现串行通信程序。程序编译运行后的界面效果如图一所示: <BR><BR>
<TABLE width="90%" align=center border=0>
  <TBODY>
  <TR>
    <TD>
      <DIV align=center><IMG 
      src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky111.files/t5tg56vrnfhc.png" 
      border=0><BR>图一、串口通信示例程序</DIV></TD></TR></TBODY></TABLE><BR>  <B>一、实现方法</B><BR><BR>  在Win16中,可以利用OpenComm()、CloseComm()和WriteComm()等函数打开、关闭和读写串口。但在Win32中,串口和其他通信设备均被作为文件处理,串口的打开、关闭和读写等操作所用的API函数与操作文件的函数相同。可通过CreateFile()函数打开串口;通过CloseFile()函数关闭串口;通过DCB结构、CommProp()、GetCommProperties()、SetCommProperties()、GetCommState()及SetCommState()等函数设置串口状态,通过函数ReadFile()和WritFile()等函数读写串口。下面来详细介绍其实现原理。 
<BR><BR>  对于串行通信设备,Win32 
API支持同步和异步两种I/O操作。同步操作方式的程序设计相对比较简单,但I/O操作函数在I/O操作结束前不能返回,这将挂起调用线程,直到I/O操作结束。异步操作方式相对要复杂一些,但它可让耗时的I/O操作在后台进行,不会挂起调用线程,这在大数据量通信的情况下对改善调用线程的响应速度是相当有效的。异步操作方式特别适合同时对多个串行设备进行I/O操作和同时对一个串行设备进行读/写操作。<BR><BR>  串行设备的初始化 
<BR><BR>  串行设备的初始化是利用CreateFile()函数实现的。该函数获得串行设备句柄并对其进行通信参数设置,包括设置输出/接收缓冲区大小、超时控制和事件监视等。 
例如下面的代码实现了串口的初始化:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>//串行设备句柄; <BR><BR>HANDLE hComDev=0; <BR><BR>//串口打开标志; <BR><BR>BOOL 
      bOpen=FALSE; <BR><BR>//线程同步事件句柄; <BR><BR>HANDLE hEvent=0; <BR>DCB dcb; 
      <BR>COMMTIMEOUTS timeouts; <BR>//设备已打开 <BR>if(bOpen) return FALSE;  
      <BR>//打开COM1 
      <BR>if((hComDev=CreateFile(“COM1”,GENERIC

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -