原创调试W5500芯片--W5500的TCPUDP回环测试

Posted tonyjia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原创调试W5500芯片--W5500的TCPUDP回环测试相关的知识,希望对你有一定的参考价值。

一、回环测试函数的实现,如下面的代码,主函数while循环中调用下面的函数loop_back

 1 void loop_back(void)
 2 {
 3     int32_t ret = 0;
 4     
 5     /* Loopback Test */
 6         // TCP server loopback test
 7         if( (ret = loopback_tcps(SOCK_TCPS, gDATABUF, 5000)) < 0) {
 8             printf("SOCKET ERROR : %ld
", ret);
 9         }
10     // UDP server loopback test
11         if( (ret = loopback_udps(SOCK_UDPS, gDATABUF, 3000)) < 0) {
12             printf("SOCKET ERROR : %ld
", ret);
13         }
14 }
 1 int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
 2 {
 3    int32_t ret;
 4    uint16_t size = 0, sentsize=0;
 5 
 6 #ifdef _LOOPBACK_DEBUG_
 7    uint8_t destip[4];
 8    uint16_t destport;
 9 #endif
10 
11    switch(getSn_SR(sn))
12    {
13       case SOCK_ESTABLISHED : //Success to connect
14          if(getSn_IR(sn) & Sn_IR_CON)
15          {
16 #ifdef _LOOPBACK_DEBUG_
17             getSn_DIPR(sn, destip);    //获取客户端的ip地址
18             destport = getSn_DPORT(sn);    //获取客户端的端口号
19 
20             printf("%d:Connected - %d.%d.%d.%d : %d
",sn, destip[0], destip[1], destip[2], destip[3], destport);
21 #endif
22             setSn_IR(sn,Sn_IR_CON);//设置socket的连接中断标志位
23          }
24          if((size = getSn_RX_RSR(sn)) > 0) // Don‘t need to check SOCKERR_BUSY because it doesn‘t not occur.
25          {
26             if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
27             ret = recv(sn, buf, size);
28 
29             if(ret <= 0) return ret;      // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
30             size = (uint16_t) ret;
31             sentsize = 0;
32 
33             while(size != sentsize)
34             {
35                 ret = send(sn, buf+sentsize, size-sentsize);
36                 if(ret < 0)
37                 {
38                     close(sn);
39                     return ret;
40                 }
41                 sentsize += ret; // Don‘t care SOCKERR_BUSY, because it is zero.
42             }
43          }
44          break;
45       case SOCK_CLOSE_WAIT :    //Closing state
46 #ifdef _LOOPBACK_DEBUG_
47          //printf("%d:CloseWait
",sn);
48 #endif
49          if((ret = disconnect(sn)) != SOCK_OK) return ret;
50 #ifdef _LOOPBACK_DEBUG_
51          printf("%d:Socket Closed
", sn);
52 #endif
53          break;
54       case SOCK_INIT :    //Initiate state
55 #ifdef _LOOPBACK_DEBUG_
56          printf("%d:Listen, TCP server loopback, port [%d]
", sn, port);
57 #endif
58          if( (ret = listen(sn)) != SOCK_OK) return ret;
59          break;
60       case SOCK_CLOSED:    //Closed
61 #ifdef _LOOPBACK_DEBUG_
62          //printf("%d:TCP server loopback start
",sn);
63 #endif
64          if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
65 #ifdef _LOOPBACK_DEBUG_
66          //printf("%d:Socket opened
",sn);
67 #endif
68          break;
69       default:
70          break;
71    }
72    return 1;
73 }
 1 int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port)
 2 {
 3    int32_t  ret;
 4    uint16_t size, sentsize;
 5    uint8_t  destip[4];
 6    uint16_t destport;
 7 
 8    switch(getSn_SR(sn))
 9    {
10       case SOCK_UDP :
11          if((size = getSn_RX_RSR(sn)) > 0)
12          {
13             if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
14             ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
15             if(ret <= 0)
16             {
17 #ifdef _LOOPBACK_DEBUG_
18                printf("%d: recvfrom error. %ld
",sn,ret);
19 #endif
20                return ret;
21             }
22             size = (uint16_t) ret;
23             sentsize = 0;
24             while(sentsize != size)
25             {
26                ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
27                if(ret < 0)
28                {
29 #ifdef _LOOPBACK_DEBUG_
30                   printf("%d: sendto error. %ld
",sn,ret);
31 #endif
32                   return ret;
33                }
34                sentsize += ret; // Don‘t care SOCKERR_BUSY, because it is zero.
35             }
36          }
37          break;
38       case SOCK_CLOSED:
39 #ifdef _LOOPBACK_DEBUG_
40          //printf("%d:UDP loopback start
",sn);
41 #endif
42          if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
43             return ret;
44 #ifdef _LOOPBACK_DEBUG_
45          printf("%d:Opened, UDP loopback, port [%d]
", sn, port);
46 #endif
47          break;
48       default :
49          break;
50    }
51    return 1;
52 }

二、下载程序开始测试测试

1.初始化后打印w5500相关信息

技术图片 

2.UDP回环测试,可以看到可以正常的从w5500的IP地址为192.168.1.123:3000端口用UDP的方式收到数据

技术图片

 

 3.TCP回环测试,可以看到可以正常的从w5500的IP地址为192.168.1.123:5000端口用TCP的方式收到数据

技术图片

 至此回环测试调试完成!下一次准备把freemodbus tcp的功能移植过来,小伙伴们期待一下吧!<_>

以上是关于原创调试W5500芯片--W5500的TCPUDP回环测试的主要内容,如果未能解决你的问题,请参考以下文章

W5500+STM32F103C8T6进行TCP通信(modbus)

WIZnet芯片的W5500

W5500开发笔记 | 01- W5500 Socket API的说明

基于STM32F1的局域网通信模块W5500驱动

W5500中断寄存器的理解

W5500 学习开发: 硬件使用说明