generated from hulk/gd32e23x_template
237 lines
7.2 KiB
C
237 lines
7.2 KiB
C
//
|
|
// Created by yelv1 on 24-12-31.
|
|
//
|
|
|
|
#include "rs485_protocol.h"
|
|
|
|
extern uint8_t g_temperature_uint8[2];
|
|
static int buffer_index_r = 0;
|
|
static int buffer_index_w = 0;
|
|
static int buffer_length = 0;
|
|
static char cmd_buffer[BUF_SIZE][MAX_CMD_SIZE];
|
|
static int serial_count = 0;
|
|
static uint8_t serial_char = 0x00;
|
|
static packet_state_t packet_state = PS_NULL;
|
|
static char *strchr_pointer = NULL;
|
|
|
|
bool code_seen(char code) {
|
|
strchr_pointer = strchr(cmd_buffer[buffer_index_r], code);
|
|
return (strchr_pointer != NULL); //Return True if a character was found
|
|
}
|
|
|
|
float code_value(void)
|
|
{
|
|
return (strtod(&cmd_buffer[buffer_index_r][strchr_pointer - cmd_buffer[buffer_index_r] + 1], NULL));
|
|
}
|
|
|
|
// void process_command(uint8_t *cmd, size_t length) {
|
|
// char combined_str[3];
|
|
// validation_result_t validate = VALIDATION_SUCCESS;
|
|
//
|
|
// validate = (validate_package_header(cmd) |
|
|
// validate_package_type(cmd) |
|
|
// validate_data_length(cmd) |
|
|
// validate_package_crc(cmd, length));
|
|
//
|
|
// switch (validate) {
|
|
// case VALIDATION_SUCCESS:
|
|
// // printf("%d", length);
|
|
// sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
|
// if (strcmp(combined_str, "M1") == 0) {
|
|
// ultrasonic_distance_report();
|
|
// } else if (strcmp(combined_str, "M2") == 0) {
|
|
// gd60914_tempture_report();
|
|
// } else if (strcmp(combined_str, "M3") == 0)
|
|
// {
|
|
// printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
|
|
// fwdgt_reset_mcu();
|
|
// } else {
|
|
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
|
// return;
|
|
// }
|
|
// break;
|
|
// case VALIDATION_CRC_ERROR:
|
|
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
|
|
// break;
|
|
// case VALIDATION_HEADER_ERROR:
|
|
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
|
|
// break;
|
|
// case VALIDATION_TYPE_ERROR:
|
|
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
|
// break;
|
|
// case VALIDATION_LENGTH_ERROR:
|
|
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
|
|
// break;
|
|
// default:
|
|
// break;
|
|
// }
|
|
// }
|
|
|
|
uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
|
|
uint8_t crc = 0;
|
|
|
|
for (uint8_t i = 1; i < data_length - 1; i++) {
|
|
crc += data[i];
|
|
}
|
|
|
|
return (uint8_t) (crc & 0xFF);
|
|
}
|
|
|
|
validation_result_t validate_package_crc(uint8_t *data, uint8_t data_length) {
|
|
if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1) {
|
|
return VALIDATION_SUCCESS;
|
|
} else {
|
|
return VALIDATION_CRC_ERROR;
|
|
}
|
|
}
|
|
|
|
validation_result_t validate_package_header(uint8_t *data) {
|
|
if (data[0] == PROTOCOL_PACKAGE_HEADER) {
|
|
return VALIDATION_SUCCESS;
|
|
} else {
|
|
return VALIDATION_HEADER_ERROR;
|
|
}
|
|
}
|
|
|
|
validation_result_t validate_package_type(uint8_t *data) {
|
|
if (data[1] == PROTOCOL_BOARD_TYPE) {
|
|
return VALIDATION_SUCCESS;
|
|
} else {
|
|
return VALIDATION_TYPE_ERROR;
|
|
}
|
|
}
|
|
|
|
validation_result_t validate_data_length(uint8_t *data) {
|
|
if (data[2] == PROTOCOL_PACKAGE_LENGTH) {
|
|
return VALIDATION_SUCCESS;
|
|
} else {
|
|
return VALIDATION_LENGTH_ERROR;
|
|
}
|
|
}
|
|
|
|
void gd60914_tempture_report(void) {
|
|
static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
|
|
|
|
uint8_t combined_data[5];
|
|
memcpy(combined_data, package_header, 3);
|
|
memcpy(combined_data + 3, g_temperature_uint8, 2);
|
|
|
|
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
|
printf("%c%c", g_temperature_uint8[1], g_temperature_uint8[0]);
|
|
printf("%c", calculate_crc(combined_data, 6));
|
|
}
|
|
|
|
void ultrasonic_distance_report(void) {
|
|
static uint16_t distance_uint16 = 0;
|
|
static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
|
|
static uint8_t package_data[4] = {0};
|
|
|
|
distance_uint16 = ultrasonic_calc_distance();
|
|
|
|
package_data[0] = (distance_uint16 >> 8) & 0xFF;
|
|
package_data[1] = distance_uint16 & 0xFF;
|
|
|
|
uint8_t combined_data[7];
|
|
memcpy(combined_data, package_header, 3);
|
|
memcpy(combined_data + 3, package_data, 2);
|
|
|
|
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
|
printf("%c%c", package_data[0], package_data[1]);
|
|
printf("%c", calculate_crc(combined_data, 6));
|
|
}
|
|
|
|
|
|
void process_printer(void) {
|
|
if(buffer_length < (RX_BUFFER_SIZE - 1)) {
|
|
get_command();
|
|
}
|
|
if(buffer_length) {
|
|
process_command();
|
|
buffer_length = (buffer_length -1);
|
|
buffer_index_r = (buffer_index_r + 1) % BUF_SIZE;
|
|
}
|
|
}
|
|
|
|
void get_command (void) {
|
|
static uint8_t check_sum = 0;
|
|
static uint8_t packet_len ;
|
|
static uint8_t packet_type;
|
|
packet_state = PS_NULL;
|
|
serial_count = 0;
|
|
serial_char=0;
|
|
|
|
if(uart_available() > 0)
|
|
delay_ms(5);
|
|
while(uart_available() > 0 && buffer_length < BUF_SIZE) {
|
|
delay_us(100);
|
|
serial_char = uart_read();
|
|
|
|
switch (packet_state) {
|
|
case PS_NULL:
|
|
if (serial_char == PACKET_START_BYTE) {
|
|
serial_count = 0;
|
|
check_sum = 0;
|
|
packet_state = PS_TYPE;
|
|
} else
|
|
{
|
|
serial_count = 0;
|
|
}
|
|
break;
|
|
case PS_TYPE:
|
|
packet_type = serial_char;
|
|
check_sum += serial_char;
|
|
packet_state = PS_LEN;
|
|
break;
|
|
case PS_LEN:
|
|
packet_len = serial_char;
|
|
check_sum += serial_char;
|
|
packet_state = PS_PAYLOAD;
|
|
break;
|
|
case PS_PAYLOAD:
|
|
check_sum += serial_char;
|
|
cmd_buffer[buffer_index_w][serial_count++] = serial_char;
|
|
if (serial_count == packet_len)
|
|
packet_state = PS_CRC;
|
|
break;
|
|
case PS_CRC:
|
|
packet_state = PS_NULL;
|
|
if (!serial_count || check_sum != serial_char)
|
|
{
|
|
serial_count = 0;
|
|
return;
|
|
}
|
|
cmd_buffer[buffer_index_w][serial_count] = 0; // 终止字符串
|
|
buffer_index_w = (buffer_index_w + 1) % BUF_SIZE;
|
|
buffer_length += 1;
|
|
serial_count = 0;
|
|
break;
|
|
default:
|
|
packet_state = PS_NULL;
|
|
serial_count = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void process_command(void) {
|
|
if (code_seen('M')) {
|
|
switch ((int) code_value()) {
|
|
case 1:
|
|
// ultrasonic_distance_report();
|
|
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xF1);
|
|
break;
|
|
case 2:
|
|
// gd60914_tempture_report();
|
|
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xF2);
|
|
break;
|
|
case 3:
|
|
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
|
|
fwdgt_reset_mcu();
|
|
break;
|
|
default:
|
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
|
break;
|
|
}
|
|
}
|
|
|
|
} |