使用LwIP实现TCP Client通信(基于STM32F407)

目录

概述

1 功能介绍

1.1 代码框架

2.2 搭建系统

2 TCP Client功能实现

2.1 代码实现

2.2 具体代码

3 功能测试

3.1 测试功能描述

3.2 运行代码


测试代码下载地址:

stm32-f407-dm9161-LwIP-tcp-client资源-CSDN文库

概述

本文主要介绍使用STM32F407和LwIP实现基于TCP/IP 协议的Client,笔者记录搭建系统的整个过程,并在板卡上运行,以测试Client连接至Server,并且可以正常接收或者发送数据。

1 功能介绍

1.1 代码框架

笔者基于ST官方移植的LwIP的示例代码,对其修改并使其支持STM32F407芯片,以太网络芯片为DM9161。下面介绍该和LwIP功能相关的测试程序的框架结构:

1)LwIP

LwIP的应用代码包,包括各类底层协议的实现代码,应用代码,还包括内存分配函数等

2)LwIP/Drv

DM9161的驱动程序,以及stm32f407 ETH接口的驱动程序

3)LwIP/App

使用LwIP的代码库实现user的功能代码

2.2 搭建系统

1)使用串口调试工具,监控log信息

2)使用网络调试助手,并将其配置为server,并配置相关的IP和端口号

2 TCP Client功能实现

2.1 代码实现

代码99行: 配置服务器的IP

代码102行:创建tcp控制块

代码109行:绑定客户端的IP和端口号

代码114行:连接服务器,并调用回调函数

2.2 具体代码

#include "bsp.h"
/* for LwIP */
#include "netconf.h"

#include "tcp.h"

#include "lwip/memp.h"
#include "lwip/tcp.h"


void lwip_pro(void);

/* 定义端口号 */
#define TCP_REMOTE_PORT    19999 /* 远端端口 */
#define TCP_LOCAL_PORT     2980  /* 本地端口 */

/******************************************************************************
 * 描述  : 数据接收回调函数
 * 参数  : -
 * 返回  : -
******************************************************************************/
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 + 1),
            *((uint8_t *)&tpcb->remote_ip.addr + 2),
            *((uint8_t *)&tpcb->remote_ip.addr + 3),
            tpcb->remote_port);
        
        while(ptmp != NULL)
        {
            for (i = 0; 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"), 0);

    /* 注册接收回调函数 */
    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, 192, 168, 1, 4);

    /* 创建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");
        }
    }
}

/*
*********************************************************************************************************
*函 数 名: main
*功能说明: c程序入口
*形    参:无
*返 回 值: 错误代码(无需处理)
*********************************************************************************************************
*/
int main(void)
{
    bsp_Init();         /* 硬件初始化 */

    /* Initilaize the LwIP stack */
    LwIP_Init();

    tcp_client_init();

    while(1)
    {
        lwip_pro();
    }
}


/*
*********************************************************************************************************
*函 数 名: lwip_pro
*功能说明: lwip 轮询,插入到主循环中
*形    参: 无
*返 回 值: 无
*********************************************************************************************************
*/
void lwip_pro(void)
{
    /* check if any packet received */
    if (ETH_CheckFrameReceived())
    {
        /* process received ethernet packet */
        LwIP_Pkt_Handle();
    }

    /* handle periodic timers for LwIP */
    LwIP_Periodic_Handle(bsp_GetRunTime() / 10);
}


3 功能测试

3.1 测试功能描述

1)上位机使用网络调试助手作为服务器,并发送数据给客户端

2)客户端接收到server发送的数据后,并通过串口终端打印出来

3)比较server和Client两边的数据是否一致

3.2 运行代码

1) 下载代码至板卡,保证Client能正常的连接到Server

2) Client 通过串口终端查看连接情况

 3)Server端口发送数据至Client,查看其是否一致

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-03 07:34:11       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-03 07:34:11       18 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-03 07:34:11       17 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-03 07:34:11       20 阅读

热门阅读

  1. go的grpc的三种流模式通信

    2024-05-03 07:34:11       10 阅读
  2. MongoDB聚合运算符:$sum

    2024-05-03 07:34:11       12 阅读
  3. opencv namedWindow函数

    2024-05-03 07:34:11       12 阅读
  4. 数论7-同余

    2024-05-03 07:34:11       10 阅读
  5. 周报 | 24.4.22-24.4.28文章汇总

    2024-05-03 07:34:11       13 阅读
  6. 计算机视觉(CV)是什么以及应用场景

    2024-05-03 07:34:11       11 阅读
  7. Mac 电脑 vscode 终端提示 zsh: command not found

    2024-05-03 07:34:11       11 阅读