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

📄 windows ce下的串口通信编程--2.htm

📁 WinCE下串口编程应用实例
💻 HTM
📖 第 1 页 / 共 5 页
字号:
class=headblack-s href="http://sh.yesky.com/">上海</A> <A class=headblack-s 
href="http://gd.yesky.com/">广东</A><BR><A class=headblack-s 
href="http://cq.yesky.com/">重庆</A> <A class=headblack-s 
href="http://cd.yesky.com/">成都</A> <A class=headblack-s 
href="http://sy.yesky.com/">沈阳</A> </DIV>
<DIV class=rightarea_right><A class=headblack-s-l 
href="http://my.yesky.com/">用户<BR>登录</A></DIV>
<DIV class=rightmenucontent><A class=headgreen 
href="http://www.chinabyte.com/">Chinabyte.com</A><SPAN 
style="FONT-SIZE: 9px">|</SPAN><A class=headblue 
href="http://www.techtarget.com.cn/">Techtarget.com.cn</A></DIV>
<DIV class=clear></DIV></DIV></DIV><!--头部结束-->
<DIV class=headclear></DIV><!--第一栏导航-->
<DIV class=box>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav06.gif" 
border=0></DIV>
<DIV id=ityj><A class=headblack-s href="http://www.chinabyte.com/"><SPAN 
style="COLOR: #07346e">IT业界</SPAN></A></DIV>
<DIV class=headbg1_1_1>
<DIV id=span_per_1><A class=headblack-s 
href="http://news.chinabyte.com/">新闻</A></DIV>
<DIV id=span_per_1><A class=headblack-s 
href="http://column.chinabyte.com/">评论</A></DIV>
<DIV id=span_per_1><A class=headblack-s 
href="http://telecom.chinabyte.com/">通信</A></DIV>
<DIV id=span_per_2><A class=headblack-s 
href="http://net.chinabyte.com/">互联网</A></DIV>
<DIV id=span_per_2><A class=headblack-s 
href="http://blog.chinabyte.com/">CB博客</A></DIV></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav07.gif" 
border=0></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav08.gif" 
border=0></DIV>
<DIV id=eqy><A class=headblack-s href="http://e.chinabyte.com/"><SPAN 
style="COLOR: #309045">E企业</SPAN></A></DIV>
<DIV class=headbg1_1_2>
<DIV id=span_m1per2_1><A class=headblack-s 
href="http://solution.chinabyte.com/">解决方案</A></DIV>
<DIV id=span_m1per2_1><A class=headblack-s 
href="http://biz.chinabyte.com/">商用系统</A></DIV>
<DIV id=span_m1per2_2><A class=headblack-s 
href="http://biz.chinabyte.com/cbfwq">服务器</A></DIV>
<DIV id=span_m1per2_3><A class=headblack 
href="http://com.chinabyte.com/">企业社区</A></DIV></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav07.gif" 
border=0></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav09.gif" 
border=0></DIV>
<DIV id=itzj><A class=headblack-s href="http://www.techtarget.com.cn/"><SPAN 
style="COLOR: #478e9a">IT专家</SPAN></A></DIV>
<DIV class=headbg1_1_3>
<DIV id=span_m1per3_1><A class=headblack-s 
href="http://searchdatabase.techtarget.com.cn/">数据库</A></DIV></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav07.gif" 
border=0></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav08.gif" 
border=0></DIV>
<DIV id=eqy1><A class=headblack-s href="http://edu.yesky.com/"><SPAN 
style="COLOR: #309045">教育</SPAN></A></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/zhy0728.gif" 
border=0></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav08.gif" 
border=0></DIV>
<DIV id=eqy1><A class=headblack-s href="http://data.chinabyte.com/"><SPAN 
style="COLOR: #309045">数据调查</SPAN></A></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/zhy0728.gif" 
border=0></DIV></DIV><!--第二栏导航-->
<DIV class=box>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav15.gif" 
border=0></DIV>
<DIV id=itjs><A class=headblack-s href="http://t.yesky.com/"><SPAN 
style="COLOR: #0051c0">IT技术</SPAN></A></DIV>
<DIV class=headbg2_1>
<DIV id=span_m2per1_w1><A class=headblack 
href="http://soft.yesky.com/">软件</A></DIV>
<DIV id=span_m2per1_w1><A class=headenglish 
href="http://soft.yesky.com/os/win/">Windows</A></DIV>
<DIV id=span_m2per1_w1><A class=headenglish 
href="http://myhard.yesky.com/diy">DIY</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/net">网管</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://design.yesky.com/">设计</A></DIV>
<DIV id=span_m2per1_w1><A class=headenglish 
href="http://design.yesky.com/photoshop/">Photoshop</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://telecom.chinabyte.com/NetCom/218449874989875200/index.shtml">交换</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://dev.yesky.com/">开发</A></DIV>
<DIV id=span_m2per1_w1><A class=headenglish 
href="http://dev.yesky.com/msdn/">MSDN</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://soft.yesky.com/security/">安全</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://edu.yesky.com/edupxpt">认证</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://bbs.yesky.com/">论坛</A></DIV></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav16.gif" 
border=0></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav17.gif" 
border=0></DIV>
<DIV id=szsh><A class=headblack-s href="http://i.yesky.com/"><SPAN 
style="COLOR: #d70418">数字生活</SPAN></A></DIV>
<DIV class=headbg2_2>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://e.yesky.com/">e时代</A></DIV>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://game.yesky.com/">游戏</A></DIV>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://pic.yesky.com/">图片</A></DIV>
<DIV id=span_m2per2_w2><A class=headblack-s 
href="http://desktop.yesky.com/">壁纸</A></DIV>
<DIV id=span_m2per2_w2><A class=headblack-s 
href="http://hot.yesky.com/">网摘</A></DIV>
<DIV id=span_m2per2_w2><A class=headblack-s 
href="http://blog.yesky.com/">博客</A></DIV>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://www.mydown.com/">下载</A></DIV></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav16.gif" 
border=0></DIV></DIV><!--第三栏导航-->
<DIV class=box>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav19.gif" 
border=0></DIV>
<DIV id=xfdh><A class=headblack-s href="http://cseek.yesky.com/"><SPAN 
style="COLOR: #f04400">消费导购</SPAN></A></DIV>
<DIV class=headbg3>
<DIV id=span_m3per1_w1><A class=headblack 
href="http://cseek.yesky.com/">商情</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack 
href="http://mobile.yesky.com/">手机</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack 
href="http://digital.yesky.com/">数码:</A></DIV>
<DIV id=span_m3per1_w3><A class=headblack-s href="http://mp3.yesky.com/"><SPAN 
class=fontsize>MP3</SPAN>随身听</A></DIV>
<DIV id=span_m3per1_w3><A class=headblack-s 
href="http://dc.yesky.com/">数码相机</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack-s 
href="http://dv.yesky.com/">摄像机</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack 
href="http://notebook.yesky.com/">笔记本</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack 
href="http://myhard.yesky.com/">硬件:</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack-s 
href="http://myhard.yesky.com/pc/">品牌机</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/acceessory/">配件</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/oa">外设</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/net/">网络</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/vga/">显卡</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/projector/">投影</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack-s 
href="http://myhard.yesky.com/scanner/">扫描仪</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/laserjet/">激打</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s 
href="http://myhard.yesky.com/inkjet/">喷打</A></DIV>
<DIV id=span_m3per1_w3><A class=headblack 
href="http://dh.yesky.com/">数字家庭</A></DIV></DIV>
<DIV class=left><IMG src="Windows CE下的串口通信编程--2.files/index-nav12.gif" 
border=0></DIV></DIV><!--菜单导航结束-->
<DIV class=clear1></DIV><!--笔记本导航条-->
<DIV class=newbox>
<DIV class=newbgpic>
<DIV class=left>您现在的位置:<A href="http://www.yesky.com/"> Yesky</A>&gt;<A 
href="http://soft.yesky.com/"> 软件</A>&gt;<A href="http://dev.yesky.com/"> 
开发者网络</A> </DIV>
<DIV class=right>
<DIV class=space1><SPAN id=ad3></SPAN></DIV></DIV></DIV></DIV>
<DIV class=clear1></DIV><!--结束-->
<DIV class=clear1></DIV><!--文章内容区-->
<DIV class=newbox>
<DIV class=newleft>
<DIV class=newleft1>
<DIV class=newsmallbox>Windows CE下的串口通信编程 </DIV>
<DIV class=clear1></DIV>
<DIV class=newsmall1box>
<DIV class=newtitle1>作者: 付林林 </DIV>
<DIV class=newtitle2>出处: 天极网 </DIV>
<DIV class=newtitle3>责任编辑: 方舟 </DIV>
<DIV class=newtitle4>[ 2004-08-23 18:45 ]</DIV></DIV>
<DIV class=clear1></DIV>
<DIV class=space2><SPAN id=ad5></SPAN></DIV><BR>
<DIV class=newmiddlebox style="OVERFLOW: hidden">
<DIV class=guanggao><SPAN 
id=ad4></SPAN></DIV>  <STRONG>二、关闭串口</STRONG><BR><BR>  关闭串口用关闭句柄函数。<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
  <TBODY>
  <TR>
    <TD>if(hSerial != NULL) <BR>{<BR> CloseHandle(hSerial);<BR> hSerial = 
      NULL;<BR>}</TD></TR></TBODY></TABLE><BR>  <STRONG>三、向串口发送数据</STRONG><BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
  <TBODY>
  <TR>
    <TD>WriteFile (hSerial, // 句柄<BR> &amp;Byte, // 数据缓冲区地址<BR> nByte, // 
      数据大小<BR> &amp;dwNumBytes, // 返回发送出去的字节数<BR> NULL // 
  不支持重叠<BR>);</TD></TR></TBODY></TABLE><BR>  向串口发送数据一般都会成功。需要注意的是如果终端设备需要一定处理时间或者称反应时间的话,那么两个写操作之间一定要注意时间间隔不能太小。具体的时间由终端设备的反应时间和缓冲区大小有关。<BR><BR>  <STRONG>四、读取串口数据</STRONG><BR><BR>  串口麻烦就麻烦在读取数据上。除了考虑及时的读取数据外,还要解决接收到的数据的处理工作。如果在读取串口数据的线程中安置数据处理工作,那么可能会丢失数据(终端设备发送数据但是没收到),也有可能不会丢失(终端设备发送的数据的时间、大小都是确定的)。如果肯定接收的数据在处理工作结束后终端设备才发送数据,那么完全可以将数据处理工作放在读取串口的线程中。对于及时的读取数据,下面提供了一种解决办法:<BR><BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
  <TBODY>
  <TR>
    <TD>*** 假设接收的都是字符 ***<BR><BR>UINT ReadThread(LPVOID pParam) 
      ////接收串口数据线程<BR>{ <BR> HANDLE hPort = *(HANDLE*)pParam;<BR> BYTE 
      Byte;<BR> int iCounter = 0;<BR> DWORD dwBytes;<BR> char ReceiveBuf[1000]; 
      ///缓冲区的大小 <BR><BR> SetCommMask (hPort, EV_RXCHAR); ///只接收字符<BR> while 
      (hPort != INVALID_HANDLE_VALUE) <BR> {<BR>  DWORD 
      dwCommStatus;<BR>  WaitCommEvent(hPort, &amp;dwCommStatus, 
      0);<BR>  SetCommMask (hPort, EV_RXCHAR); ///重新设置要等待的信号<BR>  //// 
      接收数据<BR>  do <BR>  {<BR>   ReadFile(hPort, &amp;Byte, 1, &amp;dwBytes, 0); 
      <BR>   if(dwBytes == 1)<BR>   {<BR>    ReceiveBuf[iCounter++] = 
      Byte;<BR>    if(iCounter == 
      1000)<BR>    {<BR>     ///L"接收缓冲区已满";<BR>     return -1;<BR>    }<BR>   } 
      <BR>  } while (dwBytes == 1);<BR>  if(iCounter == 0) 
      ////没接到数据<BR>  {<BR>   continue;<BR>  }<BR>  //////保存数据<BR>  char* pTmp = 
      new char[iCounter + 1];<BR>  if(pTmp == 
      NULL)<BR>  {<BR>   ///L"内存不足,接收串口数据线程关闭";<BR>   return 
      -1;<BR>  }<BR>  memcpy(pTmp, ReceiveBuf, iCounter);<BR>  pTmp[iCounter] = 
      NULL; ////字符串结尾<BR>  ////////创建新线程处理数据 <BR>  AfxBeginThread(ProcessData, 
      pTmp); ////在ProcessData函数中处理数据。别忘了delete[] pTmp;<BR>  iCounter = 0; 
      ////清空计数器<BR> } ///////end while<BR> return 0;<BR>}</TD></TR></TBODY></TABLE>
<P><BR>  在ReadThread的代码中,对接收数据后的处理采用另外一个线程来执行,这种解决方法不适合所有需要,只适合读取终端设备返回来的少量数据。软件开发者要编写串口读线程代码应该具体问题具体分析。另外如果想在任意时刻停止读串口线程,应该按如下形式调用函数:</P>
<P>SetCommMask(hPort,EV_RXCHAR|EV_TXEMPTY);</P>
<P>  EV_TXEMPTY表示当输出字符都发送出去作为一个事件。那么我们可以发送任意一个字符,这个字符发送出去后WaitCommEvent就返回。<BR><BR>  付林林:<BR><BR>  2001年毕业,一直从事Windows 
CE下操作系统内核定制和应用软件开发工作,参与中国第一台Auto 
PC(车载电脑)的软件研发工作,希望和CE平台开发者交流、探讨。<BR><BR>  联系作者请邮至:fllsoft@sina.com或windowsce@tom.com;MSN:Messenger:windowsce@tom.com<BR><BR><BR></P>
<DIV class=right>共2页。 <A href="http://www.yesky.com/194/1845194.shtml" 
target=_self><FONT face=Webdings>9</FONT></A> <A 
href="http://www.yesky.com/194/1845194.shtml" target=_self><FONT 
face=Webdings>7</FONT></A> <A href="http://www.yesky.com/194/1845194.shtml" 
target=_self>1</A> <A href="http://www.yesky.com/194/1845194_1.shtml" 
target=_self><FONT color=#ff0000><B>2</B></FONT></A></DIV></DIV>
<DIV class=clear1></DIV></DIV><!--结束-->
<DIV class=clear1></DIV>
<DIV class=newleft1>
<DIV class=space2><SPAN id=ad7></SPAN></DIV></DIV><!--文章评论区-->
<SCRIPT language=JavaScript>
<!--
var aid = 1845194;
var channelid = 324;

//-->
</SCRIPT>

<DIV class=newxiangguan>
<DIV class=newxiangguanmenu><A href="http://my.yesky.com/" 
target=_blank>天极社区</A>邀请您:<A class=red-s href="http://blog.yesky.com/" 
target=_blank>写博客</A>&nbsp;<A class=red-s href="http://photo.yesky.com/" 
target=_blank>上传相片</A>&nbsp;<A class=red-s href="http://bbs.yesky.com/" 
target=_blank>论坛聊天</A>&nbsp;<A class=red-s href="http://bbs.yesky.com/" 
target=_blank>订阅电子杂志</A>&nbsp;<A class=red-s 
href="http://www.yeecai.com/mobileant/tjadd/tjload.html" 
target=_blank>彩信蚂蚁</A>&nbsp;<A class=red-s 
href="http://hot.yesky.com/">推荐网摘</A>&nbsp;<A class=red-s 
href="http://training.yesky.com/">IT培训</A>&nbsp;<A class=red-s 
href="javascript:sendemail()" target=_self>对此感兴趣</A>&nbsp;<A 
title="推荐到天极网摘 http://hot.yesky.com [鼠标划选本文摘要,然后点击本图片]" 
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(yesky=window.open('http://hot.yesky.com/dp.aspx?t='+escape(d.title)+'&amp;u='+escape(d.location.href)+'&amp;c='+escape(t)+'&amp;st=2','yesky','scrollbars=no,width=400,height=480,left=75,top=20,status=no,resizable=yes'));yesky.focus();" 
target=_self><IMG src="Windows CE下的串口通信编程--2.files/souchang1.gif" border=0></A> 
</DIV></DIV>
<DIV class=pinlunbox>
<DIV class=pinlunguan3><SPAN id=commentsSpanText>
<SCRIPT language=JavaScript><!--function getTagId(tName) {	tName = tName.replace(/(^[\s ]*)|([\s ]*$)/g, "");	var tagid = 0;	for(var ti = 0; ti < tName.length; ti++) {		tagid += tName.charCodeAt(ti);	}	return tagid;}function UrlEncode(str){   var i,temp,p,q;   var result="";   str = str.replace(/(^[\s ]*)|([\s ]*$)/g, "");   for(i=0;i<str.length;i++){      temp = str.charCodeAt(i);      if(temp>=0x4e00){

⌨️ 快捷键说明

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