//需要定义一个串口接收缓冲区:
char buf[SIZE];
char *read = buf;
char *write = buf;
int cnt = 0;
//中断处理程序, :
void GetDataFromSerial(void)
{
if (cnt < SIZE)//事实上,应该是cnt < SIZE - num, 因为响应速度问题,需要提前禁止,num根据实际情况自定义大小
{
if (write == buf + SIZE)
{
write == buf;
}
//DI 禁止中断
*write++ = DATA;//假设DATA是从端口读的数据
cnt++;
//EI 恢复中断
}
else
{
//需要在这里禁止再接收数据
}
}
char GetDataFromSerialBuf()
{
char data;
if (cnt > 0)
{
if (read == buf + SIZE)
{
read = buf;
}
//DI 禁止中断
data = *read++;
cnt--;
//EI 恢复中断
}
else
{
while(cnt == 0);//等待数据
}
}
//主控程序中这么写
while (1)
{
data = GetDataFromSerialBuf();
switch(data)
{
case 'a':
function1();
break;
case 'b':
...
break;
case 'z':
...
break;
default:
break;
}
}