資源簡介
賦所有源代碼,開發工具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();

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Runtime.InteropServices;
using?USBHIDDevice;
namespace?USBDevice
{
????public?partial?class?Form1?:?Form
????{
????????string[]?sArr?=?new?string[]?{?“15“?“14“?“13“?“12“?“11“?“10“?“9“?“8“?“7“?“6“?“5“?“4“?“3“?“2“?“1“?};
????????Boolean?isCard?=?true;
????????string?myDataStr?=?““;
????????string?devicePath?=?““;
????????Timer?timer?=?null;
????????Timer?timer1?=?null;
????????Boolean?connBool?=?false;
????????UsbHidDevice?Device;
????????List?txInfoList?=?new?List();
????????public?Form1()
????????{
????????????InitializeComponent();
????????????Initial();
????????????????????????
????????????this.timer?=?new?Timer();
????????????this.timer.Interval?=?500;
????????????this.timer.Tick?+=?new?EventHandler(timer_Tick);
????????????this.timer.Enabled?=?true;
????????????this.timer1?=?new?Timer();
????????????this.timer1.Interval?=?500;
????????????this.timer1.Tick?+=?new?EventHandler(timer1_Tick);
????????????this.timer1.Enabled?=?true;
????????????string?vid?=?“vid_0e2c“;
????????????string?pid?=?“pid_0112“;
????????????vid?=?vid.Replace(“vid_“?““);
????????????pid?=?pid.Replace(“pid_“?““);
????????????int?vendorID?=?ConvertHelper.ToInt(ConvertHelper.Hex2Ten(vid));
????????????int?productID?=?ConvertHelper.ToInt(ConvertHelper.Hex2Ten(pid));
????????????Device?=?new?UsbHidDevice(vendorID?productID);
????????????Device.devicePath?=?this.devicePath;
????????????????????????
????????????Device.OnConnected?+=?DeviceOnConnected;
????????????Device.OnDisConnected?+=?DeviceOnDisConnected;
????????????Device.DataReceived?+=?DeviceDataReceived;
????????????Button1.Click?+=?new?EventHandler(Button1_Click);
????????????button2.Click?+=?new?EventHandler(button2_Click);
????????????bttnWriteCard.Click?+=?new?EventHandler(bttnWriteCard_Click);
????????????button5.Click?+=?new?EventHandler(button5_Click);
????????????textBox4.TextChanged?+=?new?EventHandler(textBox4_TextChanged);?//十進制
????????????textBox3.TextChanged?+=?new?EventHandler(textBox3_TextChanged);?//十六進制
????????????dataGridView1.DataBindingComplete?+=?new?DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
????????????tabControl1.SelectedIndexChanged?+=?tabControl1_SelectedIndexChanged;
????????????this.FormClosed?+=?new?FormClosedEventHandler(Form1_FormClosed);
????????????connBool?=?Device.Connect();
????????????ReadCard();
????????}
????????void?Form1_FormClosed(object?sender?FormClosedEventArgs?e)
????????{
????????????if?(this.Device?!=?null)
????????????{
????????????????this.Device.Dispose();
????????????}
????????}
????????void?tabControl1_SelectedIndexChanged(object?sender?EventArgs?e)
????????{
????????????int?n?=?tabControl1.SelectedInde
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????117??2015-11-19?12:50??USBDevice\USBDevice\app.config
?????文件??????23040??2015-11-22?20:46??USBDevice\USBDevice\bin\Debug\USBDevice.exe
?????文件????????117??2015-11-19?12:50??USBDevice\USBDevice\bin\Debug\USBDevice.exe.config
?????文件??????42496??2015-11-22?20:46??USBDevice\USBDevice\bin\Debug\USBDevice.pdb
?????文件??????11608??2015-11-22?21:05??USBDevice\USBDevice\bin\Debug\USBDevice.vshost.exe
?????文件????????117??2015-11-19?12:50??USBDevice\USBDevice\bin\Debug\USBDevice.vshost.exe.config
?????文件????????490??2012-06-06?02:06??USBDevice\USBDevice\bin\Debug\USBDevice.vshost.exe.manifest
?????文件??????35840??2015-11-22?20:46??USBDevice\USBDevice\bin\Debug\USBHIDDevice.dll
?????文件??????81408??2015-11-22?20:46??USBDevice\USBDevice\bin\Debug\USBHIDDevice.pdb
?????文件??????58481??2015-11-22?01:27??USBDevice\USBDevice\bin\Debug.rar
?????文件??????20992??2015-11-13?16:16??USBDevice\USBDevice\dll\hid.dll
?????文件??????13471??2015-11-22?01:24??USBDevice\USBDevice\Form1.cs
?????文件??????14530??2015-11-21?17:33??USBDevice\USBDevice\Form1.Designer.cs
?????文件???????6369??2015-11-21?17:33??USBDevice\USBDevice\Form1.resx
?????文件????????754??2015-11-13?15:10??USBDevice\USBDevice\Itemob
?????文件???????6518??2015-11-21?17:16??USBDevice\USBDevice\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6379??2015-11-22?21:04??USBDevice\USBDevice\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????4608??2015-11-19?12:51??USBDevice\USBDevice\obj\x86\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件???????2393??2015-11-22?21:05??USBDevice\USBDevice\obj\x86\Debug\USBDevice.csproj.FileListAbsolute.txt
?????文件????????847??2015-11-22?20:46??USBDevice\USBDevice\obj\x86\Debug\USBDevice.csproj.GenerateResource.Cache
?????文件???????6053??2015-11-22?20:46??USBDevice\USBDevice\obj\x86\Debug\USBDevice.csprojResolveAssemblyReference.cache
?????文件??????23040??2015-11-22?20:46??USBDevice\USBDevice\obj\x86\Debug\USBDevice.exe
?????文件????????180??2015-11-22?20:46??USBDevice\USBDevice\obj\x86\Debug\USBDevice.Form1.resources
?????文件??????42496??2015-11-22?20:46??USBDevice\USBDevice\obj\x86\Debug\USBDevice.pdb
?????文件????????180??2015-11-22?20:46??USBDevice\USBDevice\obj\x86\Debug\USBDevice.Properties.Resources.resources
?????文件????????490??2015-11-13?14:27??USBDevice\USBDevice\Program.cs
?????文件???????1368??2015-11-13?14:27??USBDevice\USBDevice\Properties\AssemblyInfo.cs
?????文件???????2858??2015-11-19?12:50??USBDevice\USBDevice\Properties\Resources.Designer.cs
?????文件???????5612??2015-11-13?14:27??USBDevice\USBDevice\Properties\Resources.resx
?????文件???????1109??2015-11-19?12:50??USBDevice\USBDevice\Properties\Settings.Designer.cs
............此處省略76個文件信息
評論
共有 條評論