需要有关使用Windows API的C#中的USB通信的帮助 [英] Need a help for USB Communication in C# Using Windows API's

查看:161
本文介绍了需要有关使用Windows API的C#中的USB通信的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在使用C#与连接到PC的外部硬件单元进行通信.
我实际上还处于USB通信的初期,实际上是设备枚举.

我在Windows API中使用"SetupDiEnumDeviceInterfaces"方法来枚举连接的设备.
但是我总是收到错误"259"的"false".我无法理解与此代码相关的错误.
如果有人可以答复我,我将非常感激.

代码:

Dear All,

I am using C# to communicate with an external hardware unit connected to the PC.
I am still in the initial stage of USB communication,actually speaking device enumeration.

I am using "SetupDiEnumDeviceInterfaces" metod in windows API''s to enumerate the devices connected.
But I always get "false" with error "259".I can''t understand the error related to this code.
I will be very thankful if some one can reply me.

Code:

private class Sp_DEVICE_INTERFACE_DETAIL_DATA
{
  public int cbsize;
  public string Devicepath;
}

private class SP_DEVICE_INTERFACE_DATA
{
  public int cbSize;
  public Guid InterfaceClassGuid;
  public int FLAGS;
  public int Reserved;
};

[DllImport("hid.dll")]
private static extern void HidD_GetHidGuid(ref Guid GUID);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator, IntPtr hwndParent, UInt32 Flags);

IntPtr deviceinfoset;

[DllImport("setupapi.dll")]
private static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, int DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
int MemberIndex;
Boolean Result;

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
private static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceIntefaceData, IntPtr DeviceInterfaceDetailData, int DeviceInterfacedetailDatasize, ref int DeviceInterfacedetaildataSize, IntPtr DeviceInfoData);
int BufferSize;
Boolean Success;
int counter;

public void Initialize_Device(){
  counter = 0;

  Guid hidguid = new Guid();
  HidD_GetHidGuid(ref hidguid);

  const int DIGCF_PRESENT = (0x00000002);
  const int DIGCF_DEVICEINTERFACE = (0x000000001);
  deviceinfoset = SetupDiGetClassDevsA(ref hidguid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

   listBox1.Items.Add("Device Info Set=" + deviceinfoset.ToString());
   listBox1.Items.Add("HidGuid=" + hidguid.ToString());

  

   MyDeviceInterfaceData.cbSize = 28;
   MyDeviceInterfaceData.InterfaceClassGuid = System.Guid.Empty;
   MyDeviceInterfaceData.FLAGS = 0;
   MyDeviceInterfaceData.Reserved = 0;
   MemberIndex = 0;
   Result = SetupDiEnumDeviceInterfaces(deviceinfoset, 0, ref hidguid, MemberIndex, ref MyDeviceInterfaceData);

   listBox1.Items.Add("Result=" + Result.ToString());

}

//the above method is called at the click event of a button



[edit]已添加代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

请尝试查看该代码,

通过HidD_GetHidGuid获取HID GUID.

调用SetupDiGetClassDevs和

使用SetupDiEnumDeviceInfo和SetupDiGetDeviceRegistryProperty查看结果.

调用SetupDiEnumDeviceInterfaces,确保使用GUID参数
Please try to look onto that,

 Get the HID GUID with HidD_GetHidGuid.

 Call SetupDiGetClassDevs and

 See the results with SetupDiEnumDeviceInfo and SetupDiGetDeviceRegistryProperty.

 Call SetupDiEnumDeviceInterfaces, making sure to use the GUID parameter


亲爱的所有人,

我想我找到了USB通信的解决方案.我将尽快将C#中的软件源代码,PIC C中的固件代码以及使用PIC18f4550实现的硬件电路上载到该论坛.非常感谢!!!
Dear All,

I think i found the solution for USB communication.I will upload the software source codes in C#,firmware codes in PIC C and hardware circuits implemented using PIC18f4550 to this forum as soon as possible.Thank you very much!!!


这篇关于需要有关使用Windows API的C#中的USB通信的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆