You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
5.0 KiB
194 lines
5.0 KiB
/*
|
|
* bsp_UDP.c
|
|
*
|
|
* Created on: Aug 13, 2024
|
|
* Author: akeguo
|
|
*/
|
|
|
|
#include "stm32h7xx_hal.h"
|
|
#include "lwip.h"
|
|
#include "udp.h"
|
|
#include "string.h"
|
|
#include "bsp_UDP.h"
|
|
#include "bsp_DLT_Log.h"
|
|
#include <bsp_UpperComputer_Handler.h>
|
|
#include "BSP/bsp_GPIO.h"
|
|
/* 定义端口号 */
|
|
#define UDP_DLT_LOCAL_PORT 8000 /* 本地端口 */
|
|
#define UDP_CMD_LOCAL_PORT 8002 /* 本地端口 */
|
|
|
|
#define UDP_DLT_Send_LOCAL_PORT 7000 /* 本地端口 */
|
|
#define UDP_CMD_Send_LOCAL_PORT 7002 /* 本地端口 */
|
|
#define UDP_REMOTE_DLT_PORT 9000 /* 远端端口 */
|
|
#define UDP_REMOTE_CMD_PORT 9002 /* 远端端口 */
|
|
|
|
#define UDP_UpdataPos_PORT 8883
|
|
unsigned char received_data[1000];
|
|
|
|
/* udp控制块 */
|
|
static struct udp_pcb *upcb_Rx_DLT;
|
|
static struct udp_pcb *upcb_Rx_Cmd;
|
|
|
|
static struct udp_pcb *upcb_Tx_DLT;
|
|
static struct udp_pcb *upcb_Tx_Cmd;
|
|
|
|
ip_addr_t serverIP;
|
|
|
|
/******************************************************************************
|
|
* 描述 : 接收回调函数
|
|
* 参数 : -
|
|
* 返回 : 无
|
|
******************************************************************************/
|
|
static void udp_receive_callback(void *arg, struct udp_pcb *upcb,
|
|
struct pbuf *p, const ip_addr_t *addr, u16_t port) {
|
|
if (p != NULL) {
|
|
memcpy(received_data, p->payload, p->len);
|
|
|
|
if (upcb->local_port == UDP_DLT_LOCAL_PORT) {
|
|
DLT_DataReceiveEndCallback(received_data, p->len);
|
|
} else if (UDP_CMD_LOCAL_PORT) {
|
|
|
|
if (*received_data == 0x55 && *(received_data + 1) == 0x55
|
|
&& p->len >= 3) {
|
|
decode_command_and_feedback(received_data + 2, p->len - 2, 2,
|
|
NULL); //0代表UDP
|
|
} else if (*received_data == 0x66 && *(received_data + 1) == 0x66
|
|
&& p->len >= 3)
|
|
{
|
|
GF_BSP_GPIO_SetIO(received_data[3], received_data[2]);
|
|
|
|
} else{
|
|
udp_cmd_send_back(received_data, p->len);
|
|
}
|
|
}
|
|
|
|
pbuf_free(p);
|
|
} else {
|
|
//pbuf_free(p);
|
|
}
|
|
|
|
}
|
|
|
|
//char UDPprintf[100];
|
|
|
|
void udp_dlt_send_back(char *pData, uint16_t Size) {
|
|
struct pbuf *p;
|
|
|
|
/* 分配缓冲区空间 */
|
|
p = pbuf_alloc(PBUF_TRANSPORT, Size, PBUF_RAM);
|
|
if (p != NULL) {
|
|
/* 填充缓冲区数据 */
|
|
pbuf_take(p, pData, Size);
|
|
|
|
/* 发送udp数据 */
|
|
|
|
//udp_send(upcb_DLT, p);
|
|
err_t t = udp_sendto(upcb_Tx_DLT, p, &serverIP, UDP_REMOTE_DLT_PORT);
|
|
/* 释放缓冲区空间 */
|
|
pbuf_free(p);
|
|
}
|
|
}
|
|
void udp_cmd_send_back(char *pData, uint16_t Size) {
|
|
struct pbuf *p;
|
|
|
|
/* 分配缓冲区空间 */
|
|
p = pbuf_alloc(PBUF_TRANSPORT, Size, PBUF_RAM);
|
|
|
|
if (p != NULL) {
|
|
/* 填充缓冲区数据 */
|
|
pbuf_take(p, pData, Size);
|
|
|
|
/* 发送udp数据 */
|
|
err_t err = udp_send(upcb_Tx_Cmd, p);
|
|
if (err != ERR_OK) {
|
|
// 发送失败
|
|
printf("Failed to send UDP data: %d\n", err);
|
|
}
|
|
/* 释放缓冲区空间 */
|
|
pbuf_free(p);
|
|
}
|
|
}
|
|
|
|
/******************************************************************************
|
|
* 描述 : 创建udp客户端
|
|
* 参数 : 无
|
|
* 返回 : 无
|
|
******************************************************************************/
|
|
void udp_client_init(void) {
|
|
err_t err;
|
|
|
|
IP4_ADDR(&serverIP, 192, 168, 144, 198);
|
|
|
|
/* 创建udp控制块 */
|
|
upcb_Rx_DLT = udp_new();
|
|
|
|
if (upcb_Rx_DLT != NULL) {
|
|
/* 配置本地端口 */
|
|
//upcb_DLT->local_port = UDP_DLT_LOCAL_PORT;
|
|
/* 配置服务器IP和端口 */
|
|
//upcb_DLT->so_options |= SOF_BROADCAST;
|
|
//err = udp_connect(upcb_DLT, &serverIP, UDP_REMOTE_DLT_PORT);
|
|
err = udp_bind(upcb_Rx_DLT, IP_ADDR_ANY, UDP_DLT_LOCAL_PORT);
|
|
if (err == ERR_OK) {
|
|
/* 注册接收回调函数 */
|
|
/* 注册接收回调函数 */
|
|
udp_recv(upcb_Rx_DLT, udp_receive_callback, NULL);
|
|
} else {
|
|
udp_remove(upcb_Rx_DLT);
|
|
|
|
}
|
|
}
|
|
upcb_Rx_Cmd = udp_new();
|
|
|
|
if (upcb_Rx_Cmd != NULL) {
|
|
/* 配置本地端口 */
|
|
//upcb_Cmd->local_port = UDP_CMD_LOCAL_PORT;
|
|
/* 配置服务器IP和端口 */
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
//err = udp_connect(upcb_Cmd, &serverIP, UDP_REMOTE_CMD_PORT);
|
|
err = udp_bind(upcb_Rx_Cmd, IP_ADDR_ANY, UDP_CMD_LOCAL_PORT);
|
|
if (err == ERR_OK) {
|
|
/* 注册接收回调函数 */
|
|
udp_recv(upcb_Rx_Cmd, udp_receive_callback, NULL);
|
|
} else {
|
|
udp_remove(upcb_Rx_Cmd);
|
|
}
|
|
}
|
|
|
|
upcb_Tx_Cmd = udp_new();
|
|
|
|
if (upcb_Tx_Cmd != NULL) {
|
|
/* 配置本地端口 */
|
|
upcb_Tx_Cmd->local_port = UDP_CMD_Send_LOCAL_PORT;
|
|
|
|
/* 配置服务器IP和端口 */
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
err = udp_connect(upcb_Tx_Cmd, &serverIP, UDP_REMOTE_CMD_PORT);
|
|
|
|
if (err == ERR_OK) {
|
|
/* 注册接收回调函数 */
|
|
//udp_recv(upcb_Tx_Cmd, udp_receive_callback, NULL);
|
|
} else {
|
|
udp_remove(upcb_Tx_Cmd);
|
|
}
|
|
}
|
|
upcb_Tx_DLT = udp_new();
|
|
|
|
if (upcb_Tx_DLT != NULL) {
|
|
/* 配置本地端口 */
|
|
upcb_Tx_Cmd->local_port = UDP_DLT_Send_LOCAL_PORT;
|
|
/* 配置服务器IP和端口 */
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
err = udp_connect(upcb_Tx_DLT, &serverIP, UDP_REMOTE_DLT_PORT);
|
|
|
|
if (err == ERR_OK) {
|
|
/* 注册接收回调函数 */
|
|
//udp_recv(upcb_Tx_Cmd, udp_receive_callback, NULL);
|
|
} else {
|
|
udp_remove(upcb_Tx_DLT);
|
|
}
|
|
}
|
|
}
|
|
|
|
/******************************** END OF FILE ********************************/
|
|
|
|
|