我把程序贴一下吧,比如我第一次发了01 02 03 04 05 06,串口只接收到01,后面的数据就没有了
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar buf[6];
uchar recbuf[6];
void Init_Com(void) //初始化串口
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}
void delay(uint count) //延时子函数
{
uint i;
while(count)
{
i=100;
while(i>0)
i--;
count--;
}
}
void combyte(uchar ch) //发送一个字节
{
SBUF=ch;
while(TI==0);
TI=0;
}
void comstring(uchar *str, uint len) //发送一个字符串
{
uint k=0;
do
{
combyte(*(str+k));
k++;
}
while(k<len);
}
void recvbuf(uchar *rec, uint lenth) //接收串口数据
{
uint i;
for(i=0;i<lenth;i++)
{
while(RI==0);
rec[i]=SBUF;
RI=0;
combyte(rec[i]);
}
}
void main() //主函数
{
Init_Com(); //初始化串口
RI=0;
TI=0;
do
{
delay(30);
if(RI==1)
{
recvbuf(recbuf,6);
comstring(recbuf,6);
}
}
while(1);
}
|