已经按照例程实现基本的功能,但是为什么串口转发的是前一个包

This commit is contained in:
yelvlab 2025-06-03 00:06:35 +08:00
parent c9e994f0c2
commit 8348652425
3 changed files with 30 additions and 9 deletions

View File

@ -9,6 +9,7 @@
#include "gd32e23x.h" #include "gd32e23x.h"
#include "systick.h" #include "systick.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include "ldc1612.h" #include "ldc1612.h"
#include "tmp112.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 rs485_config(void);
void process_command(uint8_t* cmd, size_t length); void process_command(uint8_t* cmd, size_t length);

View File

@ -39,6 +39,8 @@ OF SUCH DAMAGE.
#include "rs485.h" #include "rs485.h"
#include "led.h" #include "led.h"
extern uint8_t readBuffer[16];
/*! /*!
\brief this function handles NMI exception \brief this function handles NMI exception
\param[in] none \param[in] none
@ -119,22 +121,21 @@ void TIMER16_IRQHandler(void) {
void USART0_IRQHandler(void) { void USART0_IRQHandler(void) {
static uint8_t rx_index = 0; 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)) { if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
usart_interrupt_flag_clear(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 < sizeof(readBuffer) - 1) {
rx_buffer[rx_index++] = received_data; readBuffer[rx_index++] = usart_data_receive(USART0);
} }
} }
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) { if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
usart_interrupt_flag_clear(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; // 重置缓冲区索引 rx_index = 0; // 重置缓冲区索引
return; return;

View File

@ -8,6 +8,8 @@
bool g_statusSwitch = false; bool g_statusSwitch = false;
uint8_t readBuffer[16];
/*! /*!
\brief main function \brief main function
\param[in] none \param[in] none
@ -40,12 +42,23 @@ int main(void) {
/* Initialize watchdog */ /* Initialize watchdog */
watchdog_init(); watchdog_init();
uint8_t command[10];
uint8_t command_length = 0;
while (1) { while (1) {
delay_ms(10); // delay_ms(10);
fwdgt_counter_reload(); fwdgt_counter_reload();
if (g_statusSwitch) command_length = Command_GetCommand(command);
{eddy_current_value_report();} if (command_length != 0)
{
for (int i = 0; i < command_length; i++)
{
printf("%c", command[i]);
}
}
// if (g_statusSwitch)
// {eddy_current_value_report();}
} }
} }