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 ...
随机推荐
- python获取硬件信息模块
https://github.com/redhat-cip/hardware https://github.com/rdobson/python-hwinfo https://github.com/r ...
- 【Leetcode】【Easy】Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- JSP实现用户登录样例
业务描述 用户在login.jsp页面输入用户名密码登录: 如果用户名为xingoo,密码为123,则跳转到成功界面login_success.jsp,并显示用户登录的名字: 如果用户名密码错误,则跳 ...
- codeforces 611D New Year and Ancient Prophecy
f[i = 以i结尾][j = 长度为j] = 方案数. f[i][j] = sum{ f[i-j][k] , k < j || (k == j && s(i-j+1,j) &g ...
- 【转】eclipse 错误信息 "File Search" has encounter a problem 解决
在eclipse中使用搜索功能,发生错误: "File Search" has encounter a problem 仔细看了一下自动跳出的错误日志(Error Log),发现: ...
- LocalDB的奇怪问题
属性 MasterDBPath 不可用于 信息“Microsoft.SqlServer.Management.Smo.Information”.该对象可能没有此属性,也可能是访问权限不足而无法检索. ...
- html或jsp页面自动提交,无需每次重启服务
从eclipse转到idea遇到各种问题,之前eclipse可以自动保存页面内容无需重启服务,但是idea不可以,网上找了n种办法也没用,可能版本不一样吧,把我的解决方法纪录一下,方便以后有人遇到这个 ...
- eclipce导出项目发布到tomcat
1.右击项目-Except 2.在弹出框中输入“WAR file” 3.点击“next” 在Destinatin选择保存路径,即可 4.将保存的文件复制到tomcat下,启动tomcat之后,会自动解 ...
- python 并发编程之协程
一.协程 协程: 单线程下的并发,又称 微线程.协程是一种用户态的的轻量级线程,即协程是由用户程序自己控制调度的. 协程的本质就是在单线程下,由用户自己控制一个任务,遇到 io 阻塞就切换另外一个 ...
- Git错误
$ rm -rf .git $ git config --global core.autocrlf false $git init $git add . ---------------------- ...