您的位置 首页 系统

如何用STM32F407的UDP发送特定数据

如何用STM32F407的UDP发送特定数据-探究WaveShare的F407的板子好几天了,这两天一直止步于网卡模块的udp发包,因为截包工具总是收不到我代码里面发出去的数据包,我的要求是只发特定数据,不管收到的数据包。

探求WaveShare的F407的板子好几天了,这两天一向停步于网卡模块的udp发包,由于截包东西总是收不到我代码里边发出去的数据包,我的要求是只发特定数据,不论收到的数据包。

记录下整个测验进程,以便今后翻阅

运用开发板自带的例程,udp_echo_server的比如,在此基础上修正,main函数是

int main(void)

{

/*!《 At this stage the microcontroller clock setTIng is already configured to

168 MHz, this is done through SystemInit() funcTIon which is called from

startup file (startup_stm32f4xx.s) before to branch to applicaTIon main.

To reconfigure the default setTIng of SystemInit() function, refer to

system_stm32f4xx.c file

*/

#ifdef SERIAL_DEBUG

DebugComPort_Init();

#endif

/*Initialize LCD and Leds */

LCD_LED_Init();

/* configure ethernet */

ETH_BSP_Config();

/* Initilaize the LwIP stack */

LwIP_Init();

/* UDP echoserver */

udp_echoserver_init();

/* Infinite loop */

while (1)

{

/* check if any packet received */

if (ETH_CheckFrameReceived())

{

/* process received ethernet packet */

LwIP_Pkt_Handle();

}

/* handle periodic timers for LwIP */

LwIP_Periodic_Handle(LocalTime);

}

}

我发现串口不能用,可能是其初始化方法有些问题,我做了些修正

将DebugComPort_Init函数改成下面两个函数

USART_Configuration();

USART_NVIC_Config();

串口1就可以正常运用了

void USART_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

RCC_AHB1PeriphClockCmd(Open_USART_TX_GPIO_CLK,ENABLE);

RCC_AHB1PeriphClockCmd(Open_USART_RX_GPIO_CLK,ENABLE);

#ifdef USART1_OPEN

RCC_APB2PeriphClockCmd(Open_USART_CLK,ENABLE);

#else

RCC_APB1PeriphClockCmd(Open_USART_CLK,ENABLE);

#endif

GPIO_PinAFConfig(Open_USART_TX_GPIO_PORT, Open_USART_TX_SOURCE, Open_USART_TX_AF);

GPIO_PinAFConfig(Open_USART_RX_GPIO_PORT, Open_USART_RX_SOURCE, Open_USART_RX_AF);

/*

* Open_USART_TX -》 PA9 , Open_USART_RX -PA10

*/

GPIO_InitStructure.GPIO_Pin = Open_USART_TX_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(Open_USART_TX_GPIO_PORT, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = Open_USART_RX_PIN;

GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(Open_USART_RX_GPIO_PORT, &GPIO_InitStructure);

/*

USARTx configured as follow:

– BaudRate = 115200 baud

– Word Length = 8 Bits

– One Stop Bit

– No parity

– Hardware flow control disabled (RTS and CTS signals)

– Receive and transmit

*/

USART_InitStructure.USART_BaudRate = 115200;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(Open_USART, &USART_InitStructure);

/* Enable the Open_USART Transmit interrupt: this interrupt is generated when the

Open_USART transmit data register is empty */

USART_ITConfig(Open_USART,USART_IT_RXNE,ENABLE);

USART_Cmd(Open_USART, ENABLE);

}

void USART_NVIC_Config(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the USARTx Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = Open_USART_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

然后将udp_echoserver_init();注释掉

在while(1)里边增加一下函数

udp_send_data(udp_data, sizeof(udp_data));

代码是

unsigned char udp_data[]= “hello world!”;

void udp_send_data(uint8_t* pData, uint16_t len)

{

struct udp_pcb *upcb;

struct pbuf* buff;

struct ip_addr ipaddr;

err_t err;

buff = pbuf_alloc(PBUF_TRANSPORT, 1024, PBUF_ROM);

buff-》payload = pData;

buff-》len = len;

buff-》tot_len = len;

upcb = udp_new();//树立一个新的UDP包

udp_bind(upcb, IP_ADDR_ANY, 7);

IP4_ADDR(&ipaddr, 192, 168, 1, 11); //牢记,此处的IP是PC的IP,由于运用PC的截包软件接纳

err = udp_connect(upcb, &ipaddr, 7);

if(err == ERR_OK)

{

err = udp_send(upcb, buff);

if(ERR_IS_FATAL(err))

printf(“udp_send.。.%d\r\n”,err);

}

udp_disconnect(upcb);

pbuf_free(buff);

udp_remove(upcb);

}

终究main函数是

int main(void)

{

USART_Configuration();

USART_NVIC_Config();

/*Initialize LCD and Leds */

LCD_LED_Init();

/* configure ethernet */

ETH_BSP_Config();

/* Initilaize the LwIP stack */

LwIP_Init();

/* UDP echoserver */

//udp_echoserver_init();

/* Infinite loop */

while (1)

{

udp_send_data(udp_data, sizeof(udp_data));

//Delay(100);

/* check if any packet received */

if (ETH_CheckFrameReceived())

{

/* process received ethernet packet */

LwIP_Pkt_Handle();

}

/* handle periodic timers for LwIP */

LwIP_Periodic_Handle(LocalTime);

}

}

留意

udp_send_data函数里边的ip,一开始我便是由于把此处的IP设置成开发板的IP了,后来就没想到是这出了问题,所以截包东西一向截不到数据,后来单步调试,发现udp_recv的回调函数udp_echoserver_receive_callback的结构体udp_pcb的upcb的remote_ip一向是PC端的IP才想起来是这个问题。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/348005.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部