diff --git a/inc/rs485.h b/inc/rs485.h index 3200d5e..a6366f3 100644 --- a/inc/rs485.h +++ b/inc/rs485.h @@ -9,6 +9,7 @@ #include "gd32e23x.h" #include "systick.h" #include +#include #include #include "ldc1612.h" #include "tmp112.h" @@ -36,6 +37,12 @@ typedef enum /******************************************************************************/ +uint8_t Command_Write(uint8_t *data, uint8_t length); + +uint8_t Command_GetCommand(uint8_t *command); + +/******************************************************************************/ + void rs485_config(void); void process_command(uint8_t* cmd, size_t length); diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 6893a93..24ab88e 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -39,6 +39,8 @@ OF SUCH DAMAGE. #include "rs485.h" #include "led.h" +extern uint8_t readBuffer[16]; + /*! \brief this function handles NMI exception \param[in] none @@ -119,22 +121,21 @@ void TIMER16_IRQHandler(void) { void USART0_IRQHandler(void) { 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); + // usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); + // uint8_t received_data = (uint8_t) usart_data_receive(USART0); // 将接收到的数据存储到缓冲区 - if (rx_index < RX_BUFFER_SIZE - 1) { - rx_buffer[rx_index++] = received_data; + if (rx_index < sizeof(readBuffer) - 1) { + readBuffer[rx_index++] = usart_data_receive(USART0); } } if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) { usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE); - process_command(rx_buffer, rx_index); // 处理指令 + Command_Write(readBuffer, rx_index);// 处理指令 rx_index = 0; // 重置缓冲区索引 return; diff --git a/src/main.c b/src/main.c index 6c6b0d8..a342d50 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,8 @@ bool g_statusSwitch = false; +uint8_t readBuffer[16]; + /*! \brief main function \param[in] none @@ -40,12 +42,23 @@ int main(void) { /* Initialize watchdog */ watchdog_init(); + uint8_t command[10]; + uint8_t command_length = 0; + while (1) { - delay_ms(10); + // delay_ms(10); fwdgt_counter_reload(); - if (g_statusSwitch) - {eddy_current_value_report();} + command_length = Command_GetCommand(command); + if (command_length != 0) + { + for (int i = 0; i < command_length; i++) + { + printf("%c", command[i]); + } + } + // if (g_statusSwitch) + // {eddy_current_value_report();} } }