📄 frmmain.cs
字号:
// Set the number of Input reports the host will store.
try {
SetInputReportBufferSize();
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void cmdFindDevice_Click (System.Object sender, System.EventArgs e)
{
// Search for a specific device.
try {
FindTheHid();
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void cmdOnce_Click (System.Object eventSender, System.EventArgs eventArgs)
{
// 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;
byte[] InFeatureReportBuffer;
byte[] OutFeatureReportBuffer;
bool Success;
try
{
Hid.InFeatureReport myInFeatureReport = new Hid.InFeatureReport();
Hid.OutFeatureReport myOutFeatureReport = new Hid.OutFeatureReport();
// Set the size of the Feature report buffer.
// Subtract 1 from the value in the Capabilities structure because
// the array begins at index 0.
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());
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:");
// txtBytesReceived.Text = ""
for (int i = 1; i <= 2; i++) {
// Add a leading zero to values from 0 to F.
if (OutFeatureReportBuffer[i].ToString("x").Length < 2) {
ByteValue = "0" + OutFeatureReportBuffer[i].ToString("x");
} else {
ByteValue = OutFeatureReportBuffer[i].ToString("x");
}
Debug.WriteLine(" " + ByteValue);
lstResults.Items.Add(" " + ByteValue);
}
} else {
lstResults.Items.Add("The attempt to write a Feature report has failed.");
}
// Read a report from the device.
// Set the size of the Feature report buffer.
// Subtract 1 from the value in the Capabilities structure because
// the array begins at index 0.
InFeatureReportBuffer = new byte[_MyHID.Capabilities.FeatureReportByteLength];
// Read a report.
myInFeatureReport.Read(_ReadHandle, _HIDHandle, 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.");
}
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;
byte[] InputReportBuffer;
byte[] OutputReportBuffer;
bool Success;
try
{
// Set the size of the Output report buffer.
// Subtract 1 from the value in the Capabilities structure because
// the array begins at index 0.
OutputReportBuffer = new byte[_MyHID.Capabilities.OutputReportByteLength-1 + 1];
// 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());
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, _HIDHandle);
} 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, _HIDHandle);
}
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 (int i = 1; i <= 2; i++) {
// Add a leading zero to values from 0 to F.
if (OutputReportBuffer[i].ToString("x").Length < 2) {
ByteValue = "0" + OutputReportBuffer[i].ToString("x");
} else {
ByteValue = OutputReportBuffer[i].ToString("x");
}
Debug.WriteLine(" " + ByteValue);
lstResults.Items.Add(" " + ByteValue);
}
} else {
lstResults.Items.Add("The attempt to write an Output report has failed.");
}
Debug.WriteLine(" Output Report ID: " + OutputReportBuffer[0]);
Debug.WriteLine(" Output Report Data:");
// Read an Input report.
// Set the size of the Input report buffer.
// Subtract 1 from the value in the Capabilities structure because
// the array begins at index 0.
InputReportBuffer = new byte[_MyHID.Capabilities.InputReportByteLength-1 + 1];
if (chkUseControlTransfersOnly.Checked == true) {
// Read a report using a control transfer.
Hid.InputReportViaControlTransfer myInputReport = new Hid.InputReportViaControlTransfer();
// Read the report.
myInputReport.Read(_ReadHandle, _HIDHandle, ref _MyDeviceDetected, ref InputReportBuffer, ref Success);
if (Success) {
lstResults.Items.Add("An Input report has been read.");
Debug.WriteLine(" Input Report ID: " + InputReportBuffer[0]);
Debug.WriteLine(" Input Report Data:");
// Display the report data received in the form's list box.
lstResults.Items.Add(" Input Report ID: " + InputReportBuffer[0]);
lstResults.Items.Add(" Input Report Data:");
txtBytesReceived.Text = "";
for (int i = 0; i <= InputReportBuffer.Length; i++) {
// Add a leading zero to values from 0 to F.
if (InputReportBuffer[i].ToString("x").Length < 2) {
ByteValue = "0" + InputReportBuffer[i].ToString("x");
} else {
ByteValue = InputReportBuffer[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 an Input report has failed.");
}
ScrollToBottomOfListBox();
// Enable requesting another transfer.
cmdOnce.Enabled = true;
} else {
// Read a report using interrupt transfers.
// To enable reading a report without blocking the main thread, this
// application uses an asynchronous delegate.
IAsyncResult ar;
Hid.InputReport myInputReport = new Hid.InputReport();
// 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(_ReadHandle, _HIDHandle, ref _MyDeviceDetected, ref InputReportBuffer, ref Success, new AsyncCallback( GetInputReportData), MyReadInputReportDelegate);
}
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void frmMain_Closed (System.Object eventSender, System.EventArgs eventArgs)
{
try {
// Perform shutdown operations.
Shutdown();
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void frmMain_Load (System.Object eventSender, System.EventArgs eventArgs)
{
Console.WriteLine ((16).ToString("x"));
Console.WriteLine ((0).ToString("x"));
Console.WriteLine ((255).ToString("x"));
// Perform startup operations.
try {
this.Show();
Startup();
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
private void GetInputReportBufferSize ()
{
// Purpose : Finds and displays the number of Input buffers
// : (the number of Input reports the host will store).
int NumberOfInputBuffers = 0;
try {
// Get the number of input buffers.
_MyHID.GetNumberOfInputBuffers(_HIDHandle, ref NumberOfInputBuffers);
// Display the result in the text box.
txtInputReportBufferSize.Text = System.Convert.ToString(NumberOfInputBuffers);
} catch (Exception ex) {
HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -