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 ...
- mysql_01_游标的使用
一.表的创建 1.直接创建表 DROP TABLE IF EXISTS shops_info; /*EMP产品版本版本信息表*/ CREATE TABLE shops_info ( ID INT PR ...
- 深度剖析hdfs原理
大数据底层技术的三大基石起源于Google在2006年之前的三篇论文GFS.Map-Reduce. Bigtable,其中GFS.Map-Reduce技术直接支持了Apache Hadoop项目的诞生 ...
- June 04th 2017 Week 23rd Sunday
It is not the mountain we conquer but outselves. 我们要征服的不是高山,而是我们自己. After days of hard working, I sl ...
- May 18th 2017 Week 20th Thursday
The greater the struggle, the more glorious the triumph. 挑战愈艰巨,胜利愈辉煌. Sometimes it would be better t ...
- python入门12 列表list
列表使用率较高,方法也多. 列表的定义 #coding:utf-8 #/usr/bin/python """ 2018-11-10 dinghanhua 列表 " ...
- URI和URL有什么区别
URI 是从虚拟根路径开始的URL是整个链接如URL http://zhidao.baidu.com/question/68016373.html URI 是/question/68016373.ht ...
- IOS CoreLocation框架的使用(用于地理定位)
● 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ● 导航:去任意陌生的地方 ● 周边:找餐馆.找酒店.找银行.找电影院 ● 在上述应用中,都用到了地图和定位功能,在iOS开发中 ...
- maven之构建多模块maven工程
(一)环境搭建 1.Maven下载 ; http://maven.apache.org/download.cgi 第一个在Linux使用,第二个是在Windows,第三和第四是源码: 我们将下 ...
- sudo: Sorry, you must have a tty to run
The requiretty option in sudoers file The requiretty if set in sudo config file sudoers, sudo will o ...