简体   繁体   English

如何从LabWindows \\ CVI C代码或CMD命令获取当前正在运行的以太网速度

[英]How to get current running ethernet speed from LabWindows\CVI C code or CMD commands

I am developing Test Equipment which has couple of Ethernet ports. 我正在开发具有几个以太网端口的测试设备。 As part of the testing i want to check the current speed of the ethernet ports (10/100/1000) when a tested unit is connected. 作为测试的一部分,我想在连接被测设备时检查以太网端口的当前速度(10/100/1000)。

How can i get this information? 我如何获得此信息? is there a C library or CMD command which i can use that can supply this information? 我可以使用可以提供此信息的C库或CMD命令吗?

C# is probably quicker to get up and running than API access with c/c++. 与使用c / c ++进行API访问相比,C#的建立和运行速度可能更快。

Try using the System.Net.NetworkInformation classes. 尝试使用System.Net.NetworkInformation类。 In particular, System.Net.NetworkInformation.IPv4InterfaceStatistics ought to have some information along the lines of what you're looking for. 特别是, System.Net.NetworkInformation.IPv4InterfaceStatistics应该具有与您所寻找内容System.Net.NetworkInformation.IPv4InterfaceStatistics信息。

Specifically, you can check the bytesReceived property, wait a given interval, and then check the bytesReceived property again to get an idea of how many bytes/second your connection is processing. 具体来说,您可以检查bytesReceived属性,等待给定的时间间隔,然后再次检查bytesReceived属性以了解连接正在处理多少字节/秒。 To get a good number, though, you should try to download a large block of information from a given source, and check then; 但是,要获得一个好的数字,您应该尝试从给定的源下载大量信息,然后进行检查; that way you should be 'maxing' the connection when you do the test, which should give more helpful numbers. 这样一来,您在进行测试时就应该“最大化”连接,这将提供更多有用的数字。

You need a linux running machine for the code to work You need to use the SIOCETHTOOL ioctl() call in Linux. 您需要一台Linux运行机器才能使代码正常工作。您需要在Linux中使用SIOCETHTOOL ioctl()调用。

#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/ethtool.h>
#include <string.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
    int sock;
    struct ifreq ifr;
    struct ethtool_cmd edata;
    int rc;

    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sock < 0) {
        perror("socket");
        exit(1);
    }

    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
    ifr.ifr_data = &edata;

    edata.cmd = ETHTOOL_GSET;

    rc = ioctl(sock, SIOCETHTOOL, &ifr);
    if (rc < 0) {
        perror("ioctl");
        exit(1);
    }
    switch (ethtool_cmd_speed(&edata)) {
        case SPEED_10: printf("10Mbps\n"); break;
        case SPEED_100: printf("100Mbps\n"); break;
        case SPEED_1000: printf("1Gbps\n"); break;
        default: printf("Speed returned is %d\n", edata.speed);
    }

    return (0);
}

I'm not sure about the Windows .May be you can refer here: Microsoft Developer Network 我不确定Windows。可能您可以在这里参考: Microsoft Developer Network

use wmic command like this 使用这样的wmic命令

wmic PATH Win32_NetworkAdapter WHERE "PhysicalAdapter = TRUE AND NetEnabled = TRUE" GET Name, Speed

Win32_NetworkAdapter classof WMI WMI的Win32_NetworkAdapter类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM