C#与HidUsb设备通信

上传者: u010465417 | 上传时间: 2022-08-05 13:36:49 | 文件大小: 190KB | 文件类型: RAR
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 //创建一个HidUsb设备访问实例 UsbHidDevice Device = new UsbHidDevice(vid, pid); //实现数据接收事件 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":"( 52 个子文件 190KB ) C#与HidUsb设备通信","children":[{"title":"USBDevice","children":[{"title":"USBDevice.sln <span style='color:#111;'> 1.51KB </span>","children":null,"spread":false},{"title":"USBDevice.suo <span style='color:#111;'> 58.50KB </span>","children":null,"spread":false},{"title":"USBDevice","children":[{"title":"bin","children":[{"title":"Release","children":null,"spread":false},{"title":"Debug.rar <span style='color:#111;'> 57.11KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"USBDevice.pdb <span style='color:#111;'> 43.50KB </span>","children":null,"spread":false},{"title":"USBDevice.exe <span style='color:#111;'> 24.00KB </span>","children":null,"spread":false},{"title":"USBDevice.vshost.exe <span style='color:#111;'> 11.34KB </span>","children":null,"spread":false},{"title":"USBDevice.exe.config <span style='color:#111;'> 117B </span>","children":null,"spread":false},{"title":"USBDevice.vshost.exe.config <span style='color:#111;'> 117B </span>","children":null,"spread":false},{"title":"USBDevice.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"USBHIDDevice.dll <span style='color:#111;'> 35.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"dll","children":[{"title":"USBHIDDevice.dll <span style='color:#111;'> 35.50KB </span>","children":null,"spread":false}],"spread":true},{"title":"Form1.Designer.cs <span style='color:#111;'> 15.46KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"x86","children":[{"title":"Debug","children":[{"title":"USBDevice.pdb <span style='color:#111;'> 43.50KB </span>","children":null,"spread":false},{"title":"USBDevice.exe <span style='color:#111;'> 24.00KB </span>","children":null,"spread":false},{"title":"USBDevice.csproj.GenerateResource.Cache <span style='color:#111;'> 847B </span>","children":null,"spread":false},{"title":"USBDevice.csproj.FileListAbsolute.txt <span style='color:#111;'> 2.14KB </span>","children":null,"spread":false},{"title":"USBDevice.Properties.Resources.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"USBDevice.Form1.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.23KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferences.cache <span style='color:#111;'> 6.79KB </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}],"spread":true}],"spread":true}],"spread":true},{"title":"Form1.cs <span style='color:#111;'> 13.89KB </span>","children":null,"spread":false},{"title":"app.config <span style='color:#111;'> 117B </span>","children":null,"spread":false},{"title":"ItemObject.cs <span style='color:#111;'> 754B </span>","children":null,"spread":false},{"title":"Form1.resx <span style='color:#111;'> 6.22KB </span>","children":null,"spread":false},{"title":"USBDevice.csproj <span style='color:#111;'> 3.98KB </span>","children":null,"spread":false},{"title":"USBVisit.cs <span style='color:#111;'> 8.14KB </span>","children":null,"spread":false},{"title":"USBDev","children":[{"title":"WindowsAPI.cs <span style='color:#111;'> 17.41KB </span>","children":null,"spread":false},{"title":"SetupApi.cs <span style='color:#111;'> 1.25KB </span>","children":null,"spread":false},{"title":"UsbHidDevice.cs <span style='color:#111;'> 10.21KB </span>","children":null,"spread":false},{"title":"DeviceChangeNotifier.cs <span style='color:#111;'> 6.76KB </span>","children":null,"spread":false},{"title":"Hid.cs <span style='color:#111;'> 2.34KB </span>","children":null,"spread":false},{"title":"DeviceCommunication.cs <span style='color:#111;'> 8.49KB </span>","children":null,"spread":false},{"title":"SyncObjT.cs <span style='color:#111;'> 330B </span>","children":null,"spread":false},{"title":"HidpCaps.cs <span style='color:#111;'> 1.01KB </span>","children":null,"spread":false},{"title":"HiddAttributes.cs <span style='color:#111;'> 389B </span>","children":null,"spread":false},{"title":"CommandMessage.cs <span style='color:#111;'> 1.81KB </span>","children":null,"spread":false},{"title":"DeviceDiscovery.cs <span style='color:#111;'> 6.59KB </span>","children":null,"spread":false},{"title":"DeviceInformationStructure.cs <span style='color:#111;'> 1.91KB </span>","children":null,"spread":false},{"title":"DevBroadcastDeviceinterface1.cs <span style='color:#111;'> 645B </span>","children":null,"spread":false},{"title":"ConvertHelper.cs <span style='color:#111;'> 14.65KB </span>","children":null,"spread":false},{"title":"DevBroadcastDeviceinterface.cs <span style='color:#111;'> 454B </span>","children":null,"spread":false},{"title":"SpDeviceInterfaceData.cs <span style='color:#111;'> 395B </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}],"spread":false},{"title":"Properties","children":[{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"Settings.settings <span style='color:#111;'> 249B </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.Designer.cs <span style='color:#111;'> 2.79KB </span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":"USBDevice.v11.suo <span style='color:#111;'> 58.00KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

免责申明

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