This is a basic 51 microcontroller serial port receiver program for receiving six hexadecimal numbers and storing them in an array. The program then compares to the given six hexadecimal numbers, and if it matches, it sends an "O" message to the serial port, otherwise "E"
#include
#define BUFFER_SIZE 6
unsigned char buffer[BUFFER_SIZE];
unsigned char expected[] = {0x55, 0x00, 0x03, 0x11, 0x00, 0xec};
void main() {
unsigned char i;
// 初始化串口和时钟,参考上面的,不多写了
// ...
// 循环接收 6 个字节的数据
for (i = 0; i < BUFFER_SIZE; i++) {
while (!RI); // 等待接收到数据
buffer[i] = SBUF; // 读取接收到的数据
RI = 0; // 清除接收中断标志
}
// 比较接收到的数据与预期的数据
for (i = 0; i < BUFFER_SIZE; i++) {
if (buffer[i] != expected[i]) {
// 如果不匹配,向串口发送一个错误消息并退出程序
SBUF = 'E';
while (!TI); // 等待发送完成
TI = 0; // 清除发送中断标志
return;
}
}
// 如果匹配,向串口发送一个成功消息
SBUF = 'O';
while (!TI); // 等待发送完成
TI = 0; // 清除发送中断标志
}
Note that this is a basic example program. In practice, you need to modify and optimize it for your specific needs. For example, add a timeout mechanism to avoid infinite waiting for serial port data.