📄 c_comport.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=(0046)http://edu.yesky.com/edupxpt/465/2137965.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>用C语言编写串口程序-培训辅导-编程-天极网</TITLE>
<META content=用C语言编写串口程序, name=description>
<META content=用C语言编写串口程序, name=keywords>
<META content="天极网 | 全球中文IT第一门户" name=author>
<META content="天极网 | 全球中文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="用C语言编写串口程序-培训辅导-编程-天极网.files/y_mobile_content.css" type=text/css
rel=stylesheet>
<SCRIPT src="用C语言编写串口程序-培训辅导-编程-天极网.files/showlay.js"></SCRIPT>
<META content="MSHTML 6.00.2900.3086" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV>
<SCRIPT src="用C语言编写串口程序-培训辅导-编程-天极网.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="用C语言编写串口程序-培训辅导-编程-天极网.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://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A
href="http://js.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="用C语言编写串口程序-培训辅导-编程-天极网.files/content_text_ad.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="用C语言编写串口程序-培训辅导-编程-天极网.files/flash.js"
type=text/javascript></SCRIPT>
<DIV id=childmenu><A href="http://soft.yesky.com/" target=_self>软件频道></A><A
href="http://edu.yesky.com/" target=_self>IT教育></A><A
href="http://edu.yesky.com/edupxpt" target=_self>IT培训辅导</A><A
href="http://itedu.yesky.com/edu.itschool.do" target=_self>名校展台</A><A
href="http://itedu.yesky.com/course.index.do" target=_self>精品课程</A><A
href="http://itedu.yesky.com/" target=_self>培训机构登录</A>|<A
href="http://soft.yesky.com/lesson/" target=_self>软件频道产品中心</A><A
href="http://q.yesky.com/group/search.do?cid=661" target=_self>群乐</A><A
href="http://www.mydown.com/soft/" target=_self>软件下载</A><<A
href="http://q.yesky.com/group/202019" target=_self><FONT
color=#ff0000>群乐:Vista系统</FONT></A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/" target=_self>天极网</A>
> <A href="http://soft.yesky.com/" target=_self>软件频道</A> > <A
href="http://edu.yesky.com/" target=_self>IT教育频道</A> > <A
href="http://edu.yesky.com/edupxpt" target=_self>培训辅导</A> > 用C语言编写串口程序</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://edu.yesky.com/edupxpt/465/2137965.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>用C语言编写串口程序</H1></DIV>
<DIV id=conauthor><SPAN>2005-09-28 13:48 </SPAN><SPAN>作者: 王卫东 </SPAN><SPAN>出处:
豆豆技术网 </SPAN><SPAN>责任编辑:<A title=向本编辑提问
href="http://comments.yesky.com/t/ÍõÓñº­/6,1037/2137965.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>
<P> 在当今,流行的编程软件种类繁多,它们编程方便、易于维护,但是在与硬件直接打交道和编制系统软件时却束手无策,于是C语言就有了用武之地。C语言作为汇编语言与高级语言之间的一种过渡语言,兼有汇编语言的高效和高级语言的方便。</P>
<P> 在通讯中,为了保证行运安全可靠,标准的串行口必须具有许多握手信号和状态信息。这是因为通讯的各个计算机CPU速度不一样(这会导致“错帧”)以及发送机发送数据速度比接收机接收速度快(这会导致“过冲”)。为解决这个问题,我们采用一个简单的握手信号,即发送机每次仅发送半个字节(低4位)的数据,而另外半个字节(高4位)则用来传送信息。我们可以对信息位(高4位)进行如下简单的编码:</P>
<P> 0H:发送的是新的半个字节数据</P>
<P> 1H:重新发送上次传送错误的数据</P>
<P> 2H:文件名结束</P>
<P> 3H:文件结束</P>
<P> 这样,每当发送机发送一个字节以后,就等待接受机发回送信号,这回送信号就是发送机发送过来的那个字节。发送机接收到回送信号后,把它与刚发送的字节相比较,如果相同,就发送新的半个字节,否则就重新发送。新数据与旧数据通过信息位来区分。下面就是用C语言编写控制串行口的程序。</P>
<P> #include "dos.h"</P>
<P> #include "stdlib.h"</P>
<P> #include "stdio.h"</P>
<P> #define PORT 0</P>
<P> void SendFile(char *fname); /* 发送文件*/</P>
<P> void Send(int s); /*发送一个字节*/</P>
<P> void SendFileName(char *fname); /*发送文件名*/</P>
<P> void ReceiveFile(); /*接收文件*/</P>
<P> void GetFileName(char *f); /*接收文件名*/</P>
<P> void InitPort(int port,unsigned char para); /*初始化端口*/</P>
<P> void SendPort(int port,char c); /*端口发送*/</P>
<P> int ReadPort(int port); /*读端口字节*/</P>
<P> int CheckState(int port); /*检查端口状态*/</P>
<P> int Receive(int port,int *G); /*接收一个字节*/</P>
<P> main(int argc,char *argv[])</P>
<P> {</P>
<P> if(argc<2){</P>
<P> printf("Please input R(receive) or S(sent) parametre:");</P>
<P> exit(1);</P>
<P> }</P>
<P> InitPort(PORT,231);</P>
<P> if(*argv[1]==''''S'''') /*检查选择的有效性*/</P>
<P> SendFile(argv[2]);</P>
<P> else if(*argv[1]==''''R'''')</P>
<P> ReceiveFile();</P>
<P> else{</P>
<P> printf("Error parament.Please input again.");</P>
<P> exit(1);</P>
<P> }</P>
<P> }</P>
<P> void SendFile(char *fname)</P>
<P> {</P>
<P> FILE *fp;</P>
<P> int ch,s;</P>
<P> if((fp=fopen(fname,"rb"))==NULL)</P>
<P> {</P>
<P> printf("Can''''t open the file.\n");</P>
<P> exit(1);</P>
<P> }</P>
<P> SendFileName(fname);</P>
<P> do{</P>
<P> ch=(int)getc(fp);</P>
<P> if(ferror(fp)){</P>
<P> printf("Error reading file.\n");</P>
<P> break;</P>
<P> }</P>
<P> s=ch%16; /*取文件中一个字节的低4位*/</P>
<P> Send(s);</P>
<P> s=ch/16; /*取文件中一个字节的高4位*/</P>
<P> Send(s);</P>
<P> }while(!feof(fp));</P>
<P> s=46; /*发送文件结束信息*/</P>
<P> Send(s);</P>
<P> Send(s);</P>
<P> fclose(fp);</P>
<P> }</P>
<P> void Send(s)</P>
<P> int s;</P>
<P> {</P>
<P> int G;</P>
<P> SendPort(PORT,s);</P>
<P> G=ReadPort(PORT); /*等待握手信号*/</P>
<P> if(s!=G)</P>
<P> s=s+16;</P>
<P> do{</P>
<P> SendPort(PORT,s);</P>
<P> G=ReadPort(PORT);/*等待握手信号*/</P>
<P> }while(s!=G);</P>
<P> }</P>
<P> void SendFileName(fname)</P>
<P> char *fname;</P>
<P> {</P>
<P> int s,ch;</P>
<P> printf("Now transmit the file.Please wait...");</P>
<P> while(*fname){</P>
<P> ch=(int)fname++;</P>
<P> s=ch%16; /*取文件名中一个字节的低4位*/</P>
<P> Send(s);</P>
<P> s=ch/16;</P>
<P> Send(s); /*取文件名中一个字节的低4位*/</P>
<P> }</P>
<P> s=32; /*发送文件名结束标志*/</P>
<P> Send(s);</P>
<P> Send(s);</P>
<P> }</P>
<P> void ReceiveFile(){</P>
<P> FILE *fp;</P>
<P> char ch;</P>
<P> int G1,G2,G3;</P>
<P> char fname[15];</P>
<P> GetFileName(fname);</P>
<P> printf("Receiving file %s.\n",fname);</P>
<P> remove(fname);</P>
<P> if((fp=fopen(fname,"wb"))==NULL)</P>
<P> {</P>
<P> printf("Can''''t open output file.\n");</P>
<P> exit(1);</P>
<P> }</P>
<P> /*循环为检测每次接受的数据是否为新数据,如果不是,*/</P>
<P> /*则用此次接收的数据覆盖上次接收的数据*/</P>
<P> G1=ReadPort(PORT);</P>
<P> G2=Receive(PORT,&G1);</P>
<P> do{</P>
<P> G3=Receive(PORT,&G2);</P>
<P> ch=(char)(G1%16+G2*16);/*恢复分开的数据,组合高4位和低4位*/</P>
<P> putc(ch,fp);</P>
<P> if(ferror(fp)){</P>
<P> printf("\nError writing file.");</P>
<P> exit(1);</P>
<P> }</P>
<P> G2=Receive(PORT,&G3);</P>
<P> G1=G3;</P>
<P> }while(G1/16!=48);</P>
<P> printf("\nTransmit finished.");</P>
<P> fclose(fp);</P>
<P> }</P>
<P> int Receive(port,G)</P>
<P> int port,*G;</P>
<P> {</P>
<P> int GM;</P>
<P> SendPort(port,*G);</P>
<P> GM=ReadPort(port);</P>
<P> if(GM/16==0)</P>
<P> return GM;</P>
<P> else if(GM/16==1){</P>
<P> do{</P>
<P> *G=GM;</P>
<P> SendPort(port,GM);</P>
<P> GM=ReadPort(port);</P>
<P> }while(GM/16==1);</P>
<P> }</P>
<P> return GM;</P>
<P> }</P>
<P> void GetFileName(char *f)</P>
<P> {</P>
<P> int G1,G2,G3;</P>
<P> char ch;</P>
<P> G1=ReadPort(PORT);</P>
<P> G2=ReadPort(PORT);</P>
<P> do{</P>
<P> G3=Receive(PORT,&G3);</P>
<P> ch=(char)(G1%16+G2/16);</P>
<P> *f=ch;</P>
<P> *f++;</P>
<P> G2=Receive(PORT,&G3);</P>
<P> G1=G3;</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -