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

📄 frmmain.cs

📁 usbio using microsoft visual c# example
💻 CS
📖 第 1 页 / 共 5 页
字号:
		{
			
			// Attempt to write a report and read a report.
			
			try {
				// Don't allow another transfer request until this one completes.
				cmdOnce.Enabled = false;
				
				ReadAndWriteToDevice();
				
			} catch (Exception ex) {
				HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
		}
		
		private void DeviceHasChanged ()
		{
			
			// Called if the user changes the Vendor ID or Product ID in the text box.
			try
			{
				// If a device was previously detected, stop receiving notifications about it.
				if (_MyDeviceDetected) {
					_MyDeviceManagement.StopReceivingDeviceNotifications(_DeviceNotificationHandle);
				}
				
				// Search for the device the next time FindTheHid is called.
				_MyDeviceDetected = false;
				
			} catch (Exception ex) {
				HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
		}
		
		private void ExchangeFeatureReports ()
		{
			
			// Purpose    : Sends a Feature report, then retrieves one.
			//            : Assumes report ID = 0 for both reports.
			
			string ByteValue;
            int Count;  //GRV
			byte[] InFeatureReportBuffer;
			byte[] OutFeatureReportBuffer;
			bool Success=false;
			
			try
			{
				Hid.InFeatureReport myInFeatureReport = new Hid.InFeatureReport();
				Hid.OutFeatureReport myOutFeatureReport = new Hid.OutFeatureReport();

                //GRV - added
                if (_MyHID.Capabilities.FeatureReportByteLength > 0)
                   //the HID has a featrure report
                {
                    // Set the size of the Feature report buffer.
                    OutFeatureReportBuffer = new byte[_MyHID.Capabilities.FeatureReportByteLength];

                    // Store the report ID in the buffer:
                    OutFeatureReportBuffer[0] = 0;

                    // Store the report data following the report ID.
                    // Use the data in the combo boxes on the form.
                    OutFeatureReportBuffer[1] = Byte.Parse(cboByte0.SelectedIndex.ToString());

                    if (OutFeatureReportBuffer.Length > 1)
                        OutFeatureReportBuffer[2] = Byte.Parse(cboByte1.SelectedIndex.ToString());


                    // Write a report to the device
                    Success = myOutFeatureReport.Write(OutFeatureReportBuffer, _HIDHandle);

                    if (Success)
                    {
                        lstResults.Items.Add("A Feature report has been written.");

                        Debug.WriteLine(" Feature Report ID: " + OutFeatureReportBuffer[0]);
                        Debug.WriteLine(" Feature Report Data:");

                        // Display the report data sent in the form's list box.
                        lstResults.Items.Add(" Feature Report ID: " + OutFeatureReportBuffer[0]);
                        lstResults.Items.Add(" Feature Report Data:");

                        for (Count = 1; Count < OutFeatureReportBuffer.Length; Count ++ )
                        {

                            // Add a leading zero to values from 0 to F.
                            if (OutFeatureReportBuffer[Count].ToString("x").Length < 2)
                            {
                                ByteValue = "0" + OutFeatureReportBuffer[Count].ToString("x");
                            }
                            else
                            {
                                ByteValue = OutFeatureReportBuffer[Count].ToString("x");
                            }

                            Debug.WriteLine(" " + ByteValue);
                            lstResults.Items.Add(" " + ByteValue);
                        }
                    }
                    else
                    {
                        lstResults.Items.Add("The attempt to write a Feature report has failed.");
                    }

                }

		

				//GRV - mods
				// Read a report from the device.

                if (_MyHID.Capabilities.FeatureReportByteLength > 0)
                {
                    // Set the size of the Feature report buffer.
                    InFeatureReportBuffer = new byte[_MyHID.Capabilities.FeatureReportByteLength];

                    // Read a report.
                    myInFeatureReport.Read
                        (_ReadHandle,
                        _HIDHandle,
                        _WriteHandle,
                        ref _MyDeviceDetected,
                        ref InFeatureReportBuffer,
                        ref Success);

                    if (Success)
                        {
                            lstResults.Items.Add("A Feature report has been read.");
                            Debug.WriteLine(" Feature Report ID: " + InFeatureReportBuffer[0]);
                            Debug.WriteLine(" Feature Report Data:");

                            // Display the report data received in the form's list box.
                            lstResults.Items.Add(" Feature Report ID: " + InFeatureReportBuffer[0]);
                            lstResults.Items.Add(" Feature Report Data:");

                            txtBytesReceived.Text = "";
                            for (int i = 0; i < InFeatureReportBuffer.Length; i++)
                            {

                                // Add a leading zero to values from 0 to F.
                                if (InFeatureReportBuffer[i].ToString("x").Length < 2)
                                {
                                    ByteValue = "0" + InFeatureReportBuffer[i].ToString("x");
                                }
                                else
                                {
                                    ByteValue = InFeatureReportBuffer[i].ToString("x");
                                }

                                Debug.WriteLine(" " + ByteValue);
                                lstResults.Items.Add(" " + ByteValue);

                                // Display the received bytes in the "Received Bytes" text box.
                                txtBytesReceived.SelectionStart = txtBytesReceived.Text.Length;
                                txtBytesReceived.SelectedText = ByteValue + "\n";
                            }
                        }

                    else
                        {
                            lstResults.Items.Add("The attempt to read a Feature report has failed.");
                        }
                }

                else
                {
                    lstResults.Items.Add("The HID doesn't have a a Feature report.");
                }
				
				ScrollToBottomOfListBox();
				cmdOnce.Enabled = true;
				
			} catch (Exception ex) {
				HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
			
		}
		
		
		private void ExchangeInputAndOutputReports ()
		{
			// Purpose    : Sends an Outputreport, then retrieves an Input report.
			//            : Assumes report ID = 0 for both reports.
			string ByteValue;
            int Count;  //GRV
			byte[] InputReportBuffer;
			byte[] OutputReportBuffer;
			bool Success;
			
			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 != FileIOApiDeclarations.INVALID_HANDLE_VALUE) && (_WriteHandle != FileIOApiDeclarations.INVALID_HANDLE_VALUE))
                {
                    //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.
                        // Use the data in the combo boxes on the form.
                        OutputReportBuffer[1] = Byte.Parse(cboByte0.SelectedIndex.ToString());

                        if (OutputReportBuffer.Length > 1)
                            OutputReportBuffer[2] = Byte.Parse(cboByte1.SelectedIndex.ToString());

                        // Write a report.

                        if ((chkUseControlTransfersOnly.Checked) == 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); //GRV - corrected
                        }
                        else
                        {
                            // If the HID has an interrupt OUT endpoint, use an interrupt transfer
                            // to send the report. Otherwise use a control transfer.

                            Hid.OutputReport myOutputReport = new Hid.OutputReport();
                            Success = myOutputReport.Write(OutputReportBuffer, _WriteHandle); //GRV - corrected
                        }


                        if (Success)
                        {
                            lstResults.Items.Add("An Output report has been written.");

                            // Display the report data in the form's list box.
                            lstResults.Items.Add(" Output Report ID: " + OutputReportBuffer[0]);
                            lstResults.Items.Add(" Output Report Data:");

                            txtBytesReceived.Text = "";
                            for (Count = 1; Count < OutputReportBuffer.Length; Count++)
                            {

                                // Add a leading zero to values from 0 to F.
                                if (OutputReportBuffer[Count].ToString("x").Length < 2)
                                {
                                    ByteValue = "0" + OutputReportBuffer[Count].ToString("x");
                                }
                                else
                                {
                                    ByteValue = OutputReportBuffer[Count].ToString("x");
                                }

                                Debug.WriteLine(" Output Report ID: " + OutputReportBuffer[0]);
                                Debug.WriteLine(" Output Report Data:");
                                Debug.WriteLine(" " + ByteValue);
                                lstResults.Items.Add(" " + ByteValue);

                            }
                        }
                        else
                        {
                            lstResults.Items.Add("The attempt to write an Output report has failed.");
                        }


                    }
                    else
                    {
                        lstResults.Items.Add("The HID doesn't have an Output report.");
                    }




                    // Read an Input report

                    //Don't attempt to read an input report if HID doesn't have one,
                    //(HID spec requires all HIDs to have an interrupt IN endpoint,
                    //which suggests 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];

                        if (chkUseControlTransfersOnly.Checked == true)
                        {
                            // Read a report using a control transfer.
                            Hid.InputReportViaControlTransfer myInputReport = new Hid.InputReportViaControlTransfer();

                            // Read the report.
                            //GRV - mod
                            myInputReport.Read(_ReadHandle, _HIDHandle, _WriteHandle, ref _MyDeviceDetected, ref InputReportBuffer, ref Success);

⌨️ 快捷键说明

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