////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Retrieves Input report data and status information. /// This routine is called automatically when myInputReport.Read /// returns. Calls several marshaling routines to access the main form. /// </summary> /// /// <param name="ar"> an object containing status information about /// the asynchronous operation. </param> private void GetInputReportData(IAsyncResult ar) { String byteValue = null; Int32 count = 0; Byte[] inputReportBuffer = null; Boolean success = false; try { // Define a delegate using the IAsyncResult object. ReadInputReportDelegate deleg = ((ReadInputReportDelegate)(ar.AsyncState)); // Get the IAsyncResult object and the values of other paramaters that the // BeginInvoke method passed ByRef. deleg.EndInvoke(ref myDeviceDetected, ref inputReportBuffer, ref success, ar); // Display the received report data in the form's list box. if ((ar.IsCompleted && success)) { blUSBSendCmdResult = true; intUSBCommandReceiveCode = inputReportBuffer[2] * 256 + inputReportBuffer[3]; } blUSBSendCmdFinish = true; } catch (Exception ex) { throw; } }
/// <summary> /// Retrieves Input report data and status information. /// This routine is called automatically when myInputReport.Read /// returns. Calls several marshaling routines to access the main form. /// </summary> /// /// <param name="ar"> an object containing status information about the asynchronous operation. </param> private void GetInputReportData(IAsyncResult ar) { StringBuilder byteValue = null; int count = 0; byte[] inputReportBuffer = null; bool success = false; try { // Define a delegate using the IAsyncResult object. ReadInputReportDelegate deleg = ((ReadInputReportDelegate)(ar.AsyncState)); // Get the IAsyncResult object and the values of other paramaters that the // BeginInvoke method passed ByRef. deleg.EndInvoke(ref myDeviceDetected, ref inputReportBuffer, ref success, ar); // Display the received report data in the form's list box. if ((ar.IsCompleted && success)) { ; /* * byteValue = new StringBuilder(); * byteValue.AppendFormat("GetInputReportData(): An Input report has been read asyncronously via Interrupt Transfer. Input Report ID: {0:X02}\r\n Input Report Data: ", inputReportBuffer[0]); * * for (count = 0; count <= inputReportBuffer.Length - 1; count++) * { * // Display bytes as 2-character Hex strings. * byteValue.AppendFormat("{0:X02} ", inputReportBuffer[count]); * } * Tracer.Trace(byteValue.ToString()); */ } else { Tracer.Error("GetInputReportData(): The attempt to read an Input report has failed."); } } catch (Exception ex) { Tracer.Error(ex); throw; } }
/// <summary> /// Sends an Output report, then may retrieve an Input report. /// </summary> private byte[] ExchangeInputAndOutputReports(byte[] outputReportBuffer, bool doOutputReport, bool doInputReport, EventHandler<AsyncInputReportArgs> readCompleteHandler) { StringBuilder byteValue = null; int count = 0; byte[] inputReportBuffer = null; bool success = false; try { success = false; // Don't attempt to exchange reports if valid handles aren't available // (as for a mouse or keyboard under Windows 2000/XP.) if (!readHandle.IsInvalid && !writeHandle.IsInvalid) { // Don't attempt to send an Output report if the HID has no Output report. if (doOutputReport && MyHid.Capabilities.OutputReportByteLength > 0) { // Write a report. if ((UseControlTransfersOnly) == true) { // Use a control transfer to send the report, // even if the HID has an interrupt OUT endpoint. Hid.OutputReportViaControlTransfer myOutputReport = new Hid.OutputReportViaControlTransfer(); success = myOutputReport.Write(outputReportBuffer, writeHandle); } else { // Use WriteFile to send the report. // If the HID has an interrupt OUT endpoint, WriteFile uses an // interrupt transfer to send the report. // If not, WriteFile uses a control transfer. Hid.OutputReportViaInterruptTransfer myOutputReport = new Hid.OutputReportViaInterruptTransfer(); success = myOutputReport.Write(outputReportBuffer, writeHandle); } if (success) { byteValue = new StringBuilder(); byteValue.AppendFormat("An Output report has been written. Output Report ID: {0:X02}\r\n Output Report Data: ", outputReportBuffer[0]); for (count = 0; count <= outputReportBuffer.Length - 1; count++) { // Display bytes as 2-character hex strings. byteValue.AppendFormat("{0:X02} ", outputReportBuffer[count]); } Tracer.Trace(byteValue.ToString()); } else { Tracer.Error("The attempt to write an Output report has failed."); } } else { Tracer.Trace("No attempt to send an Output report was made."); if (doOutputReport) { Tracer.Error("The HID doesn't have an Output report, but it was requested."); } } // Read an Input report. // Don't attempt to send an Input report if the HID has no Input report. // (The HID spec requires all HIDs to have an interrupt IN endpoint, // which suggests that all HIDs must support Input reports.) if (doInputReport && MyHid.Capabilities.InputReportByteLength > 0) { success = false; // Set the size of the Input report buffer. inputReportBuffer = new byte[MyHid.Capabilities.InputReportByteLength]; if (UseControlTransfersOnly || readCompleteHandler == null) { // Read a report using a control transfer. Hid.InputReportViaControlTransfer myInputReport = new Hid.InputReportViaControlTransfer(); // Read the report. myInputReport.Read(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success); if (success) { byteValue = new StringBuilder(); byteValue.AppendFormat("ExchangeInputAndOutputReports(): An Input report has been read via ControlTransfer. Input Report ID: {0:X02}\r\n Input Report Data: ", inputReportBuffer[0]); for (count = 0; count <= inputReportBuffer.Length - 1; count++) { // Display bytes as 2-character Hex strings. byteValue.AppendFormat("{0:X02} ", inputReportBuffer[count]); } Tracer.Trace(byteValue.ToString()); } else { Tracer.Error("ExchangeInputAndOutputReports(): The attempt to read an Input report has failed."); } } else { // Read a report using interrupt transfers. // To enable reading a report without blocking the main thread, this // application uses an asynchronous delegate. Tracer.Trace("IP: Arranging asyncronous read via Interrupt Transfer"); IAsyncResult ar = null; Hid.InputReportViaInterruptTransfer myInputReport = new Hid.InputReportViaInterruptTransfer(); if (readCompleteHandler != null) { myInputReport.HasReadData += readCompleteHandler; } // Define a delegate for the Read method of myInputReport. ReadInputReportDelegate MyReadInputReportDelegate = new ReadInputReportDelegate(myInputReport.Read); // The BeginInvoke method calls myInputReport.Read to attempt to read a report. // The method has the same parameters as the Read function, // plus two additional parameters: // GetInputReportData is the callback procedure that executes when the Read function returns. // MyReadInputReportDelegate is the asynchronous delegate object. // The last parameter can optionally be an object passed to the callback. ar = MyReadInputReportDelegate.BeginInvoke(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success, new AsyncCallback(GetInputReportData), MyReadInputReportDelegate); } } else { Tracer.Trace("No attempt to read an Input report was made."); if (doInputReport) { Tracer.Error("The HID doesn't have an Input report, but it was requested."); } } } else { Tracer.Trace("Invalid handle. The device is probably a system mouse or keyboard."); Tracer.Trace("No attempt to write an Output report or read an Input report was made."); } return inputReportBuffer; // may still be null } catch (Exception ex) { Tracer.Error(ex); throw; } }
/// <summary> /// Sends an Output report, then may retrieve an Input report. /// </summary> private byte[] ExchangeInputAndOutputReports(byte[] outputReportBuffer, bool doOutputReport, bool doInputReport, int expectedInputReportID, EventHandler <AsyncInputReportArgs> readCompleteHandler) { StringBuilder byteValue = null; int count = 0; byte[] inputReportBuffer = null; bool success = false; try { success = false; // Don't attempt to exchange reports if valid handles aren't available // (as for a mouse or keyboard under Windows 2000/XP.) if (!readHandle.IsInvalid && !writeHandle.IsInvalid) { // Don't attempt to send an Output report if the HID has no Output report. if (doOutputReport && MyHid.Capabilities.OutputReportByteLength > 0) { // Write a report. if ((UseControlTransfersOnly) == true) { // Use a control transfer to send the report, // even if the HID has an interrupt OUT endpoint. Hid.OutputReportViaControlTransfer myOutputReport = new Hid.OutputReportViaControlTransfer(); success = myOutputReport.Write(outputReportBuffer, writeHandle); } else { // Use WriteFile to send the report. // If the HID has an interrupt OUT endpoint, WriteFile uses an // interrupt transfer to send the report. // If not, WriteFile uses a control transfer. Hid.OutputReportViaInterruptTransfer myOutputReport = new Hid.OutputReportViaInterruptTransfer(); success = myOutputReport.Write(outputReportBuffer, writeHandle); } if (success) { ; /* * byteValue = new StringBuilder(); * byteValue.AppendFormat("An Output report has been written. Output Report ID: {0:X02}\r\n Output Report Data: ", outputReportBuffer[0]); * * for (count = 0; count <= outputReportBuffer.Length - 1; count++) * { * // Display bytes as 2-character hex strings. * byteValue.AppendFormat("{0:X02} ", outputReportBuffer[count]); * } * Tracer.Trace(byteValue.ToString()); */ } else { Tracer.Error("The attempt to write an Output report has failed."); } } else { //Tracer.Trace("No attempt to send an Output report was made."); if (doOutputReport) { Tracer.Error("The HID doesn't have an Output report, but it was requested."); } } // Read an Input report. // Don't attempt to send an Input report if the HID has no Input report. // (The HID spec requires all HIDs to have an interrupt IN endpoint, // which suggests that all HIDs must support Input reports.) if (doInputReport && MyHid.Capabilities.InputReportByteLength > 0) { success = false; // Set the size of the Input report buffer. inputReportBuffer = new byte[MyHid.Capabilities.InputReportByteLength]; if (UseControlTransfersOnly || readCompleteHandler == null) { // Read a report using a control transfer. Hid.InputReportViaControlTransfer myInputReport = new Hid.InputReportViaControlTransfer(); // Read the report via HidD_GetInputReport http://www.osronline.com/DDKx/intinput/hidfunc_3hgy.htm // - "If the top-level collection includes report IDs, the caller must set the first byte of the buffer to a nonzero report ID; otherwise the caller must set the first byte to zero." inputReportBuffer[0] = (byte)expectedInputReportID; myInputReport.Read(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success); if (success) { ; /* * byteValue = new StringBuilder(); * byteValue.AppendFormat("ExchangeInputAndOutputReports(): An Input report has been read via ControlTransfer. Input Report ID: {0:X02}\r\n Input Report Data: ", inputReportBuffer[0]); * * for (count = 0; count <= inputReportBuffer.Length - 1; count++) * { * // Display bytes as 2-character Hex strings. * byteValue.AppendFormat("{0:X02} ", inputReportBuffer[count]); * } * Tracer.Trace(byteValue.ToString()); */ } else { Tracer.Error("ExchangeInputAndOutputReports(): The attempt to read an Input report has failed."); } } else { // Read a report using interrupt transfers. // To enable reading a report without blocking the main thread, this // application uses an asynchronous delegate. //Tracer.Trace("IP: Arranging asyncronous read via Interrupt Transfer"); IAsyncResult ar = null; Hid.InputReportViaInterruptTransfer myInputReport = new Hid.InputReportViaInterruptTransfer(); if (readCompleteHandler != null) { myInputReport.HasReadData += readCompleteHandler; } // Define a delegate for the Read method of myInputReport. ReadInputReportDelegate MyReadInputReportDelegate = new ReadInputReportDelegate(myInputReport.Read); // The BeginInvoke method calls myInputReport.Read to attempt to read a report. // The method has the same parameters as the Read function, // plus two additional parameters: // GetInputReportData is the callback procedure that executes when the Read function returns. // MyReadInputReportDelegate is the asynchronous delegate object. // The last parameter can optionally be an object passed to the callback. ar = MyReadInputReportDelegate.BeginInvoke(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success, new AsyncCallback(GetInputReportData), MyReadInputReportDelegate); } } else { //Tracer.Trace("No attempt to read an Input report was made."); if (doInputReport) { Tracer.Error("The HID doesn't have an Input report, but it was requested."); } } } else { Tracer.Error("Invalid handle. The device is probably a system mouse or keyboard."); Tracer.Error("No attempt to write an Output report or read an Input report was made."); } return(inputReportBuffer); // may still be null } catch (Exception ex) { Tracer.Error(ex); throw; } }
public bool FillRxBuffer() { Byte[] inputReportBuffer = null; Boolean success = false; readBuffer.Clear(); this.readBuffer.Clear(); this.ReadFinished = false; this.ReadStart = 0; //Thread.Sleep(10); /* if (readHandle.IsClosed) { Console.Out.WriteLine("readHandle closed"); } */ if (!readHandle.IsClosed) { if (!readHandle.IsInvalid && MyHid.Capabilities.InputReportByteLength > 0) //if (MyHid.Capabilities.InputReportByteLength > 0) { // Set the size of the Input report buffer. inputReportBuffer = new Byte[MyHid.Capabilities.InputReportByteLength]; // Read a report using interrupt transfers. // To enable reading a report without blocking the main thread, this // application uses an asynchronous delegate. IAsyncResult ar = null; Hid.InputReportViaInterruptTransfer myInputReport = new Hid.InputReportViaInterruptTransfer(); // Define a delegate for the Read method of myInputReport. ReadInputReportDelegate MyReadInputReportDelegate = new ReadInputReportDelegate(myInputReport.Read); /* if (readHandle.IsClosed) { Debug.WriteLine("readHandle Closed"); } */ // The BeginInvoke method calls myInputReport.Read to attempt to read a report. // The method has the same parameters as the Read function, // plus two additional parameters: // GetInputReportData is the callback procedure that executes when the Read function returns. // MyReadInputReportDelegate is the asynchronous delegate object. // The last parameter can optionally be an object passed to the callback. ar = MyReadInputReportDelegate.BeginInvoke(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success, new AsyncCallback(GetInputReportData), MyReadInputReportDelegate); } else { //Console.Out.WriteLine("No attempt to read an Input report was made."); Debug.WriteLine("No attempt to read an Input report was made."); //Console.Out.WriteLine("The HID doesn't have an Input report."); Debug.WriteLine("The HID doesn't have an Input report."); return false; } } else { Debug.WriteLine("readHandle Closed"); return false; } //while (this.readBuffer.Count < 63) //{ // Thread.Sleep(5); //} //ReadFinished = true; return true; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public int McrUSB_WR(string strData, string strUSBpipeName, ref byte[] McrUsbReceiveDataReturn, int TimeUpRcv = 1000) { int i; string[] strUSBCmd; byte[] byteUSBCmd; bool success = false; try { //Analyze command strUSBCmd = strData.Split(','); byteUSBCmd = new byte[strUSBCmd.Length]; //Convert to hexa format for (i = 0; i < strUSBCmd.Length; i++) { if (byte.TryParse(strUSBCmd[i], System.Globalization.NumberStyles.HexNumber, null, out byteUSBCmd[i]) == false) { return(200); //Error: Cannot convert to numeric } } //Check if command length is OK or not if ((byteUSBCmd.Length - 1) != byteUSBCmd[0]) { return(201); //Command length is NG } //Calculate Command code for check return later intUSBCommandSendingCode = byteUSBCmd[1] * 256 + byteUSBCmd[2]; //Check if device is connected or not bool blDeviceConnect = isDeviceConnected(this.intVendorID, this.intProductID); if (blDeviceConnect == false) { return(202); //Error: Device not connected } //Create HID handle hidHandle = CreateFile(strUSBpipeName, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, 0); if (hidHandle.IsInvalid == true) //If hidHandle create fail { hidHandle.Close(); return(202); //Error: Cannot create HID Handle } //Learn the Capabilities of device if (blManualSetCapabilities == true) { Capabilities.OutputReportByteLength = shrtOutputReportByteLength; Capabilities.InputReportByteLength = shrtInputReportByteLength; } else { Capabilities = GetDeviceCapabilities(hidHandle); } // Get handles to use in requesting Input and Output reports. readHandle = CreateFile(strUSBpipeName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); if (readHandle.IsInvalid == true) //If readHandle create fail { readHandle.Close(); return(203); //Error: Cannot create Read Handle } //Get write handle writeHandle = CreateFile(strUSBpipeName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, 0); //Flush any waiting reports in the input buffer. (optional) FlushQueue(readHandle); if (writeHandle.IsInvalid == true) //If readHandle create fail { writeHandle.Close(); return(204); //Error: Cannot create Write Handle } //Don't attempt to send an Output report if the HID has no Output report. if (Capabilities.OutputReportByteLength <= 0) { readHandle.Close(); writeHandle.Close(); return(205); //Error: HID has no Output report } //Attemp to write //byte[] outputReportBuffer = new byte[256]; byte[] outputReportBuffer = null; Array.Resize(ref outputReportBuffer, Capabilities.OutputReportByteLength); //Store the report ID in the first byte of the buffer: outputReportBuffer[0] = 0; //for (i = 1; i < Capabilities.OutputReportByteLength; i++) //{ // outputReportBuffer[i] = byteUSBCmd[i-1]; //} for (i = 1; i < byteUSBCmd.Length + 1; i++) { outputReportBuffer[i] = byteUSBCmd[i - 1]; } success = Write(outputReportBuffer, writeHandle); // Don't attempt to send an Input report if the HID has no Input report. // (The HID spec requires all HIDs to have an interrupt IN endpoint, // which suggests that all HIDs must support Input reports.) if (Capabilities.InputReportByteLength <= 0) { readHandle.Close(); writeHandle.Close(); return(206); //Error: HID has no Input report } Byte[] inputReportBuffer = null; Array.Resize(ref inputReportBuffer, Capabilities.InputReportByteLength); // Read a report using interrupt transfers. InputReportViaInterruptTransfer myInputReport = new InputReportViaInterruptTransfer(); if (blUseAsyncRead == true) { // To enable reading a report without blocking the main thread, this // application uses an asynchronous delegate. IAsyncResult ar = null; // Define a delegate for the Read method of myInputReport. ReadInputReportDelegate MyReadInputReportDelegate = new ReadInputReportDelegate(myInputReport.Read); // The BeginInvoke method calls myInputReport.Read to attempt to read a report. // The method has the same parameters as the Read function, // plus two additional parameters: // GetInputReportData is the callback procedure that executes when the Read function returns. // MyReadInputReportDelegate is the asynchronous delegate object. // The last parameter can optionally be an object passed to the callback. ar = MyReadInputReportDelegate.BeginInvoke(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success, new AsyncCallback(GetInputReportData), MyReadInputReportDelegate); blUSBSendCmdFinish = false; intUSBCommandReceiveCode = -1; //Wait return data with default 1000 ms time out int intStartTick = Environment.TickCount; do { if ((Environment.TickCount - intStartTick) > TimeUpRcv) { break; } }while (intUSBCommandReceiveCode != intUSBCommandSendingCode); } else { myInputReport.Read(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success); } //Calculate received command code intUSBCommandReceiveCode = inputReportBuffer[2] * 256 + inputReportBuffer[3]; //Transfer data to McrUsbReceiveDataReturn array int intLen = McrUsbReceiveDataReturn.Length; if (inputReportBuffer.Length > intLen) { intLen = inputReportBuffer.Length; } McrUsbReceiveDataReturn = new byte[intLen]; //Clear old data if (intUSBCommandReceiveCode == intUSBCommandSendingCode) //Sending & Receive usb command is OK { for (i = 1; i < inputReportBuffer.Length; i++) { McrUsbReceiveDataReturn[i - 1] = inputReportBuffer[i]; } } //Finally close all handle hidHandle.Close(); readHandle.Close(); writeHandle.Close(); // if (intUSBCommandReceiveCode == intUSBCommandSendingCode) { return(0); //OK code } else { return(299); //NG code } } catch (Exception ex) { return(-1); //Unexpected error code } }