|
TCP速度测试:
H743开发板的DM9162做客户端,F407开发板的DM9162做服务器,两个板子直连,H743发送数据给F407(H743的收发性能基本都可以满速,而F407接收性能满速,发送略差点)。
备份下程序。
H743客户端,IP地址192.168.28.240.
- #include "includes.h"
- /*
- *********************************************************************************************************
- * 用于本文件的调试
- *********************************************************************************************************
- */
- #if 1
- #define printf_debug printf
- #else
- #define printf_debug(...)
- #endif
- /*
- *********************************************************************************************************
- * 宏定义,远程服务器的IP和端口
- *********************************************************************************************************
- */
- /* 要访问的远程服务器IP和端口配置,也就是电脑端调试助手设置的IP和端口号 */
- #define IP1 192
- #define IP2 168
- #define IP3 28
- #define IP4 241
- #define PORT_NUM 1002
- /*
- *********************************************************************************************************
- * 变量
- *********************************************************************************************************
- */
- NET_ADDR4 addr = { NET_ADDR_IP4, PORT_NUM, IP1,IP2,IP3,IP4};
- int32_t tcp_sock;
- /* TCPnet API的返回值 */
- static const char * ReVal_Table[]=
- {
- "netOK: Operation succeeded",
- "netBusy: Process is busy",
- "netError: Unspecified error",
- "netInvalidParameter: Invalid parameter specified",
- "netWrongState: Wrong state error",
- "netDriverError: Driver error",
- "netServerError: Server error",
- "netAuthenticationFailed: User authentication failed",
- "netDnsResolverError: DNS host resolver failed",
- "netFileError: File not found or file r/w error",
- "netTimeout: Operation timeout",
- };
- /*
- *********************************************************************************************************
- * 函 数 名: tcp_cb_client
- * 功能说明: TCP Socket的回调函数
- * 形 参: socket 句柄
- * event 事件类型
- * addr NET_ADDR类型变量,记录IP地址,端口号。
- * buf ptr指向的缓冲区记录着接收到的TCP数据。
- * len 记录接收到的数据个数。
- * 返 回 值:
- *********************************************************************************************************
- */
- uint32_t tcp_cb_client (int32_t socket, netTCP_Event event,
- const NET_ADDR *addr, const uint8_t *buf, uint32_t len)
- {
- switch (event)
- {
- /*
- 远程客户端连接消息
- 1、数组ptr存储远程设备的IP地址,par中存储端口号。
- 2、返回数值1允许连接,返回数值0禁止连接。
- */
- case netTCP_EventConnect:
- return (1);
- /* Socket远程连接已经建立 */
- case netTCP_EventEstablished:
- printf_debug("Socket is connected to remote peer\r\n");
- break;
- /* 连接断开 */
- case netTCP_EventClosed:
- printf_debug("Connection has been closed\r\n");
- break;
- /* 连接终止 */
- case netTCP_EventAborted:
- break;
- /* 发送的数据收到远程设备应答 */
- case netTCP_EventACK:
- // printf("----%d\r\n", osKernelGetTickCount ());
- break;
- /* 接收到TCP数据帧,ptr指向数据地址,par记录数据长度,单位字节 */
- case netTCP_EventData:
- //printf_debug("Data length = %d\r\n", len);
- //printf ("%.*s\r\n",len, buf);
- break;
- }
- return (0);
- }
- /*
- *********************************************************************************************************
- * 函 数 名: TCPnetTest
- * 功能说明: TCPnet应用
- * 形 参: 无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void DM9162TCPnetTest(void)
- {
- int32_t iCount;
- uint8_t *sendbuf;
- uint32_t maxlen;
- netStatus res;
- const uint16_t usMaxBlockTime = 2; /* 延迟周期 */
- uint32_t EvtFlag;
- uint32_t t1, t2;
- tcp_sock = netTCP_GetSocket (tcp_cb_client);
-
- if (tcp_sock > 0)
- {
- /* 使能TCP_TYPE_KEEP_ALIVE,会一直保持连接 */
- netTCP_SetOption (tcp_sock, netTCP_OptionKeepAlive, 1);
- res = netTCP_Connect (tcp_sock, (NET_ADDR *)&addr, 0);
- printf_debug("%s\r\n", ReVal_Table[res]);
- }
-
- while (1)
- {
- EvtFlag = osThreadFlagsWait(0x0000000FU, osFlagsWaitAny, usMaxBlockTime);
-
- /* 按键消息的处理 */
- switch (EvtFlag)
- {
- /* 接收到K1键按下,给远程TCP客户端发送8字节数据 */
- case KEY1_BIT0:
- t1 = osKernelGetTickCount ();
- printf("------%d\r\n", t1);
- iCount = 100*1024*1024;
- if(netTCP_GetState(tcp_sock) == netTCP_StateESTABLISHED)
- {
- do
- {
- if(netTCP_SendReady(tcp_sock) == true )
- {
- maxlen = netTCP_GetMaxSegmentSize (tcp_sock);
- iCount -= maxlen;
-
- if(iCount < 0)
- {
- /* 这么计算没问题的 */
- maxlen = iCount + maxlen;
- }
-
- sendbuf = netTCP_GetBuffer (maxlen);
-
- /* 必须使用申请的内存空间 */
- netTCP_Send (tcp_sock, sendbuf, maxlen);
- }
-
- }while(iCount > 0);
- }
- t2 = osKernelGetTickCount ();
- printf("测试速度 = %4.1fMB/S\r\n", (float)(100.0*1024*1024)/(t2-t1) * 1000/1024/1024);
- break;
-
- /* 其他的键值不处理 */
- default:
- break;
- }
- }
- }
复制代码
F407服务器,IP地址192.168.28.241
- #include "includes.h"
- /*
- *********************************************************************************************************
- * 用于本文件的调试
- *********************************************************************************************************
- */
- #if 1
- #define printf_debug printf
- #else
- #define printf_debug(...)
- #endif
- /*
- *********************************************************************************************************
- * 宏定义
- *********************************************************************************************************
- */
- #define PORT_NUM 1002 /* TCP服务器监听端口号 */
- /*
- *********************************************************************************************************
- * 变量
- *********************************************************************************************************
- */
- static int32_t tcp_sock;
- /* TCPnet API的返回值 */
- static const char * ReVal_Table[]=
- {
- "netOK: Operation succeeded",
- "netBusy: Process is busy",
- "netError: Unspecified error",
- "netInvalidParameter: Invalid parameter specified",
- "netWrongState: Wrong state error",
- "netDriverError: Driver error",
- "netServerError: Server error",
- "netAuthenticationFailed: User authentication failed",
- "netDnsResolverError: DNS host resolver failed",
- "netFileError: File not found or file r/w error",
- "netTimeout: Operation timeout",
- };
- /*
- *********************************************************************************************************
- * 函 数 名: tcp_cb_server
- * 功能说明: TCP Socket的回调函数
- * 形 参: socket 句柄
- * event 事件类型
- * addr NET_ADDR类型变量,记录IP地址,端口号。
- * buf ptr指向的缓冲区记录着接收到的TCP数据。
- * len 记录接收到的数据个数。
- * 返 回 值:
- *********************************************************************************************************
- */
- static uint32_t tcp_cb_server (int32_t socket, netTCP_Event event,
- const NET_ADDR *addr, const uint8_t *buf, uint32_t len)
- {
- switch (event)
- {
- /*
- 远程客户端连接消息
- 1、数组ptr存储远程设备的IP地址,par中存储端口号。
- 2、返回数值1允许连接,返回数值0禁止连接。
- */
- case netTCP_EventConnect:
- if (addr->addr_type == NET_ADDR_IP4)
- {
- printf_debug("DM9000远程客户端请求连接IP: %d.%d.%d.%d 端口号:%d\r\n",
- addr->addr[0],
- addr->addr[1],
- addr->addr[2],
- addr->addr[3],
- addr->port);
- return (1);
- }
- else if (addr->addr_type == NET_ADDR_IP6)
- {
- return (1);
- }
-
- return(0);
- /* Socket远程连接已经建立 */
- case netTCP_EventEstablished:
- printf_debug("DM9000 Socket is connected to remote peer\r\n");
- break;
- /* 连接断开 */
- case netTCP_EventClosed:
- printf_debug("DM9000 Connection has been closed\r\n");
- break;
- /* 连接终止 */
- case netTCP_EventAborted:
- break;
- /* 发送的数据收到远程设备应答 */
- case netTCP_EventACK:
- break;
- /* 接收到TCP数据帧,ptr指向数据地址,par记录数据长度,单位字节 */
- case netTCP_EventData:
- //printf_debug("DM9000 Data length = %d\r\n", len);
- //printf ("%.*s\r\n",len, buf);
- break;
- }
- return (0);
- }
- /*
- *********************************************************************************************************
- * 函 数 名: DM9000TCPnetTest
- * 功能说明: DM9000TCPnetTest应用
- * 形 参: 无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void DM9162TCPnetTest(void)
- {
- int32_t iCount;
- uint8_t *sendbuf;
- uint32_t maxlen;
- netStatus res;
- const uint16_t usMaxBlockTime = 2; /* 延迟周期 */
- uint32_t EvtFlag;
- uint32_t t1, t2;
-
- tcp_sock = netTCP_GetSocket (tcp_cb_server);
-
- if (tcp_sock > 0)
- {
- res = netTCP_Listen (tcp_sock, PORT_NUM);
- printf_debug("DM9000 tcp listen res = %s\r\n", ReVal_Table[res]);
- }
-
- while (1)
- {
- EvtFlag = osThreadFlagsWait(0x00000007U, osFlagsWaitAny, usMaxBlockTime);
-
- /* 按键消息的处理 */
- if(netTCP_GetState(tcp_sock) == netTCP_StateESTABLISHED)
- {
- switch (EvtFlag)
- {
- /* 接收到K1键按下,给远程TCP客户端发送8字节数据 */
- case KEY1_BIT0:
- t1 = osKernelGetTickCount ();
- printf("------%d\r\n", t1);
- iCount = 100*1024*1024;
- if(netTCP_GetState(tcp_sock) == netTCP_StateESTABLISHED)
- {
- do
- {
- if(netTCP_SendReady(tcp_sock) == true )
- {
- maxlen = netTCP_GetMaxSegmentSize (tcp_sock);
- iCount -= maxlen;
-
- if(iCount < 0)
- {
- /* 这么计算没问题的 */
- maxlen = iCount + maxlen;
- }
-
- sendbuf = netTCP_GetBuffer (maxlen);
-
- /* 必须使用申请的内存空间 */
- netTCP_Send (tcp_sock, sendbuf, maxlen);
- }
-
- }while(iCount > 0);
- }
- t2 = osKernelGetTickCount ();
- printf("测试速度 = %4.1fMB/S\r\n", (float)(100.0*1024*1024)/(t2-t1) * 1000/1024/1024);
- break;
- /* 其他的键值不处理 */
- default:
- break;
- }
- }
- }
- }
复制代码
|
评分
-
查看全部评分
|