DI状态发生改变时,下位机主动上发DI状态,重复三次,直到上位机回复

This commit is contained in:
王绪洁 2025-02-25 14:32:45 +08:00
parent 22436f3bc0
commit 3dbaf6dc60
6 changed files with 126 additions and 83 deletions

View File

@ -23,7 +23,8 @@
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/* Includes ------------------------------------------------------------------*/
@ -34,21 +35,21 @@ extern "C" {
#include "stdio.h"
#include "tcpserverc.h"
#include <string.h>
/* USER CODE END Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
extern uint8_t tcp_echo_flags_control;
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;
#define DEST_IP_ADDR0 192
#define DEST_IP_ADDR1 168
@ -62,20 +63,20 @@ typedef struct
#define LOCAL_PORT 5001
/* USER CODE END ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
@ -168,7 +169,7 @@ void Error_Handler(void);
/* USER CODE BEGIN Private defines */
#define TRUE 0
#define FAIL -1
/* USER CODE END Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}

View File

@ -27,6 +27,9 @@
/* USER CODE BEGIN Includes */
#include "communication_protocol.h"
#include "tim.h"
#include "gpio.h"
#include "tcpserverc.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -54,18 +57,21 @@ osThreadId leds_taskHandle;
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
uint8_t di_state_last[DI_MAX] = {0};
uint8_t di_state_now[DI_MAX] = {0};
extern uint8_t tcp_echo_flags_control;
extern uint8_t send_data_flag_cmd;
/* USER CODE END FunctionPrototypes */
void start_tcp_task(void const * argument);
void start_gpio_di_do_task(void const * argument);
void start_leds_task(void const * argument);
void start_tcp_task(void const *argument);
void start_gpio_di_do_task(void const *argument);
void start_leds_task(void const *argument);
extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize);
/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
@ -81,11 +87,12 @@ void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackTyp
/* USER CODE END GET_IDLE_TASK_MEMORY */
/**
* @brief FreeRTOS initialization
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void) {
* @brief FreeRTOS initialization
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void)
{
/* USER CODE BEGIN Init */
/* USER CODE END Init */
@ -122,7 +129,6 @@ void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_start_tcp_task */
@ -132,7 +138,7 @@ void MX_FREERTOS_Init(void) {
* @retval None
*/
/* USER CODE END Header_start_tcp_task */
void start_tcp_task(void const * argument)
void start_tcp_task(void const *argument)
{
/* init code for LWIP */
MX_LWIP_Init();
@ -155,13 +161,42 @@ void start_tcp_task(void const * argument)
* @retval None
*/
/* USER CODE END Header_start_gpio_di_do_task */
void start_gpio_di_do_task(void const * argument)
void start_gpio_di_do_task(void const *argument)
{
/* USER CODE BEGIN start_gpio_di_do_task */
/* Infinite loop */
for (;;)
{
osDelay(1);
uint8_t di_ch = 0;
uint8_t tx_data_len = 7 + DI_MAX;
uint8_t tx_data[32] = {0};
tx_data[0] = FRAME_HEAD; // 帧头
tx_data[1] = COM_OK; // 状态
tx_data[2] = DEVICE_NUM; // 设备号
tx_data[3] = SEND_STATE_CMD; // 命令号
tx_data[4] = DI_MAX; // 数据长度
for (di_ch = 0; di_ch < DI_MAX; di_ch++)
{
di_state_now[di_ch] = gpio_di_test(di_ch);
if (di_state_last[di_ch] != di_state_now[di_ch])
{
di_state_last[di_ch] = di_state_now[di_ch];
send_data_flag_cmd = 1;
}
tx_data[5 + di_ch] = di_state_now[di_ch];
}
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);
send_data_flag_cmd++;
if (send_data_flag_cmd > 3) // 连续三次上位机没有回应,则停止发送数据包
{
send_data_flag_cmd = 0;
}
}
vTaskDelay(200);
}
/* USER CODE END start_gpio_di_do_task */
}
@ -173,7 +208,7 @@ void start_gpio_di_do_task(void const * argument)
* @retval None
*/
/* USER CODE END Header_start_leds_task */
void start_leds_task(void const * argument)
void start_leds_task(void const *argument)
{
/* USER CODE BEGIN start_leds_task */
/* Infinite loop */

View File

@ -63,12 +63,13 @@ void MX_FREERTOS_Init(void);
/* USER CODE BEGIN 0 */
uint8_t tcp_echo_flags_control = 0;
uint8_t send_data_flag_cmd = 0;
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
@ -118,22 +119,22 @@ int main(void)
}
/**
* @brief System Clock Configuration
* @retval None
*/
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
@ -148,9 +149,8 @@ void SystemClock_Config(void)
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
@ -167,19 +167,20 @@ void SystemClock_Config(void)
/* USER CODE END 4 */
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM1 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM1 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM1) {
if (htim->Instance == TIM1)
{
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
@ -188,9 +189,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
@ -202,14 +203,14 @@ void Error_Handler(void)
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */

View File

@ -3,11 +3,16 @@
#include "user_lib.h"
#define COM_ERROR_CODE_SIZE 7 // 通信错误返回帧长度
#define COM_AI_DATA_SIZE 11 // 模拟量数据返回帧长度
#define FRAME_HEAD 0xAA // 帧头
#define FRAME_TAIL 0x3C // 帧尾
#define COM_ERROR_CODE_SIZE 7 // 通信错误返回帧长度
#define COM_AI_DATA_SIZE 11 // 模拟量数据返回帧长度
#define FRAME_HEAD 0xAA // 帧头
#define FRAME_TAIL 0x3C // 帧尾
#define DEVICE_NUM 0x00 // 设备号
#define READ_ANALOG_CMD 0x00 // 读模拟量命令
#define WRITE_ANALOG_CMD 0x01 // 写模拟量命令
#define READ_DIGITAL_CMD 0x02 // 读数字量命令
#define WRITE_DIGITAL_CMD 0x03 // 写数字量命令
#define SEND_STATE_CMD 0x04 // 主动上发状态命令
typedef enum
{
COM_OK = 0,

View File

@ -10,8 +10,5 @@
#define TCP_PORT_CONTROL 5003
extern void tcp_echo_init(void);
extern void user_send_data_hart1(uint8_t *data, uint16_t len);
extern void user_send_data_hart2(uint8_t *data, uint16_t len);
extern void user_send_data_ble1(uint8_t *data, uint16_t len);
extern void user_send_data_ble2(uint8_t *data, uint16_t len);
extern void user_send_data_control(uint8_t *data, uint16_t len);
#endif

View File

@ -15,18 +15,15 @@
#include "communication_protocol.h"
#include "user_gpio.h"
struct tcp_pcb *server_pcb_control = NULL;
communication_di_t *user_communication_di = NULL;
communication_do_t *user_communication_do = NULL;
extern uint8_t tcp_echo_flags_control;
/*接收回调函数*/
static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
uint8_t tcp_rx_data[128] = {0}; // 接受数据缓存区
@ -37,7 +34,7 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
if (p != NULL)
{
/* 更新窗口*/
tcp_echo_flags_control = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
server_pcb_control = tpcb; // 直接赋值
memcpy(tcp_rx_data, (int *)p->payload, p->tot_len);
@ -59,7 +56,7 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
}
else
{
memcpy(communication_data.data, tcp_rx_data + 5, tcp_rx_data[4]);
memcpy(communication_data.data, tcp_rx_data + 5, tcp_rx_data[4]);
if (tcp_rx_data[3] == 0x02) // 读数字量指令
{
/*读操作,从寄存器读取数据,组包返回*/
@ -76,6 +73,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
{
// 返回命令号错误
@ -108,7 +109,6 @@ void tcp_echo_init(void)
{
struct tcp_pcb *server_control = NULL;
/* 创建控制块 */
server_control = tcp_new();
@ -122,4 +122,8 @@ void tcp_echo_init(void)
tcp_accept(server_control, tcpecho_accept_control); // 侦听到连接后回调用户编写的tcpecho_accept
}
void user_send_data_control(uint8_t *data, uint16_t len)
{
tcp_write(server_pcb_control, data, len, 1);
}