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实现oracle数据泵导出功能
脚本如下:[oracle@ycr python]$ more dump.py #/usr/bin/python#coding:utf8 import sysimport osimport time n ...
- POJ 1830 开关问题 【01矩阵 高斯消元】
任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- POJ 1088 滑雪 【记忆化搜索经典】
题目链接:http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- POJ 1579 Function Run Fun 【记忆化搜索入门】
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
- [19/03/13-星期三] 数组_二维数组&冒泡排序&二分查找
一.二维数组 多维数组可以看成以数组为元素的数组.可以有二维.三维.甚至更多维数组,但是实际开发中用的非常少.最多到二维数组(我们一般使用容器代替,二维数组用的都很少). [代码示例] import ...
- H5中的微信支付、支付宝支付
微信支付的申请: 公众号支付.扫码支付等在微信公众平台.移动端的申请在微信开放平台 公众号支付流程:申请微信公众号(服务号并完成微信认证)——申请微信支付商户号(申请微信支付,资料审核通过以后,请前往 ...
- html、css和js原生写一个模态弹出框,顺便解决父元素半透明子元素不透明效果
模态框: html部分: <!-- 按钮 --> <button id="box" onclick="pop_box()">弹出框< ...
- html单选框(性别选择)
在写单选框时,如何实现只能同时只能选择一个radio. 将name设置为一样的数值:代码如下: <input class="myforms-3-2" type="r ...
- 用HTML写伪类选择器,结构伪类选择器,伪元素选择器样式
html,css lorem乱序铭文 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, nihil? Lorem ...
- linux ccenteros 部署 redis
step one : yum install redis -- 安装redis数据库 step two:安装完成之后开启redis 服务 service redis start syste ...