当前位置: 首页 > web >正文

测试W5500的第2步_使用ioLibrary库创建TCP客户端

ioLibrary库下载地址:文件下载地址:https://gitee.com/wiznet-hk/STM32F10x_W5500_Examples
源文件下载地址:https://gitee.com/wiznet-hk

没有注册的,只能复制粘贴了。

本文介绍了如何初始化STM32的硬件资源,配置W5500的网络参数,并通过DHCP获取动态IP地址。并使用这个IP地址配置TCP客户端,以及双方交换数据的测试。

前篇文中有wiz_platform.c和wiz_platform.h,用来初始化SPI,RST引脚和TIM2,这里就不用粘贴了。

前篇文中有wiz_interface.h,用来通过DHCP获取动态IP地址,这里也不用粘贴了。

1、W5500_Variable.c和.h文件

#include "W5500_Variable.h"
#include "socket.h"	// Just include one header for WIZCHIP
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()
#include "string.h" //使能strcpy(),strlen(),memset()//W5500的网络参数
//本地物理地址:00 08 DC 11 11 11
//本地IP地址:192.168.1.199
//本地子网掩码:	255.255.255.0
//本地网关:192.168.1.1
//DNS服务器IP地址:8.8.8.8
//通过DHCP获取动态IP地址
/* network information */
wiz_NetInfo default_net_info = {{0x00, 0x08, 0xdc,0x11, 0x11, 0x11},{192, 168, 1, 199},{255,255,255,0},{192, 168, 1, 1},{8,8,8,8},NETINFO_DHCP}; //动态IP
wiz_NetInfo net_info;uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0};//端口0的网络参数
uint16_t LocalPort0=5000;	               //端口0的本地端口号(5000)
uint8_t  dest_ip[4]={192,168,1,190}; //端口0的远程IP地址:192.168.1.190
uint16_t dest_port=6000;             //端口0的远程端口号:6000
#ifndef _W5500_Variable_H
#define _W5500_Variable_H#include "stm32f10x.h"//使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#include "wizchip_conf.h"extern wiz_NetInfo default_net_info;
extern wiz_NetInfo net_info;#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
extern uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE];extern uint16_t LocalPort0;	   //端口0的本地端口号(5000)
extern uint8_t  dest_ip[4]; //端口0的远程IP地址:192.168.1.190
extern uint16_t dest_port;  //端口0的远程端口号:6000
#endif

 2、TestTcpClient.c和.h文件

#include "TestTcpClient.h"
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()
#include "w5500.h"
#include "W5500_Variable.h"
#include "socket.h"/*** @brief   tcp client loopback test* @param   sn:         socket number* @param   buf:        Data sending and receiving cache* @param   destip:     Destination IP address* @param   destport:   Destination port* @return  value for SOCK_ERRORs,return 1:no error 
*/
int32_t loop_TestTcpClient(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
{int32_t ret; // return value for SOCK_ERRORsuint16_t size = 0, sentsize=0;uint16_t any_port = 	50000;//配置本地端口为50000switch(getSn_SR(sn))//获取W5500端口sn的状态寄存器{case SOCK_ESTABLISHED ://W5500端口sn已经连接成功if(getSn_IR(sn) & Sn_IR_CON){//读端口sn的Sn_IR中断标志寄存器的bit0
#ifdef _LOOP_TestTcpClient_DEBUG_printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
#endifsetSn_IR(sn, Sn_IR_CON);  // this interrupt should be write the bit cleared to '1'//回写端口sn的Sn_IR中断标志寄存器的bit0,清除中断标志}if((size = getSn_RX_RSR(sn)) > 0)//读端口sn的Sn_RX_RSR寄存器,获取该端口的接收缓冲区的数据长度{if(size > ETHERNET_BUF_MAX_SIZE) size = ETHERNET_BUF_MAX_SIZE; // ETHERNET_BUF_MAX_SIZE means user defined buffer size (array)ret = recv(sn, buf, size);//读"W5500端口sn"的数据,长度为size个字节,保存到bufif(ret > 0){buf[ret]='\0';            //添加字符串结束符printf("recv: %s\n",buf); // print the receive data}else return ret; //接收数据错误size = (uint16_t) ret;sentsize = 0;while(size != sentsize){ret = send(sn, buf+sentsize, size-sentsize);//将(buf+sentsize)为首地址的存储单元的前len个字节通过"W5500端口sn"发送出去// Data send process (User's buffer -> Destination through H/W Tx socket buffer)if(ret < 0) // Send Error occurred (sent data length < 0){close(sn); //关闭端口sn的连接, socket closereturn ret;}else{printf("send: %s\n",buf+sentsize); // print the send data}sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.}}//break;case SOCK_CLOSE_WAIT ://W5500端口sn处于等待关闭状态
#ifdef _LOOP_TestTcpClient_DEBUG_printf("%d:CloseWait\r\n",sn);
#endifif((ret=disconnect(sn)) != SOCK_OK)//关闭端口sn的连接return ret;
#ifdef _LOOP_TestTcpClient_DEBUG_printf("%d:Socket Closed\r\n", sn);
#endifbreak;case SOCK_INIT ://W5500端口sn已经初始化
#ifdef _LOOP_TestTcpClient_DEBUG_printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
#endifif( (ret = connect(sn, destip, destport)) != SOCK_OK){//将W5500端口sn连接到远程IP地址destip和远程端口destport上//	Try to TCP connect to the TCP server (destination)return ret;}break;case SOCK_CLOSED://W5500端口sn处于关闭状态close(sn);//关闭端口sn的连接, socket closeif((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){//修改端口,执行连接服务器if(any_port == 0xffff) any_port = 50000;return ret; // TCP socket open with 'any_port' port number} 
#ifdef _LOOP_TestTcpClient_DEBUG_printf("%d:TCP client loopback start\r\n",sn);printf("%d:Socket opened\r\n",sn);
#endifbreak;default:break;}return 1;
}
#ifndef _TestTcpClient_H
#define _TestTcpClient_H#include "stm32f10x.h"//使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t#define	_LOOP_TestTcpClient_DEBUG_  //允许串口跟踪int32_t loop_TestTcpClient(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport);#endif

3、main.c代码如下:

#include "stm32f10x.h"//使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()
#include "string.h" //使能strcpy(),strlen(),memset()
#include "delay.h"
#include "USART4.h"
#include "LED.h"//文件下载地址:https://gitee.com/wiznet-hk/STM32F10x_W5500_Examples
//源文件下载地址:https://gitee.com/wiznet-hk
#include "wiz_platform.h"
#include "wizchip_conf.h"
#include "wiz_interface.h"
#include "W5500_Variable.h"
#include "TestTcpClient.h"const char CPU_Reset_REG[]="\r\nCPU reset!\r\n";
int main(void)
{//	SCB->VTOR = 0x8000000;//中断向量表重定义//	SystemInit();delay_init();//延时函数初始化NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4USART4_Serial_Interface_Enable(115200);printf("%s",CPU_Reset_REG);//调试串口输出"\r\nCPU reset!\r\n"LED_Init();LED0_ON();wiz_timer_init();  //配置TIM2每毫秒中断一次wiz_spi_init();    //SPI1初始化wiz_rst_int_init();//初始化W5500的RST引脚和INT引脚printf("%s network install example\r\n",_WIZCHIP_ID_);wizchip_initialize();//1.注册SPI片选函数,单字节读写函数和多字节读写函数//2.W5500使用RST引脚复位//3.读取芯片版本号码,并检查是否正确//4.读PHY配置寄存器的bit[2:0],bit0=1表示W5500连接到局域网//bit1=1表示当前网速为100M,否则为10M//bit2=1表示当前以太网采用全双工通讯,否则为半双工通讯network_init(ethernet_buf, &default_net_info);
//DHCP客户端使用端口0获取本地网络信息
//1.使用"默认网络参数"设置本地网络参数:MAC地址,GW网关,SN子网掩码,本地IP地址,DNS服务器IP地址,DHCP
//2.DHCP获取动态IP地址
//3.使用DHCP获取到的网络参数设置本地网络参数:MAC地址,GW网关,SN子网掩码,本地IP地址,DNS服务器IP地址,DHCP
//4.读本地网络参数:MAC地址,GW网关,SN子网掩码,本地IP地址,DNS服务器IP地址,DHCP,然后从串口输出setSn_KPALVTR(SOCKET0, 6); // 30s keepalive//Socket在线时间寄存器,在线验证心跳包传输时间,他只在TCP模式下生效,在其他模式下将会被忽略,单位时间为5秒。//因此心跳包传输时间为6*5=30秒while(1){loop_TestTcpClient(SOCKET0, ethernet_buf, dest_ip, dest_port);LED0=!LED0;delay_ms(1000);}
}

4、添加文件如下:

5、 TCP Server配置如下:

6、串口跟踪如下:

http://www.xdnf.cn/news/7573.html

相关文章:

  • 【razor】回环结构导致的控制信令错位:例如发送端收到 SR的问题
  • k8s 配置 Kafka SASL_SSL双重认证
  • 计算机网络基础概念
  • 新能源汽车焊接智能节气阀
  • uni-app 中使用 mumu模拟器 进行调试和运行详细教程
  • Matplotlib 高级进阶实战:多维度数据可视化组合图表
  • nbufxz动态规划1
  • 零基础设计模式——创建型模式 - 工厂方法模式
  • 【课堂笔记】核方法和Mercer定理
  • [Java实战]Spring Boot整合Sentinel:流量控制与熔断降级实战(二十九)
  • 数据集划分与格式转换:从原始数据到模型训练的关键步骤
  • 在 Excel 中使用通义灵码辅助开发 VBA 程序
  • 自学嵌入式 day21 - 数据结构 双向链表
  • 全局对比度调整
  • MCP 协议传输机制大变身:抛弃 SSE,投入 Streamable HTTP 的怀抱
  • taro 小程序 CoverImage Image src无法显示图片的问题
  • 剧本杀小程序:指尖上的沉浸式推理宇宙
  • 【Linux笔记】——线程同步信号量与环形队列生产者消费者模型的实现(PV操作)
  • shp2pgsql 导入 Shp 到 PostGIS 空间数据库
  • MATLAB中进行语音信号分析
  • Kotlin 协程 (一)
  • 对冲策略加仓止损盈思路
  • 数组的概述
  • 反射在spring boot自动配置的应用
  • Mysql 中的日期时间函数汇总
  • 2025ICPC南昌邀请赛题解
  • 基于规则引擎与机器学习的智能Web应用防火墙设计与实现
  • 【数据库课程设计】网上投票管理系统
  • 阿博图书馆管理系统 Java+Spring Boot+MySQL 实战项目分享
  • leetcode hot100:一、解题思路大全:技巧(只出现一次的数字、多数元素、颜色分类、下一个排列、寻找重复数)、矩阵(矩阵置零、螺旋矩阵、旋转图像、搜索二维矩阵Ⅱ)