c#_HidUsb设备通信

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<string> slist = new List<string>();
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);
//连接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(); //释放所有资源

 

资源包:https://download.csdn.net/download/u010465417/10879988

赋所有源代码,开发工具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();
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设备信息为:\\?\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 = 5; CommandMessage cmdMsg = new CommandMessage(bt, data); Boolean sbool = Device.SendMessage(cmdMsg); //发送数据 Device.Dispose(); //释放所有资源
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值