📄 frmmain.cs
字号:
this.fraDeviceIdentifiers.Controls.Add(this.lblProductID);
this.fraDeviceIdentifiers.Controls.Add(this.txtVendorID);
this.fraDeviceIdentifiers.Controls.Add(this.lblVendorID);
this.fraDeviceIdentifiers.Location = new System.Drawing.Point(13, 13);
this.fraDeviceIdentifiers.Name = "fraDeviceIdentifiers";
this.fraDeviceIdentifiers.Size = new System.Drawing.Size(174, 78);
this.fraDeviceIdentifiers.TabIndex = 10;
this.fraDeviceIdentifiers.TabStop = false;
this.fraDeviceIdentifiers.Text = "Device Identifiers";
//
// txtProductID
//
this.txtProductID.Location = new System.Drawing.Point(100, 46);
this.txtProductID.Name = "txtProductID";
this.txtProductID.Size = new System.Drawing.Size(60, 20);
this.txtProductID.TabIndex = 3;
this.txtProductID.Text = "1299";
this.txtProductID.TextChanged += new System.EventHandler(this.txtProductID_TextChanged);
//
// lblProductID
//
this.lblProductID.Location = new System.Drawing.Point(13, 46);
this.lblProductID.Name = "lblProductID";
this.lblProductID.Size = new System.Drawing.Size(94, 18);
this.lblProductID.TabIndex = 2;
this.lblProductID.Text = "Product ID (hex):";
//
// txtVendorID
//
this.txtVendorID.Location = new System.Drawing.Point(100, 20);
this.txtVendorID.Name = "txtVendorID";
this.txtVendorID.Size = new System.Drawing.Size(60, 20);
this.txtVendorID.TabIndex = 1;
this.txtVendorID.Text = "0925";
this.txtVendorID.TextChanged += new System.EventHandler(this.txtVendorID_TextChanged);
//
// lblVendorID
//
this.lblVendorID.Location = new System.Drawing.Point(13, 20);
this.lblVendorID.Name = "lblVendorID";
this.lblVendorID.Size = new System.Drawing.Size(94, 18);
this.lblVendorID.TabIndex = 0;
this.lblVendorID.Text = "Vendor ID (hex):";
//
// cmdFindDevice
//
this.cmdFindDevice.Location = new System.Drawing.Point(387, 39);
this.cmdFindDevice.Name = "cmdFindDevice";
this.cmdFindDevice.Size = new System.Drawing.Size(113, 33);
this.cmdFindDevice.TabIndex = 11;
this.cmdFindDevice.Text = "Find My Device";
this.cmdFindDevice.Click += new System.EventHandler(this.cmdFindDevice_Click);
//
// lblFeatureRepLength
//
this.lblFeatureRepLength.AutoSize = true;
this.lblFeatureRepLength.Location = new System.Drawing.Point(38, 88);
this.lblFeatureRepLength.Name = "lblFeatureRepLength";
this.lblFeatureRepLength.Size = new System.Drawing.Size(52, 13);
this.lblFeatureRepLength.TabIndex = 3;
this.lblFeatureRepLength.Text = "Length: 0";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(659, 452);
this.Controls.Add(this.cmdFindDevice);
this.Controls.Add(this.fraDeviceIdentifiers);
this.Controls.Add(this.fraInputReportBufferSize);
this.Controls.Add(this.fraReportTypes);
this.Controls.Add(this.fraSendAndReceive);
this.Controls.Add(this.fraBytesReceived);
this.Controls.Add(this.fraBytesToSend);
this.Controls.Add(this.lstResults);
this.Location = new System.Drawing.Point(21, 28);
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Jan\'s HID Tester";
this.Closed += new System.EventHandler(this.frmMain_Closed);
this.Load += new System.EventHandler(this.frmMain_Load);
this.fraSendAndReceive.ResumeLayout(false);
this.fraBytesReceived.ResumeLayout(false);
this.fraBytesToSend.ResumeLayout(false);
this.fraReportTypes.ResumeLayout(false);
this.fraReportTypes.PerformLayout();
this.fraInputReportBufferSize.ResumeLayout(false);
this.fraInputReportBufferSize.PerformLayout();
this.fraDeviceIdentifiers.ResumeLayout(false);
this.fraDeviceIdentifiers.PerformLayout();
this.ResumeLayout(false);
}
#endregion
// Project: usbhidio_vbdotnet
// Version: 2.0
// Date: 6/24/04
//
// Purpose:
// Demonstrates USB communications with a HID-class device
// Description:
// Finds an attached device that matches the vendor and product IDs in the form's
// text boxes.
// Retrieves the device's capabilities.
// Sends and requests HID reports.
// Uses RegisterDeviceNotification() and WM_DEVICE_CHANGE messages
// to detect when a device is attached or removed.
// RegisterDeviceNotification doesn't work under Windows 98 (not sure why).
// A list box displays the data sent and received,
// along with error and status messages.
// Combo boxes select data to send, and 1-time or timed, periodic transfers.
// You can change the size of the host's Input report buffer and request to use control
// transfers only to exchange Input and Output reports.
// To view additional debugging messages, in the Visual Studio development environment,
// select the Debug build (Build > Configuration Manager > Active Solution Configuration)
// and view the Output window (View > Other Windows > Output)
// The application uses a Delegate and the BeginInvoke and EndInvoke methods to read
// from the device asynchronously.
// If you want to only receive data or only send data, comment out the unwanted code
// (the "Success = " line and the "If Success" block that follows it).
// This project includes the following modules:
// frmMain.vb - routines specific to the form.
// Hid.vb - routines specific to HID communications.
// DeviceManagement.vb - routines for obtaining a handle to a device from its GUID
// and receiving device notifications. This routines are not specific to HIDs.
// Debugging.vb - contains a routine for displaying API error messages.
// HidDeclarations.vb - Declarations for API functions used by Hid.vb.
// FileIODeclarations.vb - Declarations for file-related API functions.
// DeviceManagementDeclarations.vb - Declarations for API functions used by DeviceManagement.vb.
// DebuggingDeclarations.vb - Declarations for API functions used by Debugging.vb.
// Companion device firmware for several device CPUs is available from www.Lvr.com/hidpage.htm.
// You can use any generic HID (not a system mouse or keyboard) that sends and receives reports.
// For more information about HIDs and USB, and additional example device firmware to use
// with this application, visit Lakeview Research at http:// www.Lvr.com .
// Send comments, bug reports, etc. to jan@Lvr.com .
// This application has been tested under Windows 98SE, Windows 2000, and Windows XP.
IntPtr _DeviceNotificationHandle;
bool _ExclusiveAccess; //GRV
int _HIDHandle;
string HIDUsage; //GRV
bool _MyDeviceDetected;
string _MyDevicePathName;
Hid _MyHID = new Hid();
int _ReadHandle;
int _WriteHandle; //GRV
internal Form frmMy; //GRV
// Used only in viewing results of API calls in debug.write statements:
Debugging _MyDebugging = new Debugging();
DeviceManagement _MyDeviceManagement = new DeviceManagement();
// Define a class of delegates that point to the Hid.DeviceReport.Read function.
// The delegate has the same parameters as Hid.DeviceReport.Read.
// Used for asynchronous reads from the device.
private delegate void ReadInputReportDelegate (
int readHandle,
int hidHandle,
int writeHandle,
ref bool myDeviceDetected,
ref byte[] readBuffer,
ref bool success);
//This delegate has the same parameters as AccessForm.
//Used in accessing the application's form from a different thread.
private delegate void MarshalToForm(string action, string textToAdd); //GRV
internal void OnDeviceChange (Message m)
{
// Purpose : Called when a WM_DEVICECHANGE message has arrived,
// : indicating that a device has been attached or removed.
// Accepts : m - a message with information about the device
Debug.WriteLine("WM_DEVICECHANGE");
try
{
if (m.WParam.ToInt32() == DeviceManagementApiDeclarations.DBT_DEVICEARRIVAL)
{
// If WParam contains DBT_DEVICEARRIVAL, a device has been attached.
Debug.WriteLine("A device has been attached.");
// Find out if it's the device we're communicating with.
if (_MyDeviceManagement.DeviceNameMatch(m, _MyDevicePathName))
{
Debug.WriteLine("My device attached");
lstResults.Items.Add("My device attached.");
}
}
else if (m.WParam.ToInt32() == DeviceManagementApiDeclarations.DBT_DEVICEREMOVECOMPLETE)
{
// If WParam contains DBT_DEVICEREMOVAL, a device has been removed.
Debug.WriteLine("A device has been removed.");
// Find out if it's the device we're communicating with.
if (_MyDeviceManagement.DeviceNameMatch(m, _MyDevicePathName))
{
Debug.WriteLine("My device removed");
lstResults.Items.Add("My device removed.");
// Set MyDeviceDetected False so on the next data-transfer attempt,
// FindTheHid() will be called to look for the device
// and get a new handle.
this._MyDeviceDetected = false;
}
}
ScrollToBottomOfListBox();
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private bool FindTheHid()
{
// Purpose : Uses a series of API calls to locate a HID-class device
// ; by its Vendor ID and Product ID.
// Returns : True if the device is detected, False if not detected.
bool DeviceFound = false;
string[] DevicePathName = new string[128];
string GUIDString;
System.Guid HidGuid;
bool LastDevice; //GRV
int MemberIndex = 0;
short MyProductID = 0;
short MyVendorID = 0;
int Result = 0;
FileIOApiDeclarations.SECURITY_ATTRIBUTES Security = new HID_Test_Application.FileIOApiDeclarations.SECURITY_ATTRIBUTES();
bool Success = false;
try {
HidGuid = Guid.Empty;
LastDevice = false; //GRV
_MyDeviceDetected = false;
// Values for the SECURITY_ATTRIBUTES structure:
Security.lpSecurityDescriptor = 0;
Security.bInheritHandle = System.Convert.ToInt32(true);
Security.nLength = Marshal.SizeOf(Security);
// Get the device's Vendor ID and Product ID from the form's text boxes.
GetVendorAndProductIDsFromTextBoxes(ref MyVendorID, ref MyProductID);
/*
API function: 'HidD_GetHidGuid
Purpose: Retrieves the interface class GUID for the HID class.
Accepts: 'A System.Guid object for storing the GUID.
*/
HidApiDeclarations.HidD_GetHidGuid(ref HidGuid);
Debug.WriteLine(_MyDebugging.ResultOfAPICall("GetHidGuid"));
// Display the GUID.
GUIDString = HidGuid.ToString();
Debug.WriteLine(" GUID for system HIDs: " + GUIDString);
// Fill an array with the device path names of all attached HIDs.
DeviceFound = _MyDeviceManagement.FindDeviceFromGuid(HidGuid, ref DevicePathName);
// If there is at least one HID, attempt to read the Vendor ID and Product ID
// of each device until there is a match or all devices have been examined.
if (DeviceFound) {
MemberIndex = 0;
do {
// ***
// API function:
// CreateFile
// Purpose:
// Retrieves a handle to a device.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -