gpt4 book ai didi

c - 当设备都具有相同的 deviceID 时,除了枚举之外,还有其他方法可以获取许多 IrDA 设备的套接字吗?

转载 作者:行者123 更新时间:2023-11-30 17:49:20 25 4
gpt4 key购买 nike

自从发布我的original question关于枚举多个设备,我发现了未发生多次枚举的原因;这些设备都具有相同的 deviceID。有了这个发现,我想提出一个新问题,希望能找到解决办法......

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

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

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

[编辑]
图1:总体场景:
enter image description here

图 2:来自设备管理器:
enter image description here

图 3:每个“找到的”IrDA 设备的属性:
enter image description here

以下是枚举两个或多个独特 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);
}

最佳答案

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

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

关于c - 当设备都具有相同的 deviceID 时,除了枚举之外,还有其他方法可以获取许多 IrDA 设备的套接字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17928852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com