0

自从发布关于枚举多个设备的原始问题以来,我发现了没有发生多个枚举的原因;这些设备都具有相同的 deviceID。有了这个发现,我想提出一个新问题,希望能解决...

[编辑]
新问题:
当所有设备都使用相同的 deviceID 时,除了唯一枚举之外,是否有一种方法可用于为许多设备中的每一个获取专用套接字?

场景和规则:
我正在使用 Microsoft SDK 和 ANSI C 编译器在 Windows 7 上开发应用程序。应用程序设计要求它检测范围内的任何 IrDA 设备,使用套接字连接并进行通信。将通过多个 IrDA 加密狗(每个设备一个加密狗)与多个设备进行通信,每个加密狗通过 USB 连接到 PC。注意:应避免使用虚拟 COM 端口。
目标:将每个设备连接到它自己的套接字,在它和应用软件之间提供一个专有的通信通道。

插图和源代码(用于列举 IrDA 设备)如下。

[编辑]
图 1:总体情况:
在此处输入图像描述

图 2:从设备管理器:
在此处输入图像描述

图 3:每个“找到”的 IrDA 设备的属性:
在此处输入图像描述

这是枚举两个或多个独特 IrDA 设备的代码:(经过测试并且运行良好)

    //Adapted from:  http://msdn.microsoft.com/en-us/library/windows/desktop/ms738544(v=vs.85).aspx
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <af_irda.h>
    #include <stdio.h>
    #include <windows.h>

    // link with Ws2_32.lib
    char* iGetLastErrorText(DWORD nErrorCode);


    int __cdecl main(void)
    {

        //-----------------------------------------
        // Declare and initialize variables
        WSADATA wsaData;

        int iResult;
        int i, tries = 0;
        DWORD dwError;

        SOCKET Sock = INVALID_SOCKET;

    #define DEVICE_LIST_LEN    10


        SOCKADDR_IRDA DestSockAddr = { AF_IRDA, 0, 0, 0, 0, "SampleIrDAService" };

        unsigned char DevListBuff[sizeof (DEVICELIST) -
                      sizeof (IRDA_DEVICE_INFO) +
                      (sizeof (IRDA_DEVICE_INFO) * DEVICE_LIST_LEN)];

        int DevListLen = sizeof (DevListBuff);
        PDEVICELIST pDevList;

        pDevList = (PDEVICELIST) & DevListBuff;

        // Initialize Winsock
        iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
        if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
        }

        Sock = socket(AF_IRDA, SOCK_STREAM, 0);
        if (Sock == INVALID_SOCKET) {
        dwError = WSAGetLastError();
        printf
            ("socket failed trying to create an AF_IRDA socket with error %d\n",
             dwError);

        if (dwError == WSAEAFNOSUPPORT) {
            printf("Check that the local computer has an infrared device\n");
            printf
            ("and a device driver is installed for the infrared device\n");
        }
        WSACleanup();
        return 1;
        }

        pDevList->numDevice = 0;

        while((pDevList->numDevice < 2)&&( tries++ < 3 ))
        {
            // Sock is not in connected state
            iResult = getsockopt(Sock, SOL_IRLMP, IRLMP_ENUMDEVICES,
                     (char *) pDevList, &DevListLen);

            if (iResult == SOCKET_ERROR) {
            printf("getsockopt failed with error %d - %s\n", WSAGetLastError(), iGetLastErrorText(WSAGetLastError()));
                iResult = closesocket(Sock);
                if(iResult == SOCKET_ERROR)
                {
                printf("closesocket failed with error %d - %s\n", WSAGetLastError(), iGetLastErrorText(WSAGetLastError()));
                WSACleanup();
                    getchar();
                return 1;
                }
            WSACleanup();
                getchar();
            return 1;
            }

            if (pDevList->numDevice == 0) {
            // no devices discovered or cached
            // not a bad idea to run a couple of times
            printf("No IRDA devices were discovered or cached\n");
            } else {
            // one per discovered device
            printf("pDevList->numDevice = %d\n", pDevList->numDevice);
                for (i = 0; i < (int) pDevList->numDevice; i++) {

                    printf( "irdaCharSet: %d\nHints1: %d\nHints2: %d\nDeviceID: %x\nDeviceName: %s\n\n", 
                             pDevList->Device[i].irdaCharSet, 
                             pDevList->Device[i].irdaDeviceHints1, 
                             pDevList->Device[i].irdaDeviceHints2, 
                             pDevList->Device[i].irdaDeviceID, 
                             pDevList->Device[i].irdaDeviceName);





                    // typedef struct _IRDA_DEVICE_INFO
                // {
                //     u_char    irdaDeviceID[4];
                //     char      irdaDeviceName[22];
                //     u_char    irdaDeviceHints1;
                //     u_char    irdaDeviceHints2;
                //     u_char    irdaCharSet;
                // } _IRDA_DEVICE_INFO;

                // pDevList->Device[i]. see _IRDA_DEVICE_INFO for fields
                // display the device names and let the user select one
            }
            }
        }

        // assume the user selected the first device [0]
        memcpy(&DestSockAddr.irdaDeviceID[0], &pDevList->Device[0].irdaDeviceID[0],4);

        iResult = connect(Sock, (const struct sockaddr *) &DestSockAddr, sizeof (SOCKADDR_IRDA));
        if (iResult == SOCKET_ERROR) 
        {
            if (iResult == SOCKET_ERROR) {
            printf("connect failed with error %d - %s\n", WSAGetLastError(), iGetLastErrorText(WSAGetLastError()));
                iResult = closesocket(Sock);
                if(iResult == SOCKET_ERROR)
                {
                printf("closesocket failed with error %d - %s\n", WSAGetLastError(), iGetLastErrorText(WSAGetLastError()));
                WSACleanup();
                    getchar();
                return 1;
                }
            WSACleanup();
                getchar();
            return 1;
            }
        } 
        else 
        {
            printf("connect to first IRDA device was successful\n");
        }

        getchar();

        WSACleanup();
        return 0;
    }       

    char* iGetLastErrorText(DWORD nErrorCode)
    {
        char* msg;
        // Ask Windows to prepare a standard message for a GetLastError() code:
        FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, nErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
        // Return the message
        if (!msg)
        return("Unknown error");
        else
        return(msg);
    }
4

1 回答 1

0

结束这个老问题的循环: 简短的回答是否定的,没有其他方法。

IrDA 套接字是创建的,在我的例子中,在 Windows 7 中,基于 Windows对任何和所有活动且在范围内的 IrDA 信号的询问进行枚举。枚举过程存储它找到的每个唯一设备 ID(特别是 IrDA 设备 ID)的数据。如果它看到 3 个唯一的设备 ID,则可以存储每个设备 ID,并使其可用于完成连接。随后,对于建立的每个连接,可以与这些设备中的每一个进行单独的对话。但是,如果相同的三个设备处于活动状态,并且在范围内,但具有相同的设备 ID,Windows 将仅存储与该 ID 对应的数据,并且只能建立一个连接。

于 2014-02-05T21:06:56.977 回答