📄 ftestusb.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Threading;
using EzUsb;
namespace TestUSB
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class FTestUsb : System.Windows.Forms.Form
{
private bool _stop = false;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.StatusBar statusBar;
private System.Windows.Forms.StatusBarPanel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button4;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public FTestUsb()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
button4.Enabled = false;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.statusBar = new System.Windows.Forms.StatusBar();
this.panel1 = new System.Windows.Forms.StatusBarPanel();
this.button2 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.panel1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Device Descriptor";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.textBox1.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox1.Location = new System.Drawing.Point(8, 88);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(888, 416);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
this.textBox1.WordWrap = false;
//
// button3
//
this.button3.Location = new System.Drawing.Point(152, 16);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 23);
this.button3.TabIndex = 4;
this.button3.Text = "Bulk Transfer";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 527);
this.statusBar.Name = "statusBar";
this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.panel1});
this.statusBar.ShowPanels = true;
this.statusBar.Size = new System.Drawing.Size(904, 22);
this.statusBar.SizingGrip = false;
this.statusBar.TabIndex = 14;
//
// panel1
//
this.panel1.Width = 572;
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 23);
this.button2.TabIndex = 15;
this.button2.Text = "Bulk Read";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(152, 48);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(120, 23);
this.button4.TabIndex = 16;
this.button4.Text = "Stop Read";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// FTestUsb
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(904, 549);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button4,
this.button2,
this.statusBar,
this.button3,
this.textBox1,
this.button1});
this.Name = "FTestUsb";
this.Text = "Test USB";
((System.ComponentModel.ISupportInitialize)(this.panel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FTestUsb());
}
private void ErrorMessageHandler( object sender, ErrorEventArgs e )
{
statusBar.Panels[0].Text = e.Message;
}
private void button1_Click(object sender, System.EventArgs e)
{
Usb usb = new Usb();
usb.OnErrorMessage += new Usb.ErrorMessage( this.ErrorMessageHandler );
if( !usb.Open( "ezusb-0" ) )
{
return;
}
UsbDeviceDescriptor descriptor = usb.GetDeviceDescriptor();
if( descriptor != null )
{
DisplayUsbDeviceDescriptor( descriptor );
}
usb.Close();
}
private void DisplayUsbDeviceDescriptor( UsbDeviceDescriptor descriptor )
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat( "{0:X}\r\n", descriptor.bLength );
sb.AppendFormat( "{0:X}\r\n", descriptor.bDescriptorType );
sb.AppendFormat( "{0:X}\r\n", descriptor.bcdUSB );
sb.AppendFormat( "{0:X}\r\n", descriptor.bDeviceClass );
sb.AppendFormat( "{0:X}\r\n", descriptor.bDeviceSubClass );
sb.AppendFormat( "{0:X}\r\n", descriptor.bDeviceProtocol );
sb.AppendFormat( "{0:X}\r\n", descriptor.bMaxPacketSize0 );
sb.AppendFormat( "{0:X}\r\n", descriptor.idVendor );
sb.AppendFormat( "{0:X}\r\n", descriptor.idProduct );
sb.AppendFormat( "{0:X}\r\n", descriptor.bcdDevice );
sb.AppendFormat( "{0:X}\r\n", descriptor.iManufacturer );
sb.AppendFormat( "{0:X}\r\n", descriptor.iProduct );
sb.AppendFormat( "{0:X}\r\n", descriptor.iSerialNumber );
sb.AppendFormat( "{0:X}\r\n", descriptor.bNumConfigurations );
textBox1.Text = sb.ToString();
}
private void button3_Click(object sender, System.EventArgs e)
{
FBulkXfer bx = new FBulkXfer();
bx.ShowDialog( this );
}
private void button2_Click(object sender, System.EventArgs e)
{
_stop = false;
button4.Enabled = true;
Usb usb = new Usb();
usb.OnErrorMessage += new Usb.ErrorMessage( this.ErrorMessageHandler );
if( !usb.Open( "ezusb-0" ) )
{
return;
}
byte[] byteBuf;
while( !_stop )
{
byteBuf = usb.BulkRead( 0 );
if( byteBuf != null )
{
StringBuilder msg = new StringBuilder( 256 );
for( int j = 0 ; j < byteBuf.Length ; j++ )
{
msg.AppendFormat( "{0:X} ", byteBuf[j] );
}
msg.Append( "\r\n" );
textBox1.AppendText( msg.ToString() );
//char[] data = new char[byteBuf.Length];
//Buffer.BlockCopy( byteBuf, 0, data, 0, data.Length );
//string str = new String( data );
//textBox1.AppendText( str );
//textBox1.AppendText( "\r\n" );
}
System.Windows.Forms.Application.DoEvents();
Thread.Sleep( 1000 );
}
usb.Close();
}
private void button4_Click(object sender, System.EventArgs e)
{
_stop = true;
button4.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -