1、打开生成的文件,在while函数下添加此函数,暂时忽略主函数中的udp_echoserver_init(); ,不影响连接测试
2、我们已经配置生成好了程序文件,把程序下载到开发板中,下面开始配置电脑端的IP,重点是网段号一定要一样!!!!!!!!
3、接下来看看我们与电脑的连接状态,windows+r,输入cmd
ping成功,说明电脑与开发板已经建立了连接,下面,开始发送数据
7、在配置发送数据前,这里我们需要添加一个文件,这是ST自带的库文件,也就是上图主函数调用的 udp_echoserver_init();(对ping不影响,但是网络数据发送需要用到)
`/**
@attention
Licensed under MCD-ST Liberty SW License Agreement V2, (the “License”);
You may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.st.com/software_license_agreement_liberty_v2
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* Includes ------------------------------------------------------------------/
#include “main.h”
#include “lwip/pbuf.h”
#include “lwip/udp.h”
#include “lwip/tcp.h”
//#include “ip_addr.h”
#include
#include
#include “udp_echoserver.h”
/
/ Private define ------------------------------------------------------------/
#define UDP_SERVER_PORT 8080 / define the UDP local connection port /
#define UDP_CLIENT_PORT 8080 / define the UDP remote connection port */
/* Private macro -------------------------------------------------------------/
/ Private variables ---------------------------------------------------------/
/ Private function prototypes -----------------------------------------------*/
void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const struct ip4_addr *addr, u16_t port);
/* Private functions ---------------------------------------------------------*/
/**
/* Create a new UDP control block */
upcb = udp_new();
if (upcb)
{
/* Bind the upcb to the UDP_PORT port /
/ Using IP_ADDR_ANY allow the upcb to be used by any local interface */
err = udp_bind(upcb, IP_ADDR_ANY, UDP_SERVER_PORT);
if(err == ERR_OK)
{
/* Set a receive callback for the upcb */
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
udp_recv(upcb, udp_echoserver_receive_callback, NULL);
}
else
{
udp_remove(upcb);
// printf("can not bind pcb");
}
}
else
{
// printf("can not create pcb");
}
}
/**
udp_connect(upcb, addr, UDP_CLIENT_PORT); //连接
// pq = pbuf_alloc(PBUF_TRANSPORT,2,PBUF_POOL);//allocate memory
// pbuf_take(pq,(char*)send_buf,2);//copy data to buf
pq=pbuf_alloc(PBUF_TRANSPORT,strlen((char*)receivebuf),PBUF_POOL); //申请内存
if(pq) // 如果成功,将接收到的数据寄存
{
pq->payload=(void*)receivebuf;
}
udp_send(upcb,pq);//send udp data 将接收到的数据发送出去,做一个循环试验
udp_disconnect(upcb);
pbuf_free(pq);
}
}
/************************ © COPYRIGHT STMicroelectronics *END OF FILE/
下面是头文件
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*/
#ifndef __ECHO_H__
#define __ECHO_H__
void udp_echoserver_init(void);
#endif /* __MINIMAL_ECHO_H */
8、此时把程序下载到开发板后,就可以实现我在电脑端通过网络助手使用UDP协议向开发板发送数据,开发板接收到数据活就会回传过来在上位机显示