///  <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;
            }
        }
Beispiel #2
0
            public void RW(Queue<byte> writeBuffer, int count)
            {
                String byteValue = null;
                Int32 counter = 0;
                Byte[] inputReportBuffer = null;
                Byte[] outputReportBuffer = null;
                Boolean success = false;

                Thread.Sleep(10);

                try
                {
                int c = writeBuffer.Count;
                byte[] buffer = new byte[65];;

                if (writeBuffer.Count > 0)
                {
                    buffer[0] = 0;
                    buffer[1] = (byte)writeBuffer.Count;
                    writeBuffer.ToArray().CopyTo(buffer, 2);
                    writeBuffer.Clear();
                }

                //else if(writeBuffer.Count == 0)
                //{
                //    return;
                //}

                    success = false;

                    //this.FindTheHid();
                    //if ((myDeviceDetected == false))
                    //{
                    //    FindTheHid();
                    //    myDeviceDetected = true;
                    //}

                    //  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 (MyHid.Capabilities.OutputReportByteLength > 0)
                        {
                            //  Set the size of the Output report buffer.
                            outputReportBuffer = new Byte[MyHid.Capabilities.OutputReportByteLength];

                            //  Store the report ID in the first byte of the buffer:
                            outputReportBuffer[0] = 0;

                            //  Store the report data following the report ID.
                            buffer.CopyTo(outputReportBuffer, 0);

                            //outputReportBuffer[1] = 0x0D;
                            /*
                            for (int i = 0; i < c; i++)
                            {
                                outputReportBuffer[i] = buffer[0];
                                writeBuffer.ToArray().CopyTo(buffer, 2);
                            }
                             */

                            //outputReportBuffer[1] = Convert.ToByte(cboByte0.SelectedIndex);

                            //if (Information.UBound(outputReportBuffer, 1) > 1)
                            //{
                            //    outputReportBuffer[2] = Convert.ToByte(cboByte1.SelectedIndex);
                            //}

                            //WriteFile uses an interrupt transfer to send the report.
                            Hid.OutputReportViaInterruptTransfer myOutputReport = new Hid.OutputReportViaInterruptTransfer();
                            success = myOutputReport.Write(outputReportBuffer, writeHandle);

                            if (success)
                            {
                                //Console.Out.WriteLine("\nAn Output report has been written.");
                                Debug.WriteLine("\nAn Output report has been written.");
                                //Console.Out.WriteLine("Output Report ID: " + String.Format("{0:X2} ", outputReportBuffer[0]));
                                Debug.WriteLine("Output Report ID: " + String.Format("{0:X2} ", outputReportBuffer[0]));
                                //Console.Out.WriteLine("Output Report Data:");
                                Debug.WriteLine("Output Report Data:");
                                for (counter = 0; counter <= outputReportBuffer.Length - 1; counter++)
                                {
                                    //  Display bytes as 2-character hex strings.
                                    byteValue = String.Format("{0:X2} ", outputReportBuffer[counter]);
                                    //Console.Out.Write(" " + byteValue);
                                    Debug.Write(" " + byteValue);
                                }
                            }
                            else
                            {
                                //Console.Out.WriteLine("The attempt to write an Output report has failed.");
                                Debug.WriteLine("The attempt to write an Output report has failed.");
                            }
                        }
                        else
                        {
                            //Console.Out.WriteLine("The HID doesn't have an Output report.");
                            Debug.WriteLine("The HID doesn't have an Output report.");
                        }

                        /*
                        //  Read an Input report.
                        success = false;
                        //  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 (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);

                            //  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.");
                            Console.Out.WriteLine("The HID doesn't have an Input report.");
                        }

                        while (this.readBuffer.Count < 63)
                        {
                            Thread.Sleep(5);
                        }
                        this.ReadFinished = true;

                        //this.readBuffer.Clear();
                        //this.ReadFinished = false;
                        this.ReadStart = 0;
                    */

                    }
                    else
                    {
                        //Console.Out.WriteLine("Invalid handle. The device is probably a system mouse or keyboard.");
                        Debug.WriteLine("Invalid handle. The device is probably a system mouse or keyboard.");
                        //Console.Out.WriteLine("No attempt to write an Output report or read an Input report was made.");
                        Debug.WriteLine("No attempt to write an Output report or read an Input report was made.");
                    }
                }

                catch (Exception ex)
                {
                    Console.Out.WriteLine(ex.ToString());
                    throw;
                }
                Thread.Sleep(10);
            }