STM32 W5500 以太网应用开发:TCP 服务器与客户端实现
STM32+W5500+以太网应用开发+001_TCP 服务器和客户端
实验效果1-TCP服务器
实验效果2-TCP客户端
1 制作STM32CubeMX工程文件生成MDK工程
拷贝001_Ping.ioc到002_TCP文件夹,并重命名为002_TCP.ioc,重新生成MDK工程
2 添加W5500驱动库文件
3 添加头文件目录
4 实例1-TCP服务器
4.1 修改main.c代码
只列举和上一个例子不同地方的代码,其他可以参照001_Ping例子
STM32+W5500+以太网应用开发+001_Ping 新建工程,移植驱动-CSDN博客
/* USER CODE BEGIN PTD */
#define LISTEN_PORT 5000
#define RECEIVE_BUFF_SIZE 128
wiz_NetInfo gWIZNETINFO = {
.mac = { 0x80, 0x80, 0x80,0x80,0x80,0x80 },//MSB - LSB
.ip = { 192, 168, 1, 10 },
.sn = { 255, 255, 255, 0 },
.gw = { 192, 168, 1, 1 },//网关参数
.dns = { 8, 8, 8, 8 },
.dhcp = NETINFO_STATIC };
uint8_t receive_buff[RECEIVE_BUFF_SIZE];
/* USER CODE END PTD */
/* USER CODE BEGIN 2 */
printf("A simple TCP Server Application using W5500!\r\n");
W5500Init(); //W5500初始化
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); //配置网络参数
wiz_PhyConf phyconf;
phyconf.by = PHY_CONFBY_SW;
phyconf.duplex = PHY_DUPLEX_FULL;
phyconf.speed = PHY_SPEED_10;
phyconf.mode = PHY_MODE_AUTONEGO;
ctlwizchip(CW_SET_PHYCONF, (void*)&phyconf); //配置PHY参数
PHYStatusCheck(); //检查网络连接状态
PrintPHYConf(); //打印PHY配置信息
printf("Simple TCP Server Application\r\n");
/* USER CODE END 2 */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
printf("\r\nInitializing server socket\r\n");
//Parameters in order socket_id, protocol TCP or UDP, Port number, Flags=0
//Return value is socket ID on success
if(socket(1,Sn_MR_TCP,LISTEN_PORT,0)!=1)//创建一个socket:Socket号,TCP/UDP类型,端口号
{
//error
printf("Cannot create Socket!\r\n");
while(1);//halt here
}
//success
printf("Socket Created Successfully ! \r\n");
uint8_t socket_io_mode=SOCK_IO_BLOCK;
ctlsocket(1, CS_SET_IOMODE , &socket_io_mode);//set blocking IO mode
printf("IP Address is %d.%d.%d.%d\r\n",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("Start listening on port %d ! \r\n",LISTEN_PORT);
printf("Waiting for a client connection. \r\n");
//Make it a passive socket (i.e. listen for connection)
if(listen(1)!=SOCK_OK)//监听端口
{//our socket id is 1 (w5500 have 8 sockets from 0-7)
//error
printf("Cannot listen on port %d",LISTEN_PORT);
while(1);
}
uint8_t sr=0x00;//socket status register
do
{
sr=getSn_SR(1);//获取Sn_SR寄存器,参数0~7
}while (sr!=SOCK_ESTABLISHED && sr!=SOCK_CLOSED);
if(sr==SOCK_CLOSED)
{
printf("Some error occurred on server socket. Please restart.\r\n");
while(1);
}
if(sr==SOCK_ESTABLISHED)//成功连接
{
//we come here only when a client has connected.
//Now we can read data from the socket
printf("A client connected!\r\n");
printf("Waiting for Client Data ...!\r\n");
while(1)
{
int len=recv(1, receive_buff, RECEIVE_BUFF_SIZE);//从连接设备读取数据到receive_buff
if(len==SOCKERR_SOCKSTATUS)
{
//client has disconnected
printf("Client has disconnected\r\n");
printf("*** SESSION OVER ***\r\n\r\n");
break;
}
receive_buff[len]='\0';
printf("Received %d bytes from client\r\n",len);
printf("Data Received: %s", receive_buff);
if(strcmp((char*)receive_buff,"Who are u")==0)
{//判断接收到"Who are u"
memcpy(receive_buff,"I am role_2099!",15);//修改应答内容
len = 15;
}
//Echo the data back encloused in a [] pair
send(1,(uint8_t*)"[",1);//starting sq bracket 向客户端发送[
send(1,receive_buff,len);// the data 向客户端发送接收到的内容或者特定的回答
send(1,(uint8_t*)"]",1);//closing sq bracket 向客户端发送]
printf("\r\nECHO sent back to client\r\n");
//Look for quit message and quit if received
if(strcmp((char*)receive_buff,"QUIT")==0)
{//收到"QUIT",端口客户端连接
printf("Received QUIT command from client\r\n");
printf("Disconnecting ... \r\n");
printf("*** SESSION OVER ***\r\n\r\n");
disconnect(1);//disconnect from the clinet 断开客户端连接
break;//come out of while loop 退出,回到131行,重新等待客户端连接
}
}//While loop (as long as client is connected)
}//if block, client connect success
}
/* USER CODE END 3 */
4.2 测试
1> ping测试,确认通信正常
2> 客户端未连接时log信息
3> 使用网络调试助手配置为客户端,远程主机地址是开发板IP地址,端口号5000
4> 连接成功
5 实例2-TCP客户端
5.1 修改main.c代码
/* USER CODE BEGIN PTD */
uint8_t destination_ip[]={192,168,1,20}; //远程主机地址
uint16_t destination_port = 5000; //远程主机端口
#define LISTEN_PORT 5000 //本地主机端口
#define RECEIVE_BUFF_SIZE 128
wiz_NetInfo gWIZNETINFO = {
.mac = { 0x80, 0x80, 0x80,0x80,0x80,0x80 },//MSB - LSB
.ip = { 192, 168, 1, 10 }, //IP地址
.sn = { 255, 255, 255, 0 }, //子网掩码
.gw = { 192, 168, 1, 1 }, //网关参数
.dns = { 8, 8, 8, 8 }, //DNS
.dhcp = NETINFO_STATIC }; //DHCP关闭
uint8_t receive_buff[RECEIVE_BUFF_SIZE];
/* USER CODE END PTD */
/* USER CODE BEGIN 2 */
printf("A simple TCP Client Application using W5500!\r\n");
W5500Init(); //W5500初始化
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); //配置网络参数
wiz_PhyConf phyconf;
phyconf.by = PHY_CONFBY_SW;
phyconf.duplex = PHY_DUPLEX_FULL;
phyconf.speed = PHY_SPEED_10;
phyconf.mode = PHY_MODE_AUTONEGO;
ctlwizchip(CW_SET_PHYCONF, (void*)&phyconf); //配置PHY参数
PHYStatusCheck(); //检查网络连接状态
PrintPHYConf(); //打印PHY配置信息
printf("Simple TCP Client Application\r\n");
/* USER CODE END 2 */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//The return value of socket() call is the socket number if success
//we are using socket number 1 so it should return 1 for success
//The 3rd argument is the local port, when a client app is written
//better give 0 here, that will use a random available port
if(socket(1, Sn_MR_TCP, 0, 0)==1) //创建Socket=1
{
printf("\r\nSocket Created Successfully");
}
else
{
printf("\r\nCannot create socket");
while(1);
}
printf("\r\nConnecting to server: %d.%d.%d.%d @ TCP Port: %d",destination_ip[0],destination_ip[1],destination_ip[2],destination_ip[3],destination_port);
if(connect(1, destination_ip, destination_port)==SOCK_OK) //连接远程主机
{
printf("\r\nConnected with server.");
}
else
{
//failed
printf("\r\nCannot connect with server!");
while(1);
}
while (1)
{
//Return value of the send() function is the amount of data sent
if(send(1, "I am role_2099!\r\n", 16)<=SOCK_ERROR) //向服务器发送数据I am role_2099
{
printf("\r\nSending Failed!");
while(1);
}
else
{
printf("\r\nSending Success!");
}
HAL_Delay(1000);
}
}
/* USER CODE END 3 */
5.2 测试
1> 电脑端没有开启程序指定的服务器和端口
2> 网络调试助手配置,并开启服务器
3> 重新启动开发板,电脑端收到来自TCP客户端发来的信息
作者:米兰的无线电