完成串行数据通信协议

This commit is contained in:
yelvlab 2024-12-24 09:32:33 +08:00
parent dc8bff918d
commit 5838f18742
3 changed files with 57 additions and 24 deletions

View File

@ -26,9 +26,16 @@
#define RX_BUFFER_SIZE 64
typedef enum {
VALIDATION_SUCCESS = 0,
VALIDATION_CRC_ERROR = 1
} validation_result_t;
void rs485_config(void);
void process_command(uint8_t *cmd, size_t length);
uint8_t calculate_crc(uint8_t data[], size_t data_length);
validation_result_t validate_package_header(uint8_t *data);
validation_result_t validate_package_type(uint8_t *data);
void eddy_current_value_report(void);
void tempture_value_report(void);

View File

@ -132,9 +132,18 @@ void USART0_IRQHandler(void) {
// 检查是否接收到换行符,表示指令结束
if (received_data == '\n') {
// rx_buffer[rx_index] = '\0'; // 添加字符串结束符
process_command(rx_buffer, rx_index); // 处理指令
rx_index = 0; // 重置缓冲区索引
if (calculate_crc(rx_buffer, rx_index - 2) == rx_buffer[rx_index - 2]) {
// printf("CRC check success\r\n");
// rx_buffer[rx_index] = '\0'; // 添加字符串结束符
process_command(rx_buffer, rx_index); // 处理指令
rx_index = 0; // 重置缓冲区索引
} else {
// printf("CRC check failed\r\n");
printf("%c%c%c%c\r\n", 0xB5, 0xF2, 0x00, 0xF2);
rx_index = 0; // 重置缓冲区索引
return;
}
}
}
}

View File

@ -7,7 +7,7 @@
extern uint32_t g_temperature_uint32;
extern uint32_t g_eddy_current_value_uint32;
uint8_t package_header[3] = {0xB5, 0x00, 0x04};
uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
uint8_t package_data[4] = {0};
void rs485_config(void) {
@ -39,29 +39,28 @@ void rs485_config(void) {
void process_command(uint8_t *cmd, size_t length) {
char combined_str[3];
printf("%c", calculate_crc(cmd, length - 2));
printf("%c", cmd[length - 2]);
if (calculate_crc(cmd, length - 2) == cmd[length - 2]) {
printf("CRC check success\r\n");
} else {
printf("CRC check failed\r\n");
return;
}
if (cmd[0] == 0xD5) {
if (cmd[1] == 0x03) {
if (cmd[2] == 0x02) {
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
if (strcmp(combined_str, "M1") == 0) {
if (cmd[0] == 0xD5 && cmd[1] == 0x03) {
if (cmd[2] == 0x02) {
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
if (strcmp(combined_str, "M1") == 0) {
eddy_current_value_report();
} else if (strcmp(combined_str, "M2") == 0) {
tempture_value_report();
eddy_current_value_report();
} else if (strcmp(combined_str, "M2") == 0) {
tempture_value_report();
} else {
printf("%c%c%c%c%c%c%c\r\n", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
return;
}
} else {
printf("******\r\n");
printf("%c%c%c%c\r\n", 0xB5, 0xF1, 0x00, 0xF1);
}
} else {
printf("%c%c%c%c\r\n", 0xB5, 0xF5, 0x00, 0xF5);
}
} else {
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
}
}
@ -75,8 +74,26 @@ uint8_t calculate_crc(uint8_t data[], size_t data_length) {
return (uint8_t)(crc & 0xFF);
}
validation_result_t validate_package_header(uint8_t *data) {
if (data[0] == 0xD5) {
return VALIDATION_SUCCESS;
} else {
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
return VALIDATION_CRC_ERROR;
}
}
validation_result_t validate_package_type(uint8_t *data) {
if (data[1] == 0x03) {
return VALIDATION_SUCCESS;
} else {
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
return VALIDATION_CRC_ERROR;
}
}
void eddy_current_value_report(void) {
package_header[1] = 0xF0; //eddy current
// package_header[1] = 0xF0; //eddy current
package_data[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF;
package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF;
@ -94,7 +111,7 @@ void eddy_current_value_report(void) {
}
void tempture_value_report(void) {
package_header[1] = 0xF1; //temperature
// package_header[1] = 0xF1; //temperature
package_data[0] = (g_temperature_uint32 >> 24) & 0xFF;
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;