⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hid.cs

📁 usbio using microsoft visual c# example
💻 CS
📖 第 1 页 / 共 3 页
字号:
							Debug.WriteLine("");
							success = false;
							myDeviceDetected = false;
							break;

						default:
							// Cancel the operation on other error.
							CancelTransfer(readHandle, hidHandle);
							Debug.WriteLine("");
							Debug.WriteLine("Readfile undefined error");
							success = false;
							myDeviceDetected = false;
							break;
					}
					
					success = (Result == 0) ? true : false;
					
				} catch (Exception ex) {
					HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
				}
				
			}
		}
		
		
		internal class InputReportViaControlTransfer : DeviceReport
		{
			
			protected override void ProtectedRead (int readHandle, int hidHandle, ref bool myDeviceDetected, ref byte[] inputReportBuffer, ref bool success)
			{
				
				// Purpose    : reads an Input report from the device using a control transfer.
				
				// Accepts    : readHandle - the handle for reading from the device.
				//              hidHandle - the handle for other device communications.
				//              myDeviceDetected - tells whether the device is currently attached.
				//              readBuffer - contains the requested report.
				//              success - read success
				
				try {
					// ***
					// API function: HidD_GetInputReport
					// Purpose: Attempts to read an Input report from the device using a control transfer.
					// Supported under Windows XP and later only.
					// Requires:
					// A handle to a HID
					// A pointer to a buffer containing the report ID and report
					// The size of the buffer.
					// Returns: true on success, false on failure.
					// ***
					
					success = HidApiDeclarations.HidD_GetInputReport(hidHandle, ref inputReportBuffer[0], inputReportBuffer.Length);
					
					Debug.WriteLine(MyDebugging.ResultOfAPICall("ReadFile"));
					Debug.WriteLine("");
					
				} catch (Exception ex) {
					HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
				}
				
			}
			
		}
		
		
		internal abstract class HostReport
		{
			
			// For reports the host sends to the device.
			
			// Each report class defines a ProtectedWrite method for writing a type of report.
			
			protected abstract bool ProtectedWrite(int deviceHandle, byte[] reportBuffer);
			
			
			internal bool Write(byte[] reportBuffer, int deviceHandle)
			{
				
				bool Success = false;
				
				// Purpose    : Calls the overridden ProtectedWrite routine.
				//            : This method enables other classes to override ProtectedWrite
				//            : while limiting access as Friend.
				//            : (Directly declaring Write as Friend MustOverride causes the
				//            : compiler(warning) "Other languages may permit Friend
				//            : Overridable members to be overridden.")
				// Accepts    : reportBuffer - contains the report ID and report data.
				//            : deviceHandle - handle to the device.             '
				// Returns    : True on success. False on failure.
				
				try	{
					Success = ProtectedWrite(deviceHandle, reportBuffer);
				} catch (Exception ex) {
					HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
				}

        return Success;
			}
		}
		
		
		internal class OutFeatureReport : HostReport
		{
			
			// For Feature reports the host sends to the device.
			
			protected override bool ProtectedWrite (int hidHandle, byte[] outFeatureReportBuffer)
			{
				
				//  Purpose    : writes a Feature report to the device.
				//  Accepts    : hidHandle - a handle to the device.
				//               featureReportBuffer - contains the report ID and report to send.
				//  Returns    : True on success. False on failure.
				
				bool Success = false;
				
				try {
					// ***
					// API function: HidD_SetFeature
					// Purpose: Attempts to send a Feature report to the device.
					// Accepts:
					// A handle to a HID
					// A pointer to a buffer containing the report ID and report
					// The size of the buffer.
					// Returns: true on success, false on failure.
					// ***
					
					Success = HidApiDeclarations.HidD_SetFeature(hidHandle, ref outFeatureReportBuffer[0], outFeatureReportBuffer.Length);
					
					Debug.WriteLine(MyDebugging.ResultOfAPICall("Hidd_SetFeature"));
					Debug.WriteLine("");
					Debug.WriteLine(" FeatureReportByteLength = " + outFeatureReportBuffer.Length);
					Debug.WriteLine(" Report ID: " + outFeatureReportBuffer[0]);
					Debug.WriteLine(" Report Data:");
					
					for (int i = 0; i < outFeatureReportBuffer.Length; i++) {
						Debug.WriteLine(" " + outFeatureReportBuffer[i].ToString("x"));
					}
					
				} catch (Exception ex) {
					HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
				}
				
        return Success;
			}
			
		}
		
		
		internal class OutputReport : HostReport
		{
			
			// For Output reports the host sends to the device.
			// Uses interrupt or control transfers depending on the device and OS.
			
			protected override bool ProtectedWrite (int hidHandle, byte[] outputReportBuffer)
			{
				
				// Purpose    : writes an Output report to the device.
				// Accepts    : HIDHandle - a handle to the device.
				//              OutputReportBuffer - contains the report ID and report to send.
				// Returns    : True on success. False on failure.
				
				int NumberOfBytesWritten = 0;
				int Result;
				bool Success = false;
				
				try
				{
					// The host will use an interrupt transfer if the the HID has an interrupt OUT
					// endpoint (requires USB 1.1 or later) AND the OS is NOT Windows 98 Gold (original version).
					// Otherwise the the host will use a control transfer.
					// The application doesn't have to know or care which type of transfer is used.
					
					// ***
					// API function: WriteFile
					// Purpose: writes an Output report to the device.
					// Accepts:
					// A handle returned by CreateFile
					// The output report byte length returned by HidP_GetCaps.
					// An integer to hold the number of bytes written.
					// Returns: True on success, False on failure.
					// ***
					
					Result = FileIOApiDeclarations.WriteFile(hidHandle, ref outputReportBuffer[0], outputReportBuffer.Length, ref NumberOfBytesWritten, 0);
					
					Debug.WriteLine(MyDebugging.ResultOfAPICall("WriteFile"));
					Debug.WriteLine("");
					Debug.WriteLine(" OutputReportByteLength = " + outputReportBuffer.Length.ToString());
					Debug.WriteLine(" NumberOfBytesWritten = " + NumberOfBytesWritten);
					Debug.WriteLine(" Report ID: " + outputReportBuffer[0]);
					Debug.WriteLine(" Report Data:");
					
					for (int i = 0; i < outputReportBuffer.Length; i++) {
						Debug.WriteLine("   " + outputReportBuffer[i].ToString("x"));
					}
					
					// Return True on success, False on failure.
					Success = (Result==0) ? false : true;
					
				} catch (Exception ex) {
					HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
				}

        return Success;
			}
			
		}
		
		
		internal class OutputReportViaControlTransfer : HostReport
		{
			
			protected override bool ProtectedWrite (int hidHandle, byte[] outputReportBuffer)
			{
				
				// Purpose    : writes an Output report to the device using a control transfer.
				// Accepts    : hidHandle - a handle to the device.
				//              outputReportBuffer - contains the report ID and report to send.
				// Returns    : True on success. False on failure.
				
				bool Success = false;
				
				try {
					// ***
					// API function: HidD_SetOutputReport
          // Purpose:
					// Attempts to send an Output report to the device using a control transfer.
					// Requires Windows XP or later.
					// Accepts:
					// A handle to a HID
					// A pointer to a buffer containing the report ID and report
					// The size of the buffer.
					// Returns: true on success, false on failure.
					// ***
					
					Success = HidApiDeclarations.HidD_SetOutputReport(hidHandle, ref outputReportBuffer[0], outputReportBuffer.Length);
					
					Debug.WriteLine(MyDebugging.ResultOfAPICall("Hidd_SetFeature"));
					Debug.WriteLine("");
					Debug.WriteLine(" OutputReportByteLength = " + outputReportBuffer.Length);
					Debug.WriteLine(" Report ID: " + outputReportBuffer[0]);
					Debug.WriteLine(" Report Data:");
					
					for (int i = 0; i < outputReportBuffer.Length; i++) {
						Debug.WriteLine(" " + outputReportBuffer[i].ToString("x"));
					}
					
				} catch (Exception ex) {
					HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
				}
        
        return Success;
			}
			
		}
		
		
		internal bool FlushQueue (int hidHandle) {
			// Purpose    : Remove any Input reports waiting in the buffer.
			// Accepts    : hidHandle - a handle to a device.
			// Returns    : True on success, False on failure.
			
			bool Result = false;
			
			try {
				// ***
				// API function: HidD_FlushQueue
				// Purpose: Removes any Input reports waiting in the buffer.
				// Accepts: a handle to the device.
				// Returns: True on success, False on failure.
				// ***
				
				Result = HidApiDeclarations.HidD_FlushQueue(hidHandle);
				
				Debug.WriteLine(MyDebugging.ResultOfAPICall("HidD_FlushQueue, ReadHandle"));
				Debug.WriteLine("Result = " + Result.ToString());
				
			} catch (Exception ex) {
				HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
      
      return Result;
		}
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -