Sie sind auf Seite 1von 10

VB Public Class frmUSB ' vendor and product IDs Private Const VendorID As Integer = &H1234 Private Const

ProductID As Integer = &H1234

'Replace with your device's 'product and vendor IDs

' read and write buffers Private Const BufferInSize As Integer = 1 'Size of the data buffer coming IN to the PC Private Const BufferOutSize As Integer = 1 'Size of the data buffer going OUT from the PC Dim BufferIn(BufferInSize) As Byte 'Received data will be stored he re - the first byte in the array is unused Dim BufferOut(BufferOutSize) As Byte 'Transmitted data is stored here - t he first item in the array must be 0 ' **************************************************************** ' when the form loads, connect to the HID controller - pass ' the form window handle so that you can receive notification ' events... '***************************************************************** Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.Even tArgs) Handles MyBase.Load ' do not remove! ConnectToHID(Me) End Sub '***************************************************************** ' disconnect from the HID controller... '***************************************************************** Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windo ws.Forms.FormClosedEventArgs) Handles Me.FormClosed DisconnectFromHID() End Sub '***************************************************************** ' a HID device has been plugged in... '***************************************************************** Public Sub OnPlugged(ByVal pHandle As Integer) If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = Pro ductID Then ' ** YOUR CODE HERE ** End If End Sub '***************************************************************** ' a HID device has been unplugged... '***************************************************************** Public Sub OnUnplugged(ByVal pHandle As Integer) If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = Pro ductID Then hidSetReadNotify(hidGetHandle(VendorID, ProductID), False) ' ** YOUR CODE HERE ** End If End Sub '***************************************************************** ' controller changed notification - called ' after ALL HID devices are plugged or unplugged

'***************************************************************** Public Sub OnChanged() ' get the handle of the device we are interested in, then set ' its read notify flag to true - this ensures you get a read ' notification message when there is some data to read... Dim pHandle As Integer pHandle = hidGetHandle(VendorID, ProductID) hidSetReadNotify(hidGetHandle(VendorID, ProductID), True) End Sub '***************************************************************** ' on read event... '***************************************************************** Public Sub OnRead(ByVal pHandle As Integer) ' read the data (don't forget, pass the whole array)... If hidRead(pHandle, BufferIn(0)) Then TextBox1.Text = Str(BufferIn(1)) ' ** YOUR CODE HERE ** ' first byte is the report ID, e.g. BufferIn(0) ' the other bytes are the data from the microcontroller... End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles Button1.Click BufferOut(1) = Val(TextBox2.Text) hidWriteEx(VendorID, ProductID, BufferOut(0)) End Sub End Class ******************************************************************************** ********************************** Imports System Imports System.Threading Imports System.Runtime.InteropServices Module HIDDLLInterface ' this is the interface to the HID controller DLL - you should not ' normally need to change anything in this file. ' ' WinProc() calls your main form 'event' procedures - these are currently ' set to.. ' ' MainForm.OnPlugged(ByVal pHandle as long) ' MainForm.OnUnplugged(ByVal pHandle as long) ' MainForm.OnChanged() ' MainForm.OnRead(ByVal pHandle as long) ' HID interface API declarations... Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Integer) As Boolean Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Bool ean Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As

Integer) As Integer Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Integer Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Inte ger, ByRef pData As Byte) As Boolean Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As In teger, ByRef pData As Byte) As Boolean Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID A s Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVend oID As Integer, ByVal pProductID As Integer) As Integer Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal p Handle As Integer) As Integer Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Integer) As Integer Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHa ndle As Integer) As Integer Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByV al pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (B yVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Intege r Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Inte ger Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputRepo rtLength" (ByVal pHandle As Integer) As Integer Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputRe portLength" (ByVal pHandle As Integer) As Integer Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pH andle As Integer, ByVal pValue As Boolean) Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyE nabled" (ByVal pHandle As Integer) As Boolean Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal p VendorID As Integer, ByVal pProductID As Integer) As Boolean ' windows API declarations - used to set up messaging... Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal Msg As Integer, By Val wParam As Integer, ByVal lParam As Integer) As Integer Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ (ByVal hwnd As Integer, ByVal nIndex A s Integer, ByVal dwNewLong As Integer) As Integer Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, ByVal msg As I nteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer Public Declare Function DelegateSetWindowLong Lib "USER32.DLL" Alias "SetWin dowLongA" _ (ByVal hwnd As Integer, ByVal attr As Integer, ByVal lval As SubClassProcDelegate) As Integer ' windows API Constants Public Const WM_APP As Integer = 32768 Public Const GWL_WNDPROC As Short = -4 ' HID message constants

Private Private Private Private Private ' local Private Private Private Private

Const Const Const Const Const

WM_HID_EVENT As Decimal = NOTIFY_PLUGGED As Short = NOTIFY_UNPLUGGED As Short NOTIFY_CHANGED As Short = NOTIFY_READ As Short = 4

WM_APP + 200 1 = 2 3

variables FPrevWinProc As Integer ' Handle to previous window procedure FWinHandle As Integer ' Handle to message window Ref_WinProc As New SubClassProcDelegate(AddressOf WinProc) HostForm As Object

' Set up a windows hook to receive notification ' messages from the HID controller DLL - then connect ' to the controller Public Function ConnectToHID(ByRef targetForm As Form) As Boolean Dim pHostWin As Integer = targetForm.Handle.ToInt32 FWinHandle = pHostWin pHostWin = hidConnect(FWinHandle) FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Ref_WinPro c) HostForm = targetForm End Function ' Unhook from the HID controller and disconnect... Public Function DisconnectFromHID() As Boolean DisconnectFromHID = hidDisconnect SetWindowLong(FWinHandle, GWL_WNDPROC, FPrevWinProc) End Function ' This is the procedure that intercepts the HID controller messages... Private Function WinProc(ByVal pHWnd As Integer, ByVal pMsg As Integer, ByVa l wParam As Integer, ByVal lParam As Integer) As Integer If pMsg = WM_HID_EVENT Then Select Case wParam ' HID device has been plugged message... Case Is = NOTIFY_PLUGGED HostForm.OnPlugged(lParam) ' HID device has been unplugged Case Is = NOTIFY_UNPLUGGED HostForm.OnUnplugged(lParam) ' controller has changed... Case Is = NOTIFY_CHANGED HostForm.OnChanged() ' read event... Case Is = NOTIFY_READ HostForm.OnRead(lParam) End Select End If ' next... WinProc = CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam)

End Function End Module ******************************************************************************** **************************** C# ---------------------------------------------------------------------------------------------------------using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; public class frmUSB { // vendor and product IDs //Replace with your device's private const int VendorID = 0x1234; //product and vendor IDs private const int ProductID = 0x1234; // read and write buffers //Size of the data buffer coming IN to the PC private const int BufferInSize = 1; //Size of the data buffer going OUT from the PC private const int BufferOutSize = 1; //Received data will be stored here - the first byte in the arra y is unused byte[] BufferIn = new byte[BufferInSize + 1]; //Transmitted data is stored here - the first item in the array must be 0 byte[] BufferOut = new byte[BufferOutSize + 1]; // **************************************************************** // when the form loads, connect to the HID controller - pass // the form window handle so that you can receive notification // events... //***************************************************************** private void Form1_Load(System.Object sender, System.EventArgs e) { // do not remove! ConnectToHID(this); } //***************************************************************** // disconnect from the HID controller... //***************************************************************** private void Form1_FormClosed(object sender, System.Windows.Forms.FormCl osedEventArgs e) { DisconnectFromHID(); } //***************************************************************** // a HID device has been plugged in... //***************************************************************** public void OnPlugged(int pHandle) { if (hidGetVendorID(pHandle) == VendorID & hidGetProductID(pHandl

e) == ProductID) { // ** YOUR CODE HERE ** } } //***************************************************************** // a HID device has been unplugged... //***************************************************************** public void OnUnplugged(int pHandle) { if (hidGetVendorID(pHandle) == VendorID & hidGetProductID(pHandl e) == ProductID) { hidSetReadNotify(hidGetHandle(VendorID, ProductID), fals e); // ** YOUR CODE HERE ** } } //***************************************************************** // controller changed notification - called // after ALL HID devices are plugged or unplugged //***************************************************************** public void OnChanged() { // get the handle of the device we are interested in, then set // its read notify flag to true - this ensures you get a read // notification message when there is some data to read... int pHandle = 0; pHandle = hidGetHandle(VendorID, ProductID); hidSetReadNotify(hidGetHandle(VendorID, ProductID), true); } //***************************************************************** // on read event... //***************************************************************** public void OnRead(int pHandle) { // read the data (don't forget, pass the whole array)... if (hidRead(pHandle, BufferIn[0])) { TextBox1.Text = Conversion.Str(BufferIn[1]); // ** YOUR CODE HERE ** // first byte is the report ID, e.g. BufferIn(0) // the other bytes are the data from the microcontroller ... } } private void Button1_Click(System.Object sender, System.EventArgs e) { BufferOut[1] = Conversion.Val(TextBox2.Text); hidWriteEx(VendorID, ProductID, BufferOut[0]); } public frmUSB() { FormClosed += Form1_FormClosed; Load += Form1_Load; }

} ------------------------------------------------------------------------------------------------using using using using using using using using Microsoft.VisualBasic; System; System.Collections; System.Collections.Generic; System.Data; System.Diagnostics; System.Threading; System.Runtime.InteropServices;

static class HIDDLLInterface { [DllImport("mcHID.dll", EntryPoint = "Connect", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] // this is the interface to the HID controller DLL - you should not // normally need to change anything in this file. // // WinProc() calls your main form 'event' procedures - these are current ly // set to.. // // MainForm.OnPlugged(ByVal pHandle as long) // MainForm.OnUnplugged(ByVal pHandle as long) // MainForm.OnChanged() // MainForm.OnRead(ByVal pHandle as long) // HID interface API declarations... public static extern bool hidConnect(int pHostWin); [DllImport("mcHID.dll", EntryPoint = "Disconnect", CharSet = CharSet.Ans i, SetLastError = true, ExactSpelling = true)] public static extern bool hidDisconnect(); [DllImport("mcHID.dll", EntryPoint = "GetItem", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetItem(int pIndex); [DllImport("mcHID.dll", EntryPoint = "GetItemCount", CharSet = CharSet.A nsi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetItemCount(); [DllImport("mcHID.dll", EntryPoint = "Read", CharSet = CharSet.Ansi, Set LastError = true, ExactSpelling = true)] public static extern bool hidRead(int pHandle, ref byte pData); [DllImport("mcHID.dll", EntryPoint = "Write", CharSet = CharSet.Ansi, Se tLastError = true, ExactSpelling = true)] public static extern bool hidWrite(int pHandle, ref byte pData); [DllImport("mcHID.dll", EntryPoint = "ReadEx", CharSet = CharSet.Ansi, S etLastError = true, ExactSpelling = true)] public static extern bool hidReadEx(int pVendorID, int pProductID, ref b yte pData); [DllImport("mcHID.dll", EntryPoint = "WriteEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern bool hidWriteEx(int pVendorID, int pProductID, ref byte pData); [DllImport("mcHID.dll", EntryPoint = "GetHandle", CharSet = CharSet.Ansi , SetLastError = true, ExactSpelling = true)] public static extern int hidGetHandle(int pVendoID, int pProductID);

[DllImport("mcHID.dll", EntryPoint = "GetVendorID", CharSet = CharSet.An si, SetLastError = true, ExactSpelling = true)] public static extern int hidGetVendorID(int pHandle); [DllImport("mcHID.dll", EntryPoint = "GetProductID", CharSet = CharSet.A nsi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetProductID(int pHandle); [DllImport("mcHID.dll", EntryPoint = "GetVersion", CharSet = CharSet.Ans i, SetLastError = true, ExactSpelling = true)] public static extern int hidGetVersion(int pHandle); [DllImport("mcHID.dll", EntryPoint = "GetVendorName", CharSet = CharSet. Ansi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetVendorName(int pHandle, string pText, int pLen); [DllImport("mcHID.dll", EntryPoint = "GetProductName", CharSet = CharSet .Ansi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetProductName(int pHandle, string pText, in t pLen); [DllImport("mcHID.dll", EntryPoint = "GetSerialNumber", CharSet = CharSe t.Ansi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetSerialNumber(int pHandle, string pText, i nt pLen); [DllImport("mcHID.dll", EntryPoint = "GetInputReportLength", CharSet = C harSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetInputReportLength(int pHandle); [DllImport("mcHID.dll", EntryPoint = "GetOutputReportLength", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern int hidGetOutputReportLength(int pHandle); [DllImport("mcHID.dll", EntryPoint = "SetReadNotify", CharSet = CharSet. Ansi, SetLastError = true, ExactSpelling = true)] public static extern void hidSetReadNotify(int pHandle, bool pValue); [DllImport("mcHID.dll", EntryPoint = "IsReadNotifyEnabled", CharSet = Ch arSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern bool hidIsReadNotifyEnabled(int pHandle); [DllImport("mcHID.dll", EntryPoint = "IsAvailable", CharSet = CharSet.An si, SetLastError = true, ExactSpelling = true)] public static extern bool hidIsAvailable(int pVendorID, int pProductID); [DllImport("user32", EntryPoint = "CallWindowProcA", CharSet = CharSet.A nsi, SetLastError = true, ExactSpelling = true)] // windows API declarations - used to set up messaging... public static extern int CallWindowProc(int lpPrevWndFunc, int hwnd, int Msg, int wParam, int lParam); [DllImport("user32", EntryPoint = "SetWindowLongA", CharSet = CharSet.An si, SetLastError = true, ExactSpelling = true)] public static extern int SetWindowLong(int hwnd, int nIndex, int dwNewLo ng); public delegate int SubClassProcDelegate(int hwnd, int msg, int wParam, int lParam); [DllImport("USER32.DLL", EntryPoint = "SetWindowLongA", CharSet = CharSe t.Ansi, SetLastError = true, ExactSpelling = true)] public static extern int DelegateSetWindowLong(int hwnd, int attr, SubCl assProcDelegate lval); // windows API Constants public const int WM_APP = 32768; public const short GWL_WNDPROC = -4;

// HID message constants private const decimal WM_HID_EVENT = private const short NOTIFY_PLUGGED = private const short NOTIFY_UNPLUGGED private const short NOTIFY_CHANGED = private const short NOTIFY_READ = 4;

WM_APP + 200; 1; = 2; 3;

// local variables // Handle to previous window procedure private static int FPrevWinProc; // Handle to message window private static int FWinHandle; private static SubClassProcDelegate Ref_WinProc = new SubClassProcDelega te(WinProc); private static object HostForm; // Set up a windows hook to receive notification // messages from the HID controller DLL - then connect // to the controller public static bool ConnectToHID(ref Form targetForm) { int pHostWin = targetForm.Handle.ToInt32; FWinHandle = pHostWin; pHostWin = hidConnect(FWinHandle); FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Re f_WinProc); HostForm = targetForm; } // Unhook from the HID controller and disconnect... public static bool DisconnectFromHID() { bool functionReturnValue = false; functionReturnValue = hidDisconnect(); SetWindowLong(FWinHandle, GWL_WNDPROC, FPrevWinProc); return functionReturnValue; } // This is the procedure that intercepts the HID controller messages... private static int WinProc(int pHWnd, int pMsg, int wParam, int lParam) { if (pMsg == WM_HID_EVENT) { switch (wParam) { // HID device has been plugged message... case // ERROR: Case labels with binary operator s are unsupported : Equality NOTIFY_PLUGGED: HostForm.OnPlugged(lParam); break; // HID device has been unplugged case // ERROR: Case labels with binary operator s are unsupported : Equality NOTIFY_UNPLUGGED: HostForm.OnUnplugged(lParam); break;

// controller has changed... case // ERROR: Case labels with binary operator s are unsupported : Equality NOTIFY_CHANGED: HostForm.OnChanged(); break; // read event... case // ERROR: Case labels with binary operator s are unsupported : Equality NOTIFY_READ: HostForm.OnRead(lParam); break; } } // next... return CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam) ; } }

Das könnte Ihnen auch gefallen