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.

158 lines
3.3 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"
/* 定义端口号 */
#define UDP_REMOTE_PORT 8881 /* 远端端口 */
#define UDP_LOCAL_PORT 8880 /* 本地端口 */
#define UDP_Send_LOCAL_PORT 9880 /* 本地端口 */
#define UDP_Printf_PORT 8881
#define UDP_Long_PORT 8882
#define UDP_UpdataPos_PORT 8883
unsigned char received_data[1000];
/* udp控制块 */
static struct udp_pcb *upcb;
static struct udp_pcb *upcb_send;
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);
//err_t t = udp_sendto(upcb_send, p, &serverIP, 8000);
pbuf_free(p);
} else
{
pbuf_free(p);
}
}
//char UDPprintf[100];
void udp_printf_CmdBack(char *pData)
{
struct pbuf *p;
/* 分配缓冲区空间 */
p = pbuf_alloc(PBUF_TRANSPORT, strlen(pData), PBUF_POOL);
if (p != NULL)
{
/* 填充缓冲区数据 */
pbuf_take(p, pData, strlen(pData));
/* 发送udp数据 */
udp_send(upcb, p);
//err_t t = udp_sendto(upcb, p, &serverIP, UDP_Printf_PORT);
/* 释放缓冲区空间 */
pbuf_free(p);
}
}
void GF_UDP_Send(uint8_t *pData, uint16_t Size, uint16_t UDP_port)
{
struct pbuf *p;
/* 分配缓冲区空间 */
p = pbuf_alloc(PBUF_TRANSPORT, Size, PBUF_POOL);
if (p != NULL)
{
/* 填充缓冲区数据 */
pbuf_take(p, pData, Size);
/* 发送udp数据 */
//udp_send(upcb, p);
err_t t = udp_sendto(upcb, p, &serverIP, UDP_port);
/* 释放缓冲区空间 */
pbuf_free(p);
}
}
void GF_UpdataPos_Send(uint8_t *pData, uint32_t Size)
{
GF_UDP_Send(pData, Size, UDP_UpdataPos_PORT);
}
/******************************************************************************
* : udp客户端
* :
* :
******************************************************************************/
void udp_client_init(void)
{
err_t err;
IP4_ADDR(&serverIP, 192, 168, 1, 102);
/* 创建udp控制块 */
upcb = udp_new();
if (upcb != NULL)
{
/* 配置本地端口 */
upcb->local_port = UDP_LOCAL_PORT;
/* 配置服务器IP和端口 */
err= udp_connect(upcb, &serverIP, UDP_REMOTE_PORT);
// err = udp_bind(upcb, IP_ADDR_ANY, UDP_LOCAL_PORT);
upcb->so_options |= SOF_BROADCAST;
if (err == ERR_OK)
{
/* 注册接收回调函数 */
udp_recv(upcb, udp_receive_callback, NULL);
/* 发送udp数据 */
//printf("udp client connected\r\n");
} else
{
udp_remove(upcb);
//printf("can not connect udp pcb\r\n");
}
}
// upcb_send = udp_new();
//
// if (upcb_send != NULL)
// {
// /* 配置本地端口 */
// upcb_send->local_port = UDP_Send_LOCAL_PORT;
//
// /* 配置服务器IP和端口 */
// upcb_send->so_options |= SOF_BROADCAST;
// err = udp_bind(upcb_send, IP_ADDR_ANY, UDP_Send_LOCAL_PORT);
// if (err == ERR_OK)
// {
//
// } else
// {
// udp_remove(upcb_send);
//
// }
// }
}
/******************************** END OF FILE ********************************/