c#实现HidUsb设备通信

上传者: u010465417 | 上传时间: 2019-12-21 21:19:34 | 文件大小: 307KB | 文件类型: rar
赋所有源代码,开发工具vs2010 framework3.5 baidu搜索c# HidUsb都是大同小异案例,而且拿下来基本不能用。大都是围绕public static extern int CreateFile(省略众多参数..);发现没有,copy下来测试基本都是用不了的。 原因很简单:windows不允许你用程序随便就去访问硬件设备。所以在此把之前做过的基于C#开发读写HidUsb设备的项目整理成一个简单的小案例,分享给大家,开发环境VS2010。 该案例重点在public static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); 看着貌似也是用到CreateFile这个函数,其实并不然,注意到没有"SafeFileHandle",这就是重点! 这样windows是允许程序访问外接hidusb设备的。 当然具体如何运用这个函数现在已经不是您应该 关心的了,因为我已经为您把它封装成一个类,您只要调用相应的方法就OK. 例: //第一步:获取HidUsb设备信息 List slist = new List(); UsbHidDevice usbhid = new UsbHidDevice(); usbhid.GetDeviceList(ref slist); //HidUsb设备信息包含在List数据集中 注:当获取到HidUsb设备信息为:\\?\hid#vid_0e2c&pid;_0112#6&1b44c403;&0&0000;#{4d1e55b2-f16f-11cf-88cb-001111000030}, 注意该字符串里的“vid_0e2c”和“pid_0112”部分,那么: vid为0e2c, pid为:0112 //第二步:创建一个HidUsb设备访问实例 UsbHidDevice Device = new UsbHidDevice(vid, pid); //第三步:连接HidUsb设备 Boolean connBool = Device.Connect(); //第四步:实现数据接收事件 Device.DataReceived += new UsbHidDevice.DataReceivedDelegate(Device_DataReceived); //当HidUsb设备返回信息时触发此事件 void Device_DataReceived(byte[] data) { //处理接收到的数据逻辑 } //第五步:向Hid设备发送数据"0xa0 00 0x12 0x9 0x22" string txt = "0xa0 00 0x12 0x9 0x22"; //把数据转换为字节数组 byte[] data = ConvertHelper.StringToByte(txt2); byte bt = 0; CommandMessage cmdMsg = new CommandMessage(bt, data); Boolean sbool = Device.SendMessage(cmdMsg); //发送数据 //第六步:释放所有资源 Device.Dispose();

文件下载

资源详情

[{"title":"( 83 个子文件 307KB ) c#实现HidUsb设备通信","children":[{"title":"USBDevice","children":[{"title":"USBHIDDevice","children":[{"title":"DevBroadcastDeviceinterface.cs <span style='color:#111;'> 450B </span>","children":null,"spread":false},{"title":"ConvertHelper.cs <span style='color:#111;'> 14.65KB </span>","children":null,"spread":false},{"title":"WindowsAPI.cs <span style='color:#111;'> 17.41KB </span>","children":null,"spread":false},{"title":"SpDeviceInterfaceData.cs <span style='color:#111;'> 391B </span>","children":null,"spread":false},{"title":"DevBroadcastDeviceinterface1.cs <span style='color:#111;'> 641B </span>","children":null,"spread":false},{"title":"DeviceCommunication.cs <span style='color:#111;'> 8.13KB </span>","children":null,"spread":false},{"title":"DevBroadcastHdr.cs <span style='color:#111;'> 364B </span>","children":null,"spread":false},{"title":"USBHIDEnum.cs <span style='color:#111;'> 900B </span>","children":null,"spread":false},{"title":"User32.cs <span style='color:#111;'> 548B </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.34KB </span>","children":null,"spread":false}],"spread":true},{"title":"DeviceChangeNotifier.cs <span style='color:#111;'> 6.76KB </span>","children":null,"spread":false},{"title":"HiddAttributes.cs <span style='color:#111;'> 385B </span>","children":null,"spread":false},{"title":"HidpCaps.cs <span style='color:#111;'> 1.00KB </span>","children":null,"spread":false},{"title":"DeviceInformationStructure.cs <span style='color:#111;'> 1.91KB </span>","children":null,"spread":false},{"title":"CommandMessage.cs <span style='color:#111;'> 1.81KB </span>","children":null,"spread":false},{"title":"SyncObjT.cs <span style='color:#111;'> 326B </span>","children":null,"spread":false},{"title":"UsbHidDevice.cs <span style='color:#111;'> 12.13KB </span>","children":null,"spread":false},{"title":"USBHID.cs <span style='color:#111;'> 8.40KB </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"Debug","children":[{"title":"USBHIDDevice.dll <span style='color:#111;'> 35.00KB </span>","children":null,"spread":false},{"title":"USBHIDDevice.csproj.FileListAbsolute.txt <span style='color:#111;'> 644B </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"USBHIDDevice.pdb <span style='color:#111;'> 79.50KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 5.77KB </span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":"USBHIDDevice.csproj <span style='color:#111;'> 3.27KB </span>","children":null,"spread":false},{"title":"Hid.cs <span style='color:#111;'> 2.33KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"Debug","children":[{"title":"USBHIDDevice.dll <span style='color:#111;'> 35.00KB </span>","children":null,"spread":false},{"title":"USBHIDDevice.zip <span style='color:#111;'> 15.97KB </span>","children":null,"spread":false},{"title":"USBHIDDevice.pdb <span style='color:#111;'> 79.50KB </span>","children":null,"spread":false}],"spread":false},{"title":"Release","children":null,"spread":false}],"spread":false},{"title":"SetupApi.cs <span style='color:#111;'> 1.25KB </span>","children":null,"spread":false},{"title":"DeviceDiscovery.cs <span style='color:#111;'> 6.55KB </span>","children":null,"spread":false}],"spread":false},{"title":"USBDevice.suo <span style='color:#111;'> 58.50KB </span>","children":null,"spread":false},{"title":"USBDevice.v11.suo <span style='color:#111;'> 53.50KB </span>","children":null,"spread":false},{"title":"USBDevice","children":[{"title":"Form1.cs <span style='color:#111;'> 13.16KB </span>","children":null,"spread":false},{"title":"USBDevice.csproj <span style='color:#111;'> 4.03KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"ItemObject.cs <span style='color:#111;'> 754B </span>","children":null,"spread":false},{"title":"USBDev","children":[{"title":"DevBroadcastDeviceinterface.cs <span style='color:#111;'> 454B </span>","children":null,"spread":false},{"title":"ConvertHelper.cs <span style='color:#111;'> 14.65KB </span>","children":null,"spread":false},{"title":"WindowsAPI.cs <span style='color:#111;'> 17.41KB </span>","children":null,"spread":false},{"title":"SpDeviceInterfaceData.cs <span style='color:#111;'> 395B </span>","children":null,"spread":false},{"title":"DevBroadcastDeviceinterface1.cs <span style='color:#111;'> 645B </span>","children":null,"spread":false},{"title":"DeviceCommunication.cs <span style='color:#111;'> 8.49KB </span>","children":null,"spread":false},{"title":"DevBroadcastHdr.cs <span style='color:#111;'> 368B </span>","children":null,"spread":false},{"title":"User32.cs <span style='color:#111;'> 552B </span>","children":null,"spread":false},{"title":"DeviceChangeNotifier.cs <span style='color:#111;'> 6.76KB </span>","children":null,"spread":false},{"title":"HiddAttributes.cs <span style='color:#111;'> 389B </span>","children":null,"spread":false},{"title":"HidpCaps.cs <span style='color:#111;'> 1.01KB </span>","children":null,"spread":false},{"title":"DeviceInformationStructure.cs <span style='color:#111;'> 1.91KB </span>","children":null,"spread":false},{"title":"CommandMessage.cs <span style='color:#111;'> 1.81KB </span>","children":null,"spread":false},{"title":"SyncObjT.cs <span style='color:#111;'> 330B </span>","children":null,"spread":false},{"title":"UsbHidDevice.cs <span style='color:#111;'> 10.21KB </span>","children":null,"spread":false},{"title":"Hid.cs <span style='color:#111;'> 2.34KB </span>","children":null,"spread":false},{"title":"SetupApi.cs <span style='color:#111;'> 1.25KB </span>","children":null,"spread":false},{"title":"DeviceDiscovery.cs <span style='color:#111;'> 6.59KB </span>","children":null,"spread":false}],"spread":false},{"title":"Form1.resx <span style='color:#111;'> 6.22KB </span>","children":null,"spread":false},{"title":"app.config <span style='color:#111;'> 117B </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"Settings.settings <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"Resources.Designer.cs <span style='color:#111;'> 2.79KB </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.34KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.08KB </span>","children":null,"spread":false},{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false}],"spread":true},{"title":"dll","children":[{"title":"hid.dll <span style='color:#111;'> 20.50KB </span>","children":null,"spread":false}],"spread":true},{"title":"Form1.Designer.cs <span style='color:#111;'> 14.19KB </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"x86","children":[{"title":"Debug","children":[{"title":"USBDevice.csproj.FileListAbsolute.txt <span style='color:#111;'> 2.34KB </span>","children":null,"spread":false},{"title":"TempPE","children":[{"title":"Properties.Resources.Designer.cs.dll <span style='color:#111;'> 4.50KB </span>","children":null,"spread":false}],"spread":false},{"title":"USBDevice.csprojResolveAssemblyReference.cache <span style='color:#111;'> 5.91KB </span>","children":null,"spread":false},{"title":"USBDevice.csproj.GenerateResource.Cache <span style='color:#111;'> 847B </span>","children":null,"spread":false},{"title":"USBDevice.exe <span style='color:#111;'> 22.50KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.23KB </span>","children":null,"spread":false},{"title":"USBDevice.Form1.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferences.cache <span style='color:#111;'> 6.37KB </span>","children":null,"spread":false},{"title":"USBDevice.Properties.Resources.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"USBDevice.pdb <span style='color:#111;'> 41.50KB </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":true},{"title":"USBVisit.cs <span style='color:#111;'> 8.14KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"Debug","children":[{"title":"USBDevice.vshost.exe.config <span style='color:#111;'> 117B </span>","children":null,"spread":false},{"title":"USBHIDDevice.dll <span style='color:#111;'> 35.00KB </span>","children":null,"spread":false},{"title":"USBHIDDevice.pdb <span style='color:#111;'> 79.50KB </span>","children":null,"spread":false},{"title":"USBDevice.exe <span style='color:#111;'> 22.50KB </span>","children":null,"spread":false},{"title":"USBDevice.exe.config <span style='color:#111;'> 117B </span>","children":null,"spread":false},{"title":"USBDevice.vshost.exe <span style='color:#111;'> 11.34KB </span>","children":null,"spread":false},{"title":"USBDevice.pdb <span style='color:#111;'> 41.50KB </span>","children":null,"spread":false},{"title":"USBDevice.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false}],"spread":false},{"title":"Debug.rar <span style='color:#111;'> 57.11KB </span>","children":null,"spread":false},{"title":"Release","children":null,"spread":false}],"spread":false}],"spread":false},{"title":"USBDevice.sln <span style='color:#111;'> 2.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

  • BTSSkyFly :
    别下了,发现不了
    2021-06-23
  • *素* :
    没什么用,连接不上
    2021-05-20
  • diao_run_ze :
    没什么用,不要上当
    2021-04-10
  • 大婶在行动 :
    为什么下发数据时,后面会带上很多其他的数据呢,从bushound里面抓取看到的
    2021-03-17
  • weixin_45641119 :
    好东西,正好用上
    2021-01-25

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明