stm32+lwip(三):TCP测试
我是卓波,很高兴你来看我的博客。
系列文章:
stm32+lwip(一):使用STM32CubeMX生成项目
ST官方有lwip的例程,下载地址如下:
本文例子参考ST官方给出的例程。
一、准备
ST例程文档关于lwip的介绍如下:
由此可以看到LWIP有三种API,在本文中,使用Raw API。
本文用到的TCP Raw API如下:
二、tcp client
/**
*****************************************************************************
* @file tcp_client.c
* @author Zorb
* @version V1.0.0
* @date 2018-09-04
* @brief tcp客户端的实现
*****************************************************************************
* @history
*
* 1. Date:2018-09-04
* Author:Zorb
* Modification:建立文件
*
*****************************************************************************
*/ #include "stm32f4xx_hal.h"
#include "lwip.h"
#include "tcp.h"
#include "string.h" /* 定义端口号 */
#define TCP_REMOTE_PORT 8881 /* 远端端口 */
#define TCP_LOCAL_PORT 8880 /* 本地端口 */ /******************************************************************************
* 描述 : 数据接收回调函数
* 参数 : -
* 返回 : -
******************************************************************************/
static err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb,
struct pbuf *p, err_t err)
{
uint32_t i; /* 数据回传 */
//tcp_write(tpcb, p->payload, p->len, 1); if (p != NULL)
{
struct pbuf *ptmp = p; /* 打印接收到的数据 */
printf("get msg from %d:%d:%d:%d port:%d:\r\n",
*((uint8_t *)&tpcb->remote_ip.addr),
*((uint8_t *)&tpcb->remote_ip.addr + ),
*((uint8_t *)&tpcb->remote_ip.addr + ),
*((uint8_t *)&tpcb->remote_ip.addr + ),
tpcb->remote_port); while(ptmp != NULL)
{
for (i = ; i < p->len; i++)
{
printf("%c", *((char *)p->payload + i));
} ptmp = p->next;
} printf("\r\n"); tcp_recved(tpcb, p->tot_len); /* 释放缓冲区数据 */
pbuf_free(p);
}
else if (err == ERR_OK)
{
printf("tcp client closed\r\n"); tcp_recved(tpcb, p->tot_len); return tcp_close(tpcb);
} return ERR_OK;
} /******************************************************************************
* 描述 : 连接服务器回调函数
* 参数 : -
* 返回 : -
******************************************************************************/
static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
printf("tcp client connected\r\n"); tcp_write(tpcb, "tcp client connected", strlen("tcp client connected"), ); /* 注册接收回调函数 */
tcp_recv(tpcb, tcp_client_recv); return ERR_OK;
} /******************************************************************************
* 描述 : 创建tcp客户端
* 参数 : 无
* 返回 : 无
******************************************************************************/
void tcp_client_init(void)
{
struct tcp_pcb *tpcb;
ip_addr_t serverIp; /* 服务器IP */
IP4_ADDR(&serverIp, , , , ); /* 创建tcp控制块 */
tpcb = tcp_new(); if (tpcb != NULL)
{
err_t err; /* 绑定本地端号和IP地址 */
err = tcp_bind(tpcb, IP_ADDR_ANY, TCP_LOCAL_PORT); if (err == ERR_OK)
{
/* 连接服务器 */
tcp_connect(tpcb, &serverIp, TCP_REMOTE_PORT, tcp_client_connected);
}
else
{
memp_free(MEMP_TCP_PCB, tpcb); printf("can not bind pcb\r\n");
}
}
} /******************************** END OF FILE ********************************/
本例用到的上位机IP为192.168.2.194,开放端口为8881
STM32的IP为192.168.2.8,开放端口为8880
先将网络调试助手的TCP Server打开,然后给STM32上电。
网络调试助手将会收到如下信息:
然后点击网络调试助手的发送,STM32调试串口输出以下信息:
get msg from ::: port::
hello zorb
三、tcp server
/**
*****************************************************************************
* @file tcp_server.c
* @author Zorb
* @version V1.0.0
* @date 2018-09-04
* @brief tcp服务端的实现
*****************************************************************************
* @history
*
* 1. Date:2018-09-04
* Author:Zorb
* Modification:建立文件
*
*****************************************************************************
*/ #include "stm32f4xx_hal.h"
#include "lwip.h"
#include "tcp.h"
#include "string.h" /* 定义端口号 */
#define TCP_REMOTE_PORT 8881 /* 远端端口 */
#define TCP_LOCAL_PORT 8880 /* 本地端口 */ /******************************************************************************
* 描述 : 接收回调函数
* 参数 : -
* 返回 : -
******************************************************************************/
static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb,
struct pbuf *p, err_t err)
{
uint32_t i; /* 数据回传 */
//tcp_write(tpcb, p->payload, p->len, 1); if (p != NULL)
{
struct pbuf *ptmp = p; /* 打印接收到的数据 */
printf("get msg from %d:%d:%d:%d port:%d:\r\n",
*((uint8_t *)&tpcb->remote_ip.addr),
*((uint8_t *)&tpcb->remote_ip.addr + ),
*((uint8_t *)&tpcb->remote_ip.addr + ),
*((uint8_t *)&tpcb->remote_ip.addr + ),
tpcb->remote_port); while(ptmp != NULL)
{
for (i = ; i < p->len; i++)
{
printf("%c", *((char *)p->payload + i));
} ptmp = p->next;
} printf("\r\n"); tcp_recved(tpcb, p->tot_len); /* 释放缓冲区数据 */
pbuf_free(p);
}
else if (err == ERR_OK)
{
printf("tcp client closed\r\n"); tcp_recved(tpcb, p->tot_len); return tcp_close(tpcb);
} return ERR_OK;
} /******************************************************************************
* 描述 : 客户端接入回调函数
* 参数 : -
* 返回 : -
******************************************************************************/
static err_t tcp_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
printf("tcp client connected\r\n"); printf("ip %d:%d:%d:%d port:%d\r\n",
*((uint8_t *)&newpcb->remote_ip.addr),
*((uint8_t *)&newpcb->remote_ip.addr + ),
*((uint8_t *)&newpcb->remote_ip.addr + ),
*((uint8_t *)&newpcb->remote_ip.addr + ),
newpcb->remote_port); tcp_write(newpcb, "tcp client connected", strlen("tcp client connected"), ); /* 注册接收回调函数 */
tcp_recv(newpcb, tcp_server_recv); return ERR_OK;
} /******************************************************************************
* 描述 : 创建tcp服务器
* 参数 : 无
* 返回 : 无
******************************************************************************/
void tcp_server_init(void)
{
struct tcp_pcb *tpcb; /* 创建tcp控制块 */
tpcb = tcp_new(); if (tpcb != NULL)
{
err_t err; /* 绑定端口接收,接收对象为所有ip地址 */
err = tcp_bind(tpcb, IP_ADDR_ANY, TCP_LOCAL_PORT); if (err == ERR_OK)
{
/* 监听 */
tpcb = tcp_listen(tpcb); /* 注册接入回调函数 */
tcp_accept(tpcb, tcp_server_accept); printf("tcp server listening\r\n");
printf("tcp server ip:%d:%d:%d:%d prot:%d\r\n",
*((uint8_t *)&ipaddr.addr),
*((uint8_t *)&ipaddr.addr + ),
*((uint8_t *)&ipaddr.addr + ),
*((uint8_t *)&ipaddr.addr + ),
tpcb->local_port);
}
else
{
memp_free(MEMP_TCP_PCB, tpcb); printf("can not bind pcb\r\n");
} }
} /******************************** END OF FILE ********************************/
本例用到的上位机IP为192.168.2.194,开放端口为8881
STM32的IP为192.168.2.8,开放端口为8880
先将STM32上电,STM32调试串口输出以下信息:
tcp server listening
tcp server ip:::: prot:
然后通过网络调试助手连接到STM32的tcp服务器:
STM32调试串口输出以下信息:
tcp client connected
ip ::: port:
在网络调试助手发送信息”hello zorb”,STM32调试串口输出以下信息:
get msg from ::: port::
hello zorb
四、最后
本文测试了lwip的tcp功能,能正常连接并收发数据,撒花。
github:https://github.com/54zorb/stm32-lwip
版权所有,转载请打赏哟
如果你喜欢我的文章,可以通过微信扫一扫给我打赏哟
stm32+lwip(三):TCP测试的更多相关文章
- stm32+lwip(二):UDP测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- stm32+lwip(五):以太网帧发送测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- stm32+lwip(四):网页服务器测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- stm32+lwip(一):使用STM32CubeMX生成项目
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- 免花生壳 TCP测试 DTU测试 GPRS测试TCP服务器
通常在学习GPRS或者DTU的时候,往往没有自己的服务器,很多时候我们只能用这个模块打个电话发个短信,但是随着移动互联的兴起,各行各业大家都开始弄移动接入.为了这个需求,这里提供TCP移动接入. 工作 ...
- 2018.10.2浪在ACM 集训队第三次测试赛
2018.10.26 浪在ACM 集训队第三次测试赛 今天是暴力场吗???????可怕 题目一览表 来源 考察知识点 完成时间 A 1275 珠心算测试 NOIP 普及组 2014 暴力??? 201 ...
- 两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)
本篇文章主要介绍了"两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)",主要涉及到两款JSON类库Jackson与JSON-lib的性能对比(新增第三款 ...
- 2018.11.2浪在ACM集训队第三次测试赛
2018.11.2 浪在ACM 集训队第三次测试赛 整理人:孔晓霞 A 珠心算测试 参考博客:[1]李继朋 B 比例简化 参考博客: [1]李继朋 C 螺旋矩阵 参考博客:[1]朱远迪 D 子矩阵 ...
- Keil MDK STM32系列(三) 基于标准外设库SPL的STM32F407开发
Keil MDK STM32系列 Keil MDK STM32系列(一) 基于标准外设库SPL的STM32F103开发 Keil MDK STM32系列(二) 基于标准外设库SPL的STM32F401 ...
随机推荐
- sql 2008 远程过程调用失败 0x800706be
啊哦,SQL Server挂了!sql 2008 远程过程调用失败 0x800706be,找了一下解决方案,如下: 1.打开控制面板->添加删除程序 2.卸载一个叫Microsoft SQL S ...
- jQuery的datatable的destroy属性,和$("#test").dataTable().fnDestroy();区别,两者的区别
jQuery的datatable的destroy属性,和$("#test").dataTable().fnDestroy();区别,两者的区别. 1 destroy属性是,销毁实例 ...
- Android(java)学习笔记55:LayoutInflater 和 findViewById
1. 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(). 不同点是LayoutInflater是用来找res/layout/下的xml布局文件, ...
- selenium定位学习回顾
之前已经专门学过了定位,但后来因为浏览器比较方便,可以直接复制xpath和css进行定位,个人觉得自己快遗忘了这块,所以专门来回顾一下, 顺便记录一下,以便后期查看. 进行web页面自动化测试,对页面 ...
- cascade DecodeBBox层
https://zhuanlan.zhihu.com/p/36095768 我的推断,第二第三阶段应该不是把所有anchor进行bounding box regression,然后再选取当前条件下的所 ...
- 标准对象——RegExp
字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样麻烦且代码难以复 ...
- 在cengos中安装zabbix server/agent, 并创建一个简单demo
添加zabbix更新源 rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch ...
- Vue node.js商城-购物车模块
一.渲染购物车列表页面 新建src/views/Cart.vue获取cartList购物车列表数据就可以在页面中渲染出该用户的购物车列表数据 data(){ return { car ...
- 与JSON相关的问题
1.JSON.stringify 与 JSON.parse 相关的问题 JSON.stringify 把字符串转化为字符串,JSON.parse把字符串转化为JSON格式 会出现的问题Unexpect ...
- 轻量ORM-SqlRepoEx (二)初始化SqlRepoEx
一.SqlRepoEx引用 暂时没放至nuget上,可以直接到https://github.com/AzThinker/SqlRepoEx下载源码,编译引用. (一).静态引用 1.需引用以下dll在 ...