Tip / ログイン to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

デバイス インスタンス パスを使用した複数のUSBシリアル デバイスへのアクセス – KBA228257- Community Translated (JA)

デバイス インスタンス パスを使用した複数のUSBシリアル デバイスへのアクセス – KBA228257- Community Translated (JA)

ChaitanyaV_61
Employee
Employee
50 questions asked 25 likes received 25 sign-ins

Community Translated by  YoOb_1790021          Version: **

Translation - English: Accessing Multiple USB-Serial Devices using the Device Instance Path – KBA228257

質問:

複数のデバイスが接続されているとき、USBシリアル デバイスへのアクセス方法を教えてください。

回答:

ベンダーモードでI2C、SPI、またはUARTとして設定されたUSBシリアル製品がUSBホストへ接続されたとき、設定に基づいて1つ以上のデバイスがエニュメレートされます。

Windows OSは、デバイス エニュメレーション プロセス中に各デバイスに特定のデバイス エニュメレーション インデックスを割り当てます。また、このUSBデバイス エニュメレーション プロセスは、すべてのUSBデバイスに対して一意のデバイス インスタンス パスを作成します。このデバイス インスタンス パスを使用して、この記事で定義されている関数を利用して、デバイスのインスタンス パスをそれぞれのデバイス エニュメレーション インデックスにマッピングすることにより、同じVIDとPIDを持つデバイスを区別できます。

以下に定義する GetDevicePath() 関数は、CyOpen および CyGetDeviceInfo API と同様に、サイプレス デバイスのデバイス エニュメレーション インデックスをパラメータとして受け取り、Windows APIを使用してそのデバイスのデバイス インスタンス パスを出力します。

注: setupapi ライブラリはプロジェクトにリンクする必要があります。Property Page → Linker → Input → Additional Dependencies に setupapi.lib を追加することによって行えます。

コード:

// Additional header file to be added

#include <setupapi.h>

// GetDevicePath Function

void GetDevicePath(int deviceNumber)

{

       //Initialization

       GUID CyDrvGuid = { 0xae18aa60, 0x7f6a, 0x11d4, 0x97, 0xdd, 0x0, 0x1, 0x2, 0x29, 0xb9, 0x59 };   // cyusb3 guid

       SP_DEVINFO_DATA devInfoData;

       SP_DEVICE_INTERFACE_DATA  devInterfaceData;

       PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData;

       ULONG requiredLength = 0;

       HDEVINFO hwDeviceInfo = SetupDiGetClassDevs((LPGUID)& CyDrvGuid,   //Returns a handle to the device information set

              NULL,

              NULL,

              DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);

       if (hwDeviceInfo != INVALID_HANDLE_VALUE) { //checks if the handle is invalid

              devInterfaceData.cbSize = sizeof(devInterfaceData);         //get the size of devInterfaceData structure

              //enumerates the device interfaces that are contained in a device information set

              if (SetupDiEnumDeviceInterfaces(hwDeviceInfo, 0, (LPGUID)& CyDrvGuid,    

                     deviceNumber, &devInterfaceData)) {                                  

                     SetupDiGetInterfaceDeviceDetail(hwDeviceInfo, &devInterfaceData, NULL, 0,

                     &requiredLength, NULL);

                     ULONG predictedLength = requiredLength;

                     functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(predictedLength);

                     functionClassDeviceData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

                     devInfoData.cbSize = sizeof(devInfoData);

                     //Retrieve the information from Plug and Play including the device path

                     if (SetupDiGetInterfaceDeviceDetail(hwDeviceInfo,

                   &devInterfaceData,

                   functionClassDeviceData,

                   predictedLength,

                   &requiredLength,

                   &devInfoData)) {

                   wprintf(L"%ls\n", functionClassDeviceData->DevicePath); //Prints the device path of the required device

                     }

              }

       }

       SetupDiDestroyDeviceInfoList(hwDeviceInfo);

}

0 件の賞賛
2051 件の閲覧回数
寄稿者