我的建议:首先定义一个变量储存收到的数据,同时允许接收在控制完毕以后使能。在判断语句里面使用这个变量来判断。最好通讯协议里面能够有起始,结束,以及校验字节。避免干扰造成控制紊乱。#include <REG52.H>#include <stdio.h>sbit K1 = P0^0;sbit K2 = P0^1;sbit K3 = P0^2;sbit K4 = P0^3;sbit K5 = P0^4;sbit K6 = P0^5;sbit K7 = P0^6;sbit K8 = P0^7;定义command为字节变量。。。。。。void main (void) {//TH1 = 0xFD; //baud*2 /* 波特率 19200、数据位8、停止位1。效验位无 (11.0592) TMOD=0X20; // 定时器工作模式2SCON=0X50; //允许串行接受状态,串口工作模式1PCON=0X80; //波特率加倍TH1 = 0xF3; // //baud*2 /* 波特率4800、数据位8、停止位1。效验位无 (12M)TL1 = 0xF3; TR1 = 1; ES = 1; //开串口中断 EA = 1; // 开总中断 while(1){ if (RI) //RI接受中断标志 { Command=SBUF; EA=0; ES=0; RI=0; //清除RI接受中断标志 } if (command == 0x01) K1 = 0; if (command == 0x02) K1 = 1; if (command == 0x03) K2 = 0; if (command == 0x04) K2 = 1; if (command == 0x05) K3 = 0; if (command == 0x06) K3 = 1; if (command == 0x07) K4 = 0; if (command == 0x08) K4 = 1; if (command == 0x09) K5 = 0; if (command == 0x10) K5 = 1; if (command == 0xAB) K6 = 0; if (command == 0xAC) K6 = 1; if (command == 0xAD) K7 = 0; if (command == 0xAE) K7 = 1; if (command == 0xAF) K8 = 0; if (command == 0xBA) K8 = 1; if (command == 0xAA) { K1 = 0; K2 = 0; K3 = 0; K4 = 0; K5 = 0; K6 = 0; K7 = 0; K8 = 0; } if (command == 0xBB) { K1 = 1; K2 = 1; K3 = 1; K4 = 1; K5 = 1; K6 = 1; K7 = 1; K8 = 1; } RI=0; //清除RI接受中断标志 ES=1; EA=1;}}