📄 fbulkxfer.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using EzUsb;
namespace TestUSB
{
/// <summary>
/// Summary description for FBulkXfer.
/// </summary>
public class FBulkXfer : System.Windows.Forms.Form
{
private int _BlkSize = 1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBoxBlkSize;
private System.Windows.Forms.TextBox textBoxInData;
private System.Windows.Forms.TextBox textBoxOutData;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.StatusBarPanel panel1;
private System.Windows.Forms.StatusBar statusBar;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public FBulkXfer()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBoxBlkSize = new System.Windows.Forms.TextBox();
this.textBoxInData = new System.Windows.Forms.TextBox();
this.textBoxOutData = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.statusBar = new System.Windows.Forms.StatusBar();
this.panel1 = new System.Windows.Forms.StatusBarPanel();
((System.ComponentModel.ISupportInitialize)(this.panel1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(536, 40);
this.label1.TabIndex = 0;
this.label1.Text = "This application demonstrates using Bulk Transfers to move data in and out of EZ-" +
"USB devices using the EZ-USB Generic Device Driver.";
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(288, 24);
this.label2.TabIndex = 1;
this.label2.Text = "Follow the steps listed below to see the demonstration.";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 128);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(440, 24);
this.label3.TabIndex = 2;
this.label3.Text = "1) Use the \"Ez-USB Control Panel\" application to download \"Ep_Pair.hex\" to a EZ-U" +
"SB ";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 166);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(272, 23);
this.label4.TabIndex = 3;
this.label4.Text = "2) Choose the EZ-USB Board you selected in Step 1.";
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 203);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(296, 23);
this.label5.TabIndex = 4;
this.label5.Text = "3) Enter the number of characters that triggers a transfer:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(16, 240);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(416, 32);
this.label6.TabIndex = 5;
this.label6.Text = "4) Enter text into this textbox. When the number of characters entered equals th" +
"e \"trigger\" size, the characters will appear in the output textbox.";
//
// label7
//
this.label7.Location = new System.Drawing.Point(16, 368);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(152, 23);
this.label7.TabIndex = 6;
this.label7.Text = "5) See output text here:";
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(328, 167);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 7;
//
// textBoxBlkSize
//
this.textBoxBlkSize.Location = new System.Drawing.Point(328, 204);
this.textBoxBlkSize.Name = "textBoxBlkSize";
this.textBoxBlkSize.TabIndex = 8;
this.textBoxBlkSize.Text = "";
this.textBoxBlkSize.TextChanged += new System.EventHandler(this.textBoxBlkSize_TextChanged);
//
// textBoxInData
//
this.textBoxInData.Location = new System.Drawing.Point(32, 280);
this.textBoxInData.Multiline = true;
this.textBoxInData.Name = "textBoxInData";
this.textBoxInData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBoxInData.Size = new System.Drawing.Size(424, 64);
this.textBoxInData.TabIndex = 9;
this.textBoxInData.Text = "";
this.textBoxInData.TextChanged += new System.EventHandler(this.textBoxInData_TextChanged);
//
// textBoxOutData
//
this.textBoxOutData.Location = new System.Drawing.Point(32, 392);
this.textBoxOutData.Multiline = true;
this.textBoxOutData.Name = "textBoxOutData";
this.textBoxOutData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBoxOutData.Size = new System.Drawing.Size(424, 64);
this.textBoxOutData.TabIndex = 10;
this.textBoxOutData.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(472, 301);
this.button1.Name = "button1";
this.button1.TabIndex = 11;
this.button1.Text = "Clear";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(472, 413);
this.button2.Name = "button2";
this.button2.TabIndex = 12;
this.button2.Text = "Clear";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 471);
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(572, 22);
this.statusBar.SizingGrip = false;
this.statusBar.TabIndex = 13;
//
// panel1
//
this.panel1.Width = 572;
//
// FBulkXfer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(572, 493);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar,
this.button2,
this.button1,
this.textBoxOutData,
this.textBoxInData,
this.textBoxBlkSize,
this.comboBox1,
this.label7,
this.label6,
this.label5,
this.label4,
this.label3,
this.label2,
this.label1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "FBulkXfer";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.Text = "FBulkXfer";
this.Load += new System.EventHandler(this.FBulkXfer_Load);
((System.ComponentModel.ISupportInitialize)(this.panel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void FBulkXfer_Load(object sender, System.EventArgs e)
{
SetupDriverComboBox();
textBoxBlkSize.Text = _BlkSize.ToString();
}
private void button1_Click(object sender, System.EventArgs e)
{
textBoxInData.Text = "";
}
private void button2_Click(object sender, System.EventArgs e)
{
textBoxOutData.Text = "";
}
private void textBoxBlkSize_TextChanged(object sender, System.EventArgs e)
{
_BlkSize = int.Parse( textBoxBlkSize.Text );
if( _BlkSize > Usb.MaxBlkSize )
{
_BlkSize = Usb.MaxBlkSize;
textBoxBlkSize.Text = _BlkSize.ToString();
}
}
private void textBoxInData_TextChanged(object sender, System.EventArgs e)
{
if( textBoxInData.Text.Length >= _BlkSize )
{
string inText = textBoxInData.Text;
int byteCount = inText.Length;
Usb usb = new Usb();
usb.OnErrorMessage += new Usb.ErrorMessage( this.ErrorMessageHandler );
if( usb.Open( (string)comboBox1.SelectedItem ) )
{
char[] data = inText.ToCharArray();
int size = Buffer.ByteLength( data );
byte[] byteBuf = new byte[size];
Buffer.BlockCopy( data, 0, byteBuf, 0, size );
int count = usb.BulkWrite( byteBuf, 1 );
if( count != 0 )
{
byteBuf = usb.BulkRead( 0 );
if( byteBuf != null )
{
data = new char[byteBuf.Length];
Buffer.BlockCopy( byteBuf, 0, data, 0, data.Length );
string msg = new String( data );
textBoxOutData.Text = msg;
}
else
{
MessageBox.Show( "Bulk Read failed.",
"EZ-USB Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}
else
{
MessageBox.Show( "Bulk Write failed.",
"EZ-USB Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
usb.Close();
}
}
}
private const int MAX_USB_DEV_NUMBER = 32;
private void SetupDriverComboBox()
{
comboBox1.Items.Clear();
Usb usb = new Usb();
usb.OnErrorMessage += new Usb.ErrorMessage( this.ErrorMessageHandler );
bool found = false;
for( int j = 0 ; j < MAX_USB_DEV_NUMBER ; j++ )
{
StringBuilder driverName = new StringBuilder( "ezusb-" + j.ToString() );
if( usb.Open( driverName.ToString() ) )
{
comboBox1.Items.Add( driverName.ToString() );
found = true;
usb.Close();
}
}
if( !found )
{
MessageBox.Show( "No EZ-USB device drivers were found. Perhaps no device is connected.",
"EZ-USB Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
else
{
statusBar.Panels[0].Text = "";
comboBox1.SelectedIndex = 0;
comboBox1.Select( 0, 0 );
}
}
private void ErrorMessageHandler( object sender, ErrorEventArgs e )
{
statusBar.Panels[0].Text = e.Message;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -