generated from hulk/gd32e23x_template
	完成收到包的分解与指令解析,并输出正确数据,待加入CRC验证机制。
This commit is contained in:
		| @@ -27,8 +27,8 @@ | ||||
| #define RX_BUFFER_SIZE 64 | ||||
|  | ||||
| void rs485_config(void); | ||||
| void process_command(char *cmd); | ||||
| uint8_t calculate_crc(uint8_t package_header[3], uint8_t package_data[], size_t data_length); | ||||
| void process_command(uint8_t *cmd, size_t length); | ||||
| uint8_t calculate_crc(uint8_t data[], size_t data_length); | ||||
| void eddy_current_value_report(void); | ||||
| void tempture_value_report(void); | ||||
|  | ||||
|   | ||||
| @@ -39,19 +39,15 @@ OF SUCH DAMAGE. | ||||
| #include "rs485.h" | ||||
| #include "led.h" | ||||
|  | ||||
| char rx_buffer[RX_BUFFER_SIZE]; | ||||
| uint8_t rx_index = 0; | ||||
|  | ||||
| /*! | ||||
|     \brief      this function handles NMI exception | ||||
|     \param[in]  none | ||||
|     \param[out] none | ||||
|     \retval     none | ||||
| */ | ||||
| void NMI_Handler(void) | ||||
| { | ||||
| void NMI_Handler(void) { | ||||
|     /* if NMI exception occurs, go to infinite loop */ | ||||
|     while(1) { | ||||
|     while (1) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -61,10 +57,9 @@ void NMI_Handler(void) | ||||
|     \param[out] none | ||||
|     \retval     none | ||||
| */ | ||||
| void HardFault_Handler(void) | ||||
| { | ||||
| void HardFault_Handler(void) { | ||||
|     /* if Hard Fault exception occurs, go to infinite loop */ | ||||
|     while(1) { | ||||
|     while (1) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -74,10 +69,9 @@ void HardFault_Handler(void) | ||||
|     \param[out] none | ||||
|     \retval     none | ||||
| */ | ||||
| void SVC_Handler(void) | ||||
| { | ||||
| void SVC_Handler(void) { | ||||
|     /* if SVC exception occurs, go to infinite loop */ | ||||
|     while(1) { | ||||
|     while (1) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -87,10 +81,9 @@ void SVC_Handler(void) | ||||
|     \param[out] none | ||||
|     \retval     none | ||||
| */ | ||||
| void PendSV_Handler(void) | ||||
| { | ||||
| void PendSV_Handler(void) { | ||||
|     /* if PendSV exception occurs, go to infinite loop */ | ||||
|     while(1) { | ||||
|     while (1) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -100,8 +93,7 @@ void PendSV_Handler(void) | ||||
|     \param[out] none | ||||
|     \retval     none | ||||
| */ | ||||
| void SysTick_Handler(void) | ||||
| { | ||||
| void SysTick_Handler(void) { | ||||
| } | ||||
|  | ||||
| /** | ||||
| @@ -111,12 +103,10 @@ void SysTick_Handler(void) | ||||
|   * @retval None | ||||
|   */ | ||||
| void TIMER16_IRQHandler(void) { | ||||
|     if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) | ||||
|     { | ||||
|     if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) { | ||||
|         timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP); | ||||
|         static uint8_t led_status = 0; | ||||
|         if (led_status) | ||||
|         { | ||||
|         if (led_status) { | ||||
|             gpio_bit_write(LED_PORT, LED_PIN, RESET); | ||||
|             timer_autoreload_value_config(LED_TIMER, 19200); | ||||
|         } else { | ||||
| @@ -128,21 +118,23 @@ void TIMER16_IRQHandler(void) { | ||||
| } | ||||
|  | ||||
| void USART0_IRQHandler(void) { | ||||
|     if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) | ||||
|     { | ||||
|     static uint8_t rx_index = 0; | ||||
|     static uint8_t rx_buffer[RX_BUFFER_SIZE]; | ||||
|  | ||||
|     if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { | ||||
|         usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); | ||||
|         uint8_t received_data = (uint8_t)usart_data_receive(USART0); | ||||
|         uint8_t received_data = (uint8_t) usart_data_receive(USART0); | ||||
|  | ||||
|         // 将接收到的数据存储到缓冲区 | ||||
|         if(rx_index < RX_BUFFER_SIZE - 1) { | ||||
|         if (rx_index < RX_BUFFER_SIZE - 1) { | ||||
|             rx_buffer[rx_index++] = received_data; | ||||
|         } | ||||
|  | ||||
|         // 检查是否接收到换行符,表示指令结束 | ||||
|         if(received_data == '\n') { | ||||
|             rx_buffer[rx_index] = '\0'; // 添加字符串结束符 | ||||
|             process_command(rx_buffer); // 处理指令 | ||||
|         if (received_data == '\n') { | ||||
|             // rx_buffer[rx_index] = '\0'; // 添加字符串结束符 | ||||
|             process_command(rx_buffer, rx_index); // 处理指令 | ||||
|             rx_index = 0; // 重置缓冲区索引 | ||||
|         } | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
							
								
								
									
										62
									
								
								src/rs485.c
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								src/rs485.c
									
									
									
									
									
								
							| @@ -37,41 +37,37 @@ void rs485_config(void) { | ||||
|     usart_interrupt_enable(RS485_PHY, USART_INT_RBNE); | ||||
| } | ||||
|  | ||||
| void process_command(char *cmd) { | ||||
| void process_command(uint8_t *cmd, size_t length) { | ||||
|     char combined_str[3]; | ||||
|  | ||||
|     // printf("\n"); | ||||
|     // printf("%s\n", combined_str); | ||||
|     // } | ||||
|  | ||||
|     // printf("length: %d\r\n", length); | ||||
|  | ||||
|  | ||||
|     printf("%c", calculate_crc(cmd, length - 2)); | ||||
|     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) { | ||||
|  | ||||
|  | ||||
|  | ||||
|     if (strncmp(cmd, "M12", 3) == 0) { | ||||
|         // printf("M1 -=-=- OK!\r\n"); | ||||
|         eddy_current_value_report(); | ||||
|     } else if (strncmp(cmd, "M2", 2) == 0) { | ||||
|         // printf("M2 -=-=- OK!\r\n"); | ||||
|         tempture_value_report(); | ||||
|         // } else if (strncmp(cmd, "M3", 2) == 0) { | ||||
|         //     char *param_str = cmd + 2; // Skip "M3" | ||||
|         //     int param = atoi(param_str + 1); // Skip "S" and convert to integer | ||||
|         //     if (param >= 0 && param <= 100) { | ||||
|         //         printf("M3 with parameter %d -=-=- OK!\r\n", param); | ||||
|         //     } else { | ||||
|         //         printf("Invalid parameter for M3 command!\r\n"); | ||||
|         //     } | ||||
|     } else { | ||||
|         printf("Invalid Command!\r\n"); | ||||
|                 eddy_current_value_report(); | ||||
|             } else if (strcmp(combined_str, "M2") == 0) { | ||||
|                 tempture_value_report(); | ||||
|             } else { | ||||
|                 printf("******\r\n"); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| uint8_t calculate_crc(uint8_t package_header[3], uint8_t package_data[], size_t data_length) { | ||||
| uint8_t calculate_crc(uint8_t data[], size_t data_length) { | ||||
|     uint8_t crc = 0; | ||||
|  | ||||
|     /* Calculate CRC for header */ | ||||
|     for (int i = 1; i < 3; i++) { | ||||
|         crc += package_header[i]; | ||||
|     } | ||||
|  | ||||
|     for (size_t i = 0; i < data_length; i++) { | ||||
|         crc += package_data[i]; | ||||
|     for (size_t i = 1; i < data_length; i++) { | ||||
|         crc += data[i]; | ||||
|     } | ||||
|  | ||||
|     return (uint8_t)(crc & 0xFF); | ||||
| @@ -85,10 +81,14 @@ void eddy_current_value_report(void) { | ||||
|     package_data[2] = (g_eddy_current_value_uint32 >> 8) & 0xFF; | ||||
|     package_data[3] = g_eddy_current_value_uint32 & 0xFF; | ||||
|  | ||||
|     uint8_t combined_data[7]; | ||||
|     memcpy(combined_data, package_header, 3); | ||||
|     memcpy(combined_data + 3, package_data, 4); | ||||
|  | ||||
|     // printf("EddyCurrent: %x\r\n", g_eddy_current_value_uint32); | ||||
|     printf("%c%c%c", package_header[0], package_header[1], package_header[2]); | ||||
|     printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]); | ||||
|     printf("%c\r\n", calculate_crc(package_header, package_data, 4)); | ||||
|     printf("%c\r\n", calculate_crc(combined_data, 7)); | ||||
| } | ||||
|  | ||||
| void tempture_value_report(void) { | ||||
| @@ -99,8 +99,12 @@ void tempture_value_report(void) { | ||||
|     package_data[2] = (g_temperature_uint32 >> 8) & 0xFF; | ||||
|     package_data[3] = g_temperature_uint32 & 0xFF; | ||||
|  | ||||
|     uint8_t combined_data[7]; | ||||
|     memcpy(combined_data, package_header, 3); | ||||
|     memcpy(combined_data + 3, package_data, 4); | ||||
|  | ||||
|     // printf("Temperature: %x\r\n", g_temperature_uint32); | ||||
|     printf("%c%c%c", package_header[0], package_header[1], package_header[2]); | ||||
|     printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]); | ||||
|     printf("%c\r\n", calculate_crc(package_header, package_data, 4)); | ||||
|     printf("%c\r\n", calculate_crc(combined_data, 7)); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user