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

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

📁 串口编程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
      )<BR> {<BR>  dwEvtMask = 0 ;<BR>  WaitCommEvent( pCom-&gt;m_hCom, 
      &amp;dwEvtMask, NULL );<BR>  if ((dwEvtMask &amp; EV_RXCHAR) == 
      EV_RXCHAR)<BR>  {<BR>   if ((nLength = ReadCommBlock( *pCom, (LPSTR) buf, 
      255 )))<BR>   {<BR>    //WriteTTYBlock( hTTYWnd, (LPSTR) abIn, nLength ) 
      ;<BR>    buf[nLength]='\0';<BR>    AfxMessageBox(buf);<BR>   }<BR>  }<BR> }<BR> CloseHandle( 
      os.hEvent ) ;<BR> return( TRUE ) ;<BR>} // end of 
      CommWatchProc()<BR><BR>int ReadCommBlock(CComStatus&amp; comDev,LPSTR 
      lpszBlock, int nMaxLength )<BR>{<BR> BOOL fReadStat ;<BR> COMSTAT ComStat 
      ;<BR> DWORD dwErrorFlags;<BR> DWORD dwLength;<BR> DWORD 
      dwError;<BR><BR> char szError[ 10 ] ;<BR><BR> // only try to read number 
      of bytes in queue<BR><BR> ClearCommError( comDev.m_hCom, 
      &amp;dwErrorFlags, &amp;ComStat ) ;<BR> dwLength = min( (DWORD) 
      nMaxLength, ComStat.cbInQue ) ;<BR><BR> if (dwLength &gt; 
      0)<BR> {<BR>  fReadStat = ReadFile( comDev.m_hCom, lpszBlock,dwLength, 
      &amp;dwLength, &amp;(comDev.m_rdos) ) ;<BR>  if 
      (!fReadStat)<BR>  {<BR>   if (GetLastError() == 
      ERROR_IO_PENDING)<BR>   {<BR>    OutputDebugString("\n\rIO 
      Pending");<BR>    while(!GetOverlappedResult( comDev.m_hCom 
      ,&amp;(comDev.m_rdos), &amp;dwLength, TRUE ))<BR>    {<BR>     dwError = 
      GetLastError();<BR>     if(dwError == ERROR_IO_INCOMPLETE)<BR>      // 
      normal result if not 
      finished<BR>      continue;<BR>     else<BR>     {<BR>      // an error 
      occurred, try to recover<BR>      wsprintf( szError, "&lt;CE-%u&gt;", 
      dwError ) ;<BR>      ClearCommError( comDev.m_hCom , &amp;dwErrorFlags, 
      &amp;ComStat ) 
      ;<BR>      break;<BR>     }<BR>    }<BR>   }<BR>   else<BR>   {<BR>    // 
      some other error occurred<BR>    dwLength = 0 ;<BR>    ClearCommError( 
      comDev.m_hCom , &amp;dwErrorFlags, &amp;ComStat ) 
      ;<BR>   }<BR>  }<BR> }<BR> return ( dwLength ) ;<BR>} // end of 
      ReadCommBlock()<BR><BR>int ReadCommBlockEx(CComStatus&amp; comDev,LPSTR 
      lpszBlock, int nMaxLength,DWORD dwTimeOut)<BR>{<BR> LPSTR 
      lpOffset=lpszBlock;<BR> int nReadCount = 0;<BR> char chBuf;<BR> //time_t 
      beginTime,endTime;<BR> if(!comDev.m_hCom)<BR>  return 0;<BR> if(dwTimeOut 
      &lt;= 0)<BR>  return 0;<BR> MSG msg;<BR> //time(&amp;beginTime);<BR> DWORD 
      dwLastTick,dwNowTick,dwGoneTime;<BR> dwGoneTime = 0;<BR> dwLastTick = 
      GetTickCount();<BR> dwNowTick = dwLastTick;<BR> // double 
      diftime;<BR> do<BR> {<BR>  if(PeekMessage(&amp;msg,NULL,0,0,PM_REMOVE))<BR>  {<BR>   ::TranslateMessage(&amp;msg);<BR>   ::DispatchMessage(&amp;msg);<BR>  }<BR>  if(ReadCommBlock(comDev,&amp;chBuf,1) 
      &gt; 0)<BR>  {<BR>   //TRACE("----get a char----\n");<BR>   *lpOffset = 
      chBuf;<BR>   lpOffset ++;<BR>   nReadCount ++;<BR>  }<BR>  dwNowTick = 
      GetTickCount();<BR>  if(dwNowTick &lt; dwLastTick)<BR>  {<BR>   dwLastTick 
      = dwNowTick;<BR>  }<BR><BR>  dwGoneTime = dwNowTick - 
      dwLastTick;<BR><BR>  //TRACE("gon time = 
      %lu\n",dwGoneTime);<BR><BR> }while((nReadCount &lt; nMaxLength) &amp;&amp; 
      (dwGoneTime &lt; dwTimeOut));<BR> return (nReadCount);<BR>}//end 
      ReadCommBlockEx<BR><BR>BOOL WriteCommBlock( CComStatus&amp; comDev, LPSTR 
      lpByte , DWORD dwBytesToWrite)<BR>{<BR> BOOL fWriteStat ;<BR> DWORD 
      dwBytesWritten ;<BR> DWORD dwErrorFlags;<BR> DWORD dwError;<BR> DWORD 
      dwBytesSent=0;<BR> COMSTAT ComStat;<BR><BR> char szError[ 128 ] 
      ;<BR><BR> fWriteStat = WriteFile( comDev.m_hCom , lpByte, 
      dwBytesToWrite,&amp;dwBytesWritten, &amp;( comDev.m_wtos) ) ;<BR> if 
      (!fWriteStat)<BR> {<BR>  if(GetLastError() == 
      ERROR_IO_PENDING)<BR>  {<BR>   while(!GetOverlappedResult( 
      comDev.m_hCom,&amp;(comDev.m_wtos), &amp;dwBytesWritten, TRUE 
      ))<BR>   {<BR>    dwError = GetLastError();<BR>    if(dwError == 
      ERROR_IO_INCOMPLETE)<BR>    {<BR>     // normal result if not 
      finished<BR>     dwBytesSent += 
      dwBytesWritten;<BR>     continue;<BR>    }<BR>    else<BR>    {<BR>     // 
      an error occurred, try to recover<BR>     wsprintf( szError, 
      "&lt;CE-%u&gt;", dwError ) ;<BR>     ClearCommError( comDev.m_hCom, 
      &amp;dwErrorFlags, &amp;ComStat ) 
      ;<BR>     break;<BR>    }<BR>   }<BR>   dwBytesSent += 
      dwBytesWritten;<BR>   if( dwBytesSent != dwBytesToWrite 
      )<BR>    wsprintf(szError,"\nProbable Write Timeout: Total of %ld bytes 
      sent", dwBytesSent);<BR>   else<BR>    wsprintf(szError,"\n%ld bytes 
      written", 
      dwBytesSent);<BR>   OutputDebugString(szError);<BR>  }<BR>  else<BR>  {<BR>   // 
      some other error occurred<BR>   ClearCommError( comDev.m_hCom, 
      &amp;dwErrorFlags, &amp;ComStat ) ;<BR>   return ( FALSE 
      );<BR>  }<BR> }<BR> return ( TRUE ) ;<BR>} // end of 
  WriteCommBlock()</TD></TR></TBODY></TABLE><BR>  <B>四、小结 
</B><BR><BR>  以上给出了用Win32 API设计串行通信的基本思路,在实际应用中,我们可以利用Win32 API设计出满足各种需要的串行通信程序。 
<BR><BR>
<TABLE borderColor=#000000 cellSpacing=4 width="90%" align=center border=1>
  <TBODY>
  <TR>
    <TD>
      <DIV align=center><B>编辑推荐:<A 
      href="http://soft.yesky.com/lesson/network/vcnetwork/" target=_blank><FONT 
      color=#cc3333>VC网络及通讯编程</FONT></A></B></DIV></TD></TR></TBODY></TABLE>
<DIV class=right>共2页。 <A class=page 
href="http://soft.yesky.com/50/2214050.shtml" target=_self><FONT 
face=Webdings>9</FONT></A> <A class=page 
href="http://soft.yesky.com/50/2214050.shtml" target=_self><FONT 
face=Webdings>7</FONT></A> <A class=page 
href="http://soft.yesky.com/50/2214050.shtml" target=_self><B>1</B></A> <A 
class=page href="http://soft.yesky.com/50/2214050_1.shtml" target=_self><FONT 
color=#666666><B>2</B></FONT></A></DIV></DIV><!--广告代码开始-->
<SCRIPT>
var ad_cid;
if (window.location.search.substring(1) != "")
{
ad_cid = window.location.search.substring(1);
} else {
ad_cid = 324;
}
</SCRIPT>
<!--小通栏:c-scolumn顶部500*90--><SPAN id=span_ad1>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-scolumn.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad1").innerHTML=document.getElementById("span_ad1").innerHTML;
document.getElementById("span_ad1").innerHTML="";
</SCRIPT>
<!--c-pip文章正文中间240*200--><SPAN id=span_ad3>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-pip.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT>
if(document.getElementById("contentAdv")) {
document.getElementById("contentAdv").innerHTML=document.getElementById("span_ad3").innerHTML;
document.getElementById("span_ad3").innerHTML="";
} else {
document.getElementById("ad3").innerHTML=document.getElementById("span_ad3").innerHTML;
document.getElementById("span_ad3").innerHTML="";
}</SCRIPT>
<!--c-text-1“您现在的位置”右侧文字--><SPAN id=span_ad2>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-text-1.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad2").innerHTML=document.getElementById("span_ad2").innerHTML;
document.getElementById("span_ad2").innerHTML="";
</SCRIPT>
<!--c-column-2“标题”上面的广告--><SPAN id=span_ad10>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-column-2.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad10").innerHTML=document.getElementById("span_ad10").innerHTML;
document.getElementById("span_ad10").innerHTML="";
</SCRIPT>
<!--c-column-1“摘要”下面的广告--><SPAN id=span_ad9>
<DIV id=ad9ad>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-column-1.js\"><\/script>");</SCRIPT>
</DIV></SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad9").innerHTML=document.getElementById("span_ad9").innerHTML;
document.getElementById("span_ad9").innerHTML="";
</SCRIPT>
<!--广告代码结束-->
<DIV id=ad7ad><SPAN id=ad7></SPAN></DIV>
<DIV class=clear></DIV>
<DIV id=conlink>
<SCRIPT type=text/javascript>var clipBoardContent = document.URL;  function copyUserHomeToClipBoard()  {    window.clipboardData.setData("Text",clipBoardContent);  alert("复制成功,请粘贴到你的QQ/MSN上推荐给你的好友!\r\n\r\n内容如下:\r\n" + clipBoardContent);  }</SCRIPT>

<FORM id=feedbackForm name=feedbackForm 
action=http://feedback.yesky.com/reader/user/doWelcome.view target=_blank><INPUT 
id=refurl type=hidden name=refurl> <INPUT id=reftitle type=hidden name=reftitle> 
</FORM>
<SCRIPT type=text/javascript>document.write("<a href=\"http:\/\/dig.yesky.com\" id=\'yeskyDig\'  onclick=\"javascript:this.href=\'http:\/\/dig.yesky.com\/ding.php?t=\'+encodeURIComponent(document.title)+\'&u=\'+escape(document.location.href)+\'&ac=ding\';\" target=_blank title=\"此文不错,我顶!\"><b>我顶一下<\/b><\/a> <a onclick='javascript:submitFeedback();' style='cursor:hand;'><font color='#FF0000'><b>我要挑错</b></font></a> <a href=\"#top\" target=\"_self\"><a 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)+'&u='+escape(d.location.href)+'&c='+escape(t)+'&st=2','_blank','scrollbars=no,width=400,height=480,left=75,top=20,status=no,resizable=yes'));yesky.focus();\"title=\"推荐到天极网摘 http://hot.yesky.com [鼠标划选本文摘要,然后点击本图片]\" target=\"_self\">收藏到天极收藏夹</a> <a href=\"JavaScript: copyUserHomeToClipBoard()\" target=\"_self\">复制链接发给好友</a> <a href=\"javascript:window.external.AddFavorite('"+clipBoardContent+"')\" target=\"_self\">加入收藏</a> <a href=\"javascript:window.print()\" target=\"_self\">打印</a> <img src=\"http://image.yesky.com/TLimages/newmobile/titlebg19.gif\" title=\"返回顶部\"/></a>");function submitFeedback() {	document.getElementById("refurl").value=document.location.href;	document.getElementById("reftitle").value=document.title;	document.getElementById("feedbackForm").submit();}</SCRIPT>
</DIV>
<DIV class=clear></DIV><!--评论-->
<SCRIPT language=JavaScript>
<!--
var aid = 2214050;
var channelid = 324;
var tagname = "VC";
//-->
</SCRIPT>

<SCRIPT language=JavaScript>
if(typeof(channelid) != "undefined") {
	document.write("<script language=\"JavaScript\" charset=\"gb2312\"  src=\"http://comments.yesky.com/channlereview/6/"+(channelid%5000)+"/"+channelid+".js\"><\/script>");
}
</SCRIPT>

<SCRIPT language=javascript>
<!--
if(typeof(aid) != "undefined") {
		document.write("<script language=\"JavaScript\" charset=\"gb2312\"  src=\"http://comments.yesky.com/articlereplies/6/"+(aid%5000)+"/"+aid+".js\"><\/script>");
 }
//-->
</SCRIPT>

<SCRIPT src="在VC++6_0中用Win32 API实现串行通信-软件-VC-天极Yesky.files/mobile_comment.js" 
type=text/javascript></SCRIPT>

<DIV class=clear></DIV><!--相关文章等-->
<DIV id=conrele>
<DIV class=xgleft id=__002>
<DIV class=xgmenus>相关文章</DIV>
<DT><!-- Error --></DT></DIV>
<DIV class=xgright id=__003>
<DIV class=xgmenus>最新更新</DIV>
<DT></DT>
<DD>·<A href="http://design.yesky.com/homepage/47/7777547.shtml">保护视力 
把网页底色变成浅绿色</A></DD>
<DD>·<A 
href="http://soft.yesky.com/info/486/7777486.shtml">第21次中国互联网络发展状况统计报告要点</A></DD>
<DD>·<A href="http://design.yesky.com/photoshop/402/7773402.shtml">Photoshop 
CS3合成实例:人间天堂</A></DD>
<DD>·<A 
href="http://design.yesky.com/show/89/7774089.shtml">北京奥运会徽“中国印”设计者郭春宁专访</A></DD>
<DD>·<A href="http://design.yesky.com/flash/235/7773735.shtml">Flash 
ActionScript函数制作雪花飘飘动画</A></DD>
<DD>·<A href="http://soft.yesky.com/355/7777855.shtml">Stanislav 
Katz趣味家居产品设计欣赏</A> </DD></DIV></DIV>
<DIV class=clear></DIV><!--天极无线-->
<DIV class=conpict>天极无线</DIV>
<DIV>
<DIV>
<OBJECT id=templink type=application/x-shockwave-flash height=100 width=640 
data=http://image.yesky.com/TLimages/include/other/mobile_ad.swf></OBJECT></DIV></DIV></DIV><!--右边开始-->
<DIV id=conright>
<DIV id=adgg5>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-pip1.js\"><\/script>");</SCRIPT>
</DIV>

⌨️ 快捷键说明

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