|

楼主 |
发表于 2020-12-4 16:40:41
|
显示全部楼层
UDP速度测试(测试错误)。
测试条件和TCP是一样的:
备份测试程序。UDP不分主从,给IP地址和端口号就可以发送。
H743的UDP程序:
- #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 1001
- #define Local_NUM 1001
- /*
- *********************************************************************************************************
- * 变量
- *********************************************************************************************************
- */
- static NET_ADDR addr = { NET_ADDR_IP4, PORT_NUM, IP1, IP2, IP3, IP4};
- static int32_t udp_sock; // UDP socket handle
- /* 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_callback
- * 功能说明: TCP Socket的回调函数
- * 形 参: socket UDP Socket类型
- * remip 远程设备的IP地址
- * remport 远程设备的端口号
- * buf 远程设备发来的数据地址
- * len 远程设备发来的数据长度,单位字节
- * 返 回 值: 默认返回0即可,一般用不上
- *********************************************************************************************************
- */
- static uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len)
- {
- // char buffer[50];
- // uint16_t i = 0;
- // /* 确保是udp_soc的回调 */
- // if (socket != udp_sock)
- // {
- // return (0);
- // }
- // /* 发消息的远程IP,打印IP和端口号 */
- // sprintf(buffer, "远程连接IP: %d.%d.%d.%d", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3]);
- // printf_debug("%s port:%d\r\n", buffer, addr->port);
- // /* 接收到的数据长度,单位字节 */
- // printf_debug("Data length = %d\r\n", len);
- // /* 打印接收到的消息 */
- // for(i = 0; i < len; i++)
- // {
- // printf_debug("buf[%d] = %d\r\n", i, buf[i]);
- // }
- return (0);
- }
- /*
- *********************************************************************************************************
- * 函 数 名: DM9162TCPnetTest
- * 功能说明: DM9162TCPnetTest应用
- * 形 参: 无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void DM9162TCPnetTest(void)
- {
- int32_t iCount;
- uint8_t *sendbuf;
- netStatus res;
- const uint16_t usMaxBlockTime = 2; /* 延迟周期 */
- uint32_t EvtFlag;
- uint32_t t1, t2;
-
- udp_sock = netUDP_GetSocket (udp_cb_func);
-
- if (udp_sock >= 0)
- {
- netUDP_Open (udp_sock, Local_NUM);
- //netUDP_SetOption (udp_sock, netUDP_OptionInterface, NET_IF_CLASS_ETH | 0);
- }
- while (1)
- {
- EvtFlag = osThreadFlagsWait(0x00000007U, osFlagsWaitAny, usMaxBlockTime);
-
- switch (EvtFlag)
- {
- /* 接收到K1键按下,给远程TCP客户端发送数据 */
- case KEY1_BIT0:
- iCount = 50*1024;
- t1 = osKernelGetTickCount ();
- printf("------%d\r\n", t1);
- do
- {
- /* 申请8字节的空间 */
- sendbuf = netUDP_GetBuffer(2000);
-
- if(sendbuf != NULL)
- {
- // /* 初始化8个字节变量 */
- // sendbuf[0] = '1';
- // sendbuf[1] = '2';
- // sendbuf[2] = '3';
- // sendbuf[3] = '4';
- // sendbuf[4] = '5';
- // sendbuf[5] = '6';
- // sendbuf[6] = '7';
- // sendbuf[7] = '8';
- res = netUDP_Send(udp_sock, &addr, sendbuf, 2000);
- /* 保证发送成功了发送次数才可以减一,这里发送成功并不保证远程设备接受成功 */
- if(res == netOK)
- {
- iCount--;
- }
- else
- {
- printf("发送失败%s\r\n", ReVal_Table[res]);
- }
- //osDelay(50);
- }
- }while(iCount > 0);
- t2 = osKernelGetTickCount ();
- printf("测试速度 = %4.1fMB/S\r\n", (float)(50.0*1024*2000)/(t2-t1) * 1000/1024/1024);
- break;
- /* 其他的键值不处理 */
- default:
- break;
- }
- }
- }
复制代码
F407的UDP程序:
- #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 240
- #define PORT_NUM 1001
- #define Local_NUM 1001
- /*
- *********************************************************************************************************
- * 变量
- *********************************************************************************************************
- */
- static NET_ADDR addr = { NET_ADDR_IP4, PORT_NUM, IP1, IP2, IP3, IP4};
- static int32_t udp_sock; // UDP socket handle
- /* 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_callback
- * 功能说明: TCP Socket的回调函数
- * 形 参: socket UDP Socket类型
- * remip 远程设备的IP地址
- * remport 远程设备的端口号
- * buf 远程设备发来的数据地址
- * len 远程设备发来的数据长度,单位字节
- * 返 回 值: 默认返回0即可,一般用不上
- *********************************************************************************************************
- */
- static uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len)
- {
- // char buffer[50];
- // uint16_t i = 0;
- // /* 确保是udp_soc的回调 */
- // if (socket != udp_sock)
- // {
- // return (0);
- // }
- // /* 发消息的远程IP,打印IP和端口号 */
- // sprintf(buffer, "远程连接IP: %d.%d.%d.%d", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3]);
- // printf_debug("%s port:%d\r\n", buffer, addr->port);
- // /* 接收到的数据长度,单位字节 */
- // printf_debug("Data length = %d\r\n", len);
- // /* 打印接收到的消息 */
- // for(i = 0; i < len; i++)
- // {
- // printf_debug("buf[%d] = %d\r\n", i, buf[i]);
- // }
- return (0);
- }
- /*
- *********************************************************************************************************
- * 函 数 名: DM9162TCPnetTest
- * 功能说明: DM9162TCPnetTest应用
- * 形 参: 无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void DM9162TCPnetTest(void)
- {
- int32_t iCount;
- uint8_t *sendbuf;
- netStatus res;
- const uint16_t usMaxBlockTime = 2; /* 延迟周期 */
- uint32_t EvtFlag;
- uint32_t t1, t2;
-
- udp_sock = netUDP_GetSocket (udp_cb_func);
-
- if (udp_sock >= 0)
- {
- netUDP_Open (udp_sock, Local_NUM);
- //netUDP_SetOption (udp_sock, netUDP_OptionInterface, NET_IF_CLASS_ETH | 0);
- }
- while (1)
- {
- EvtFlag = osThreadFlagsWait(0x00000007U, osFlagsWaitAny, usMaxBlockTime);
-
- switch (EvtFlag)
- {
- /* 接收到K1键按下,给远程TCP客户端发送数据 */
- case KEY1_BIT0:
- iCount = 50*1024;
- t1 = osKernelGetTickCount ();
- printf("------%d\r\n", t1);
- do
- {
- /* 申请8字节的空间 */
- sendbuf = netUDP_GetBuffer(2000);
-
- if(sendbuf != NULL)
- {
- // /* 初始化8个字节变量 */
- // sendbuf[0] = '1';
- // sendbuf[1] = '2';
- // sendbuf[2] = '3';
- // sendbuf[3] = '4';
- // sendbuf[4] = '5';
- // sendbuf[5] = '6';
- // sendbuf[6] = '7';
- // sendbuf[7] = '8';
- res = netUDP_Send(udp_sock, &addr, sendbuf, 2000);
- /* 保证发送成功了发送次数才可以减一,这里发送成功并不保证远程设备接受成功 */
- if(res == netOK)
- {
- iCount--;
- }
- else
- {
- printf("发送失败%s\r\n", ReVal_Table[res]);
- }
- //osDelay(50);
- }
- }while(iCount > 0);
- t2 = osKernelGetTickCount ();
- printf("测试速度 = %4.1fMB/S\r\n", (float)(50.0*1024*2000)/(t2-t1) * 1000/1024/1024);
- break;
- /* 其他的键值不处理 */
- default:
- break;
- }
- }
- }
复制代码 |
|