面就是用C语言编写控制串行口的程序_第1页
面就是用C语言编写控制串行口的程序_第2页
面就是用C语言编写控制串行口的程序_第3页
面就是用C语言编写控制串行口的程序_第4页
面就是用C语言编写控制串行口的程序_第5页
已阅读5页,还剩5页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

面就是用C语言编写控制串行口的程序面就是用C语言编写控制串行口的程序面就是用C语言编写控制串行口的程序xxx公司面就是用C语言编写控制串行口的程序文件编号:文件日期:修订次数:第1.0次更改批准审核制定方案设计,管理制度面就是用C语言编写控制串行口的程序。

#include

""

#include

""

#include

""

#define

PORT

0

void

SendFile(char

*fname);

/*

发送文件*/

void

Send(int

s);

/*发送一个字节*/

void

SendFileName(char

*fname);

/*发送文件名*/

void

ReceiveFile();

/*接收文件*/

void

GetFileName(char

*f);

/*接收文件名*/

void

InitPort(int

port,unsigned

char

para);

/*初始化端口*/

void

SendPort(int

port,char

c);

/*端口发送*/

int

ReadPort(int

port);

/*读端口字节*/

int

CheckState(int

port);

/*检查端口状态*/

int

Receive(int

port,int

*G);

/*接收一个字节*/

main(int

argc,char

*argv[])

{

if(argc<2){

printf("Please

input

R(receive)

or

S(sent)

parametre:");

exit(1);

}

InitPort(PORT,231);

if(*argv[1]==''''S'''')

/*检查选择的有效性*/

SendFile(argv[2]);

else

if(*argv[1]==''''R'''')

ReceiveFile();

else{

printf("Error

input

again.");

exit(1);

}

}

void

SendFile(char

*fname)

{

FILE

*fp;

int

ch,s;

if((fp=fopen(fname,"rb"))==NULL)

{

printf("Can''''t

open

the

file.\n");

exit(1);

}

SendFileName(fname);

do{

ch=(int)getc(fp);

if(ferror(fp)){

printf("Error

reading

file.\n");

break;

}

s=ch%16;

/*取文件中一个字节的低4位*/

Send(s);

s=ch/16;

/*取文件中一个字节的高4位*/

Send(s);

}while(!feof(fp));

s=46;

/*发送文件结束信息*/

Send(s);

Send(s);

fclose(fp);

}

void

Send(s)

int

s;

{

int

G;

SendPort(PORT,s);

G=ReadPort(PORT);

/*等待握手信号*/

if(s!=G)

s=s+16;

do{

SendPort(PORT,s);

G=ReadPort(PORT);/*等待握手信号*/

}while(s!=G);

}

void

SendFileName(fname)

char

*fname;

{

int

s,ch;

printf("Now

transmit

the

wait...");

while(*fname){

ch=(int)fname++;

s=ch%16;

/*取文件名中一个字节的低4位*/

Send(s);

s=ch/16;

Send(s);

/*取文件名中一个字节的低4位*/

}

s=32;

/*发送文件名结束标志*/

Send(s);

Send(s);

}

void

ReceiveFile(){

FILE

*fp;

char

ch;

int

G1,G2,G3;

char

fname[15];

GetFileName(fname);

printf("Receiving

file

%s.\n",fname);

remove(fname);

if((fp=fopen(fname,"wb"))==NULL)

{

printf("Can''''t

open

output

file.\n");

exit(1);

}

/*循环为检测每次接受的数据是否为新数据,如果不是,*/

/*则用此次接收的数据覆盖上次接收的数据*/

G1=ReadPort(PORT);

G2=Receive(PORT,&G1);

do{

G3=Receive(PORT,&G2);

ch=(char)(G1%16+G2*16);/*恢复分开的数据,组合高4位和低4位*/

putc(ch,fp);

if(ferror(fp)){

printf("\nError

writing

file.");

exit(1);

}

G2=Receive(PORT,&G3);

G1=G3;

}while(G1/16!=48);

printf("\nTransmit

finished.");

fclose(fp);

}

int

Receive(port,G)

int

port,*G;

{

int

GM;

SendPort(port,*G);

GM=ReadPort(port);

if(GM/16==0)

return

GM;

else

if(GM/16==1){

do{

*G=GM;

SendPort(port,GM);

GM=ReadPort(port);

}while(GM/16==1);

}

return

GM;

}

void

GetFileName(char

*f)

{

int

G1,G2,G3;

char

ch;

G1=ReadPort(PORT);

G2=ReadPort(PORT);

do{

G3=Receive(PORT,&G3);

ch=(char)(G1%16+G2/16);

*f=ch;

*f++;

G2=Receive(PORT,&G3);

G1=G3;

}while(G1/16!=32);

printf("File

name

transmit

finished.\n");

}

void

InitPort(port,para)

int

port;

unsigned

char

para;

{

union

REGS

reg;

int86(0x14,®,®);

}

void

SendPort(port,c)

int

port;

char

c;

{

union

REGS

reg;

int86(0x14,®,®);

if&128){

printf("\nSend

mistakes!");

exit(1);

}

}

int

ReadPort(port)

int

port;

{

union

REGS

reg;

while(!(CheckState(port)&256)){

if(kbhit()){/*如端口长期无数据可人为终止等待*/

printf("Press

any

key

to

exit.");

getch();

ex

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论