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

📄 pc.c

📁 PC机与单片机之间进行通讯连接的C51代码和PC机的VC++代码
💻 C
字号:
/***************************/
/*PC机与8051的C语言通讯程序*/
/***************************/
/*主机PC的C语言程序*/
/*******************/
#include <stdio.h>
#include <bios.h>
#include <conio.h>
#include <ctype.h>
#include <process.h>
#include <dos.h>
/***************************/
char *fname;
char sport(char ch);
void rport();
void sendef(char *fname);
/***************************/
int a
/***************************/
char sport(char ch)
{
	while(!(bioscom(3,0,0)&0x2000))  /*操作指定的串行通讯口函数,
	bioscom(int cmd,char byte,int port),p.348,  3--返回接口状态
	0x2000--发送保持器空状态位*/
	{
		if(kbhit())
		{getch();
	     exit(1);
		}
	}
	if(bioscom(1,ch,0)&0x8000)     /*&--按位与操作符
	0x8000--超时错误状态位,     1--发送一字符*/
	{
		printf("超时错误\n");
		exit(1);
	}
}

void rport()
{
	/*int a;*/
	while(!(bioscom(3,0,0)&0x0100))  /*0x0100--终止检验错误*/
	{
		if(kbhit())
		{getch();
		exit(1);
		}
	}
	a=bioscom(2,0,0)&0x00ff;         /* 2--接收一字符,
	0x00ff--低8位字节中存放由通讯口接收到的数据*/
	return;
}

void sendf(char *fname)
{
	FILE *fp;                       /*文件类型,属性指针变量*/
	char ch;
	int handle,count,temp,sum=0;
	fp=fopen(fname,"r");            /*以只读方式打开文件*/
	handle=fileno(fp);              /*返回 fp->文件的句柄*/
	count=filelength(handle);       /*取文件总字节数*/
	temp=count;
	sport(count);                   /*发送字节总数*/
rep:for(;count;count--)             /*循环发送字符*/
	{
		ch=getc(fp);                /*从文件中取一字符*/
		sum=sum+ch;
		sport(ch);
	}
	sport(sum);                     /*发送累加和*/
	if(rport()=="F")                /*发送错误重发*/
	{
		sum=0;
		fseek(fp,-count,1);         /*fseek(FILE *stream,long offset,int origin)   p.260
		依照offset(偏移量)和origin(其始位置)的值,来设定与stream相连接的文件的位置指示器*/
		count=temp;
		goto rep;
	}
	else
	{
		fclose(fp);
		printf("发送文件结束\n");
	}
}

void receivef(char *fname)
{
	FILE *fp;
	char ch;
	int count,temp,sum=0;
	remove(fname);                /* remove--删除一文件 */
    fp=fopen(fname,"w");
	temp=rport();                 /*收总字节数*/
	count=temp;
rep:for(;count;count--)           /*循环接收字节*/
	{
		ch=rport();
		putc(ch,fp);              /*将一字节写入文件,putc()--将字符输出到指定的文件*/
		sum=sum+ch;               /*累加效验和*/
	}
	if(rport()!=sum)              /*接收有错误重收*/
	{
		ch="F";
		sport(ch);
		count=temp;
		sum=0;
		fseek(fp,-count,1);
		goto rep;
	}
	else
	{
		ch='0';
		sport(ch);
		fclose(fp);
		printf("接收文件结束\n");
	}
}

main()
{
	int addr;
	char ch;
	char command;            /*命令字--S or R*/
	outportb(0x3fb,0x80);    /* 8250 初始化*/
	outportb(0x3f8,0x30);    /* outportb(int port,char byte)--向指定的port(接口)输出指定的字节*/
	outportb(0x3f9,0x00);
	outportb(0x3fb,0x2b);
	clrscr();                /*清除文本模式窗口 void clrscr(void)*/
	gotoxy(3,4);
	textcolor(223);
	cprintf("输入8051地址:");
	sport(addr);
	while(rport()!=addr)
	{
		ch=addr;
		sport(ch);
	}
	gotoxy(3,6);
	outport(0x3fb,0x3b);
	cprintf("输入命令:\n");
	command=getch();
	cprintf("%\n",command);
	sport(command);
	if(command=='S')
	{
		gotoxy(3,8);
		cprintf("接收文件名:");
		scanf("%s",fname);
		cprintf("%s\n",fname);
		receivef(fname);
	}
	else
	{
		gotoxy(3,8);
		cprintf("发送文件名:");
		scanf("%s",fname);
		cprintf("%s\n",fname);
		sendf(fname);
	}
}

⌨️ 快捷键说明

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