From 51d15c1977ad8126a8e9d0ed59a2ac9c16d5c1a7 Mon Sep 17 00:00:00 2001 From: wangxujie Date: Tue, 25 Feb 2025 13:43:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E4=B8=8A=E5=8F=91=E4=B8=89?= =?UTF-8?q?=E6=AC=A1=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Inc/main.h | 43 ++++++++++---------- Core/Src/freertos.c | 17 +++++--- Core/Src/main.c | 1 + MDK-ARM/semi-finished_product_testing.uvoptx | 10 +++++ User/application/src/tcpserverc.c | 7 +++- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index c0924a2..6e752a4 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -37,30 +37,31 @@ extern "C" #include "tcpserverc.h" #include "leds.h" #include -/* USER CODE END Includes */ + /* USER CODE END Includes */ -/* Exported types ------------------------------------------------------------*/ -/* USER CODE BEGIN ET */ -extern uint8_t tcp_echo_flags_hart1; -extern uint8_t tcp_echo_flags_hart2; -extern uint8_t tcp_echo_flags_ble1; -extern uint8_t tcp_echo_flags_ble2; -extern uint8_t tcp_echo_flags_control; + /* Exported types ------------------------------------------------------------*/ + /* USER CODE BEGIN ET */ + extern uint8_t tcp_echo_flags_hart1; + extern uint8_t tcp_echo_flags_hart2; + extern uint8_t tcp_echo_flags_ble1; + extern uint8_t tcp_echo_flags_ble2; + extern uint8_t tcp_echo_flags_control; + extern uint8_t send_data_flag_cmd; #define ARRAY_LEN(arr) (sizeof(arr)) / (sizeof(arr[0])) -typedef struct -{ - uint16_t rx_num; - uint8_t rx_data[512]; - uint8_t rx_data_temp[512]; - uint8_t tx_data[512]; -} uart_t; + typedef struct + { + uint16_t rx_num; + uint8_t rx_data[512]; + uint8_t rx_data_temp[512]; + uint8_t tx_data[512]; + } uart_t; -extern uart_t lcd_uart4; -extern uart_t ble2_uart3; -extern uart_t ble1_uart6; -extern uart_t hart1_uart5; -extern uart_t hart2_uart2; -extern uart_t usb_uart1; + extern uart_t lcd_uart4; + extern uart_t ble2_uart3; + extern uart_t ble1_uart6; + extern uart_t hart1_uart5; + extern uart_t hart2_uart2; + extern uart_t usb_uart1; #define DEST_IP_ADDR0 192 #define DEST_IP_ADDR1 168 #define DEST_IP_ADDR2 1 diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 320bdd1..9f18324 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -64,6 +64,7 @@ osThreadId ec11_taskHandle; /* USER CODE BEGIN FunctionPrototypes */ extern float current_buff[2]; extern uint8_t tcp_echo_flags_control; +extern uint8_t send_data_flag_cmd; int direction = 0; int encode_num = 0; uint8_t di_state_last[DI_MAX] = {0}; @@ -264,7 +265,7 @@ void start_gpio_di_do_task(void const *argument) /* Infinite loop */ for (;;) { - uint8_t di_flag = 0; + uint8_t di_ch = 0; uint8_t tx_data_len = 7 + DI_MAX; uint8_t tx_data[32] = {0}; @@ -279,18 +280,22 @@ void start_gpio_di_do_task(void const *argument) if (di_state_last[di_ch] != di_state_now[di_ch]) { di_state_last[di_ch] = di_state_now[di_ch]; - di_flag = 1; + send_data_flag_cmd = 1; } - tx_data[5 + di_ch] = di_state_now[di_ch]; + tx_data[5 + di_ch] = di_state_now[di_ch]; } - if ((1 == di_flag) && (1 == tcp_echo_flags_control)) + if ((send_data_flag_cmd != 0) && (1 == tcp_echo_flags_control)) { tx_data[5 + DI_MAX] = xor_compute(tx_data + 1, tx_data_len - 3); // 异或校验 tx_data[6 + DI_MAX] = FRAME_TAIL; // 帧尾 user_send_data_control(tx_data, tx_data_len); - di_flag = 0; + send_data_flag_cmd++; + if (send_data_flag_cmd > 3) // 连续三次上位机没有回应,则停止发送数据包 + { + send_data_flag_cmd = 0; + } } - vTaskDelay(10); + vTaskDelay(100); } /* USER CODE END start_gpio_di_do_task */ } diff --git a/Core/Src/main.c b/Core/Src/main.c index 716132c..fbf6be8 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -78,6 +78,7 @@ uint8_t tcp_echo_flags_hart2 = 0; uint8_t tcp_echo_flags_ble1 = 0; uint8_t tcp_echo_flags_ble2 = 0; uint8_t tcp_echo_flags_control = 0; +uint8_t send_data_flag_cmd = 0; /* USER CODE END 0 */ /** diff --git a/MDK-ARM/semi-finished_product_testing.uvoptx b/MDK-ARM/semi-finished_product_testing.uvoptx index 9d3ee36..a18ada5 100644 --- a/MDK-ARM/semi-finished_product_testing.uvoptx +++ b/MDK-ARM/semi-finished_product_testing.uvoptx @@ -215,6 +215,16 @@ 1 di_state_last + + 13 + 1 + di_state_now + + + 14 + 1 + tcp_echo_flags_control + 0 diff --git a/User/application/src/tcpserverc.c b/User/application/src/tcpserverc.c index 22b5583..94f3435 100644 --- a/User/application/src/tcpserverc.c +++ b/User/application/src/tcpserverc.c @@ -31,6 +31,7 @@ extern uint8_t tcp_echo_flags_hart2; extern uint8_t tcp_echo_flags_ble1; extern uint8_t tcp_echo_flags_ble2; extern uint8_t tcp_echo_flags_control; +extern uint8_t send_data_flag_cmd; /*接收回调函数*/ static err_t tcpecho_recv_hart1(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) @@ -171,7 +172,7 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf * else if (tcp_rx_data[3] == WRITE_ANALOG_CMD) // 写模拟量指令 { /*写操作,将数据写入寄存器,组包返回*/ - + user_communication_ao = &communication_data.ao_data; communication_set_ao(user_communication_ao); tcp_write(tpcb, tcp_rx_data, rx_data_len, 1); @@ -192,6 +193,10 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf * user_write_gpio(user_communication_do); tcp_write(tpcb, tcp_rx_data, rx_data_len, 1); } + else if (tcp_rx_data[3] == SEND_STATE_CMD) + { + send_data_flag_cmd = 0; // 上位机返回数据,发送状态标志位清零 + } else { // 返回命令号错误