单片机 串口通信 长字符串

#include <reg52.h>
/*数据定义区*/
#define uchar	unsigned char
#define uint	unsigned int
uchar		flag, a, i;
uchar code	table[]		= "I get ";
unsigned char	receive[20]	= { 0 }; /* 接收缓存 长度20 */

/*程序定义区*/
void init()
{
	TMOD	= 0x20;
	TH1	= 0xfd;
	TL1	= 0xfd;
	TR1	= 1;
	SCON	= 0x50;
	EA	= 1;
	ES	= 1;
}


void main( void )
{
	init();
	while ( 1 )
	{
		if ( flag == 1 )
		{
			ES = 0;
			for ( i = 0; i < 6; i++ ) /* 注意:若把i<6改成i<7,现象出错; */
			{
				SBUF = table[i];
				while ( !TI )
					;
				TI = 0;
			}
			i = 0;
			do /* 注意:若把i<6改成i<7,现象出错; */
			{
				SBUF = receive[i];
				while ( !TI )
					;
				TI = 0;
				i++;
			}
			while ( receive[i] != 0 );
			ES	= 1;
			flag	= 0;
			for ( i = 0; i < 19; i++ )//清空接受缓存
			{
				receive[i] = 0x00;
			}
		}
	}
}


void ser() interrupt 4
{
	static unsigned char count;
	RI = 0;
	if ( SBUF == 0x2e )//判断数据头
	{
		count = 1;
	}else if ( SBUF == 0x2d )//判断数据尾
	{
		count	= 0;
		flag	= 1;
		ES	= 0;
	}else  {
		receive[count - 1] = SBUF;//接收数据到数组receive
		count++;
	}
}


Proteus串口仿真7.0