基本信息
源码名称:USB_HID测试小助手源码测试成功可更改
源码大小:3.32M
文件格式:.zip
开发语言:C#
更新时间:2018-12-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace USB_HID
{
public class Form1 : Form
{
private IContainer components = null;
private ComboBox com_name;
private Button but_scan;
private Label label1;
private Label label2;
private Label label3;
private TextBox text_pid;
private TextBox text_vid;
private Button but_con;
private GroupBox groupBox1;
private GroupBox groupBox2;
private RichTextBox log;
private StatusStrip statusStrip1;
private ToolStripStatusLabel toolStripStatusLabel1;
private MenuStrip menuStrip1;
private ToolStripMenuItem 文件ToolStripMenuItem;
private ToolStripMenuItem 帮助ToolStripMenuItem;
private ToolStripMenuItem 关于ToolStripMenuItem;
private RichTextBox rich_send;
private Button but_send;
private TextBox textBox4;
private Button button4;
private Button button3;
private CheckBox checkBox1;
private GroupBox groupBox3;
private CheckBox checkBox2;
private CheckBox checkBox4;
private CheckBox checkBox3;
private HIDDevice.interfaceDetails[] devices;
private HIDDevice device;
protected override void Dispose(bool disposing)
{
if (disposing && this.components != null)
{
this.components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.com_name = new ComboBox();
this.but_scan = new Button();
this.label1 = new Label();
this.label2 = new Label();
this.label3 = new Label();
this.text_pid = new TextBox();
this.text_vid = new TextBox();
this.but_con = new Button();
this.groupBox1 = new GroupBox();
this.groupBox2 = new GroupBox();
this.log = new RichTextBox();
this.statusStrip1 = new StatusStrip();
this.toolStripStatusLabel1 = new ToolStripStatusLabel();
this.menuStrip1 = new MenuStrip();
this.文件ToolStripMenuItem = new ToolStripMenuItem();
this.帮助ToolStripMenuItem = new ToolStripMenuItem();
this.关于ToolStripMenuItem = new ToolStripMenuItem();
this.rich_send = new RichTextBox();
this.but_send = new Button();
this.textBox4 = new TextBox();
this.button4 = new Button();
this.button3 = new Button();
this.checkBox1 = new CheckBox();
this.groupBox3 = new GroupBox();
this.checkBox4 = new CheckBox();
this.checkBox3 = new CheckBox();
this.checkBox2 = new CheckBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.menuStrip1.SuspendLayout();
this.groupBox3.SuspendLayout();
base.SuspendLayout();
this.com_name.FormattingEnabled = true;
this.com_name.Location = new Point(59, 20);
this.com_name.Name = "com_name";
this.com_name.Size = new Size(97, 20);
this.com_name.TabIndex = 0;
this.com_name.SelectedIndexChanged = this.com_name_SelectedIndexChanged;
this.but_scan.Location = new Point(12, 119);
this.but_scan.Name = "but_scan";
this.but_scan.Size = new Size(66, 28);
this.but_scan.TabIndex = 1;
this.but_scan.Text = "扫描";
this.but_scan.UseVisualStyleBackColor = true;
this.but_scan.Click = this.but_scan_Click;
this.label1.AutoSize = true;
this.label1.Location = new Point(12, 23);
this.label1.Name = "label1";
this.label1.Size = new Size(41, 12);
this.label1.TabIndex = 2;
this.label1.Text = "名称:";
this.label2.AutoSize = true;
this.label2.Location = new Point(12, 55);
this.label2.Name = "label2";
this.label2.Size = new Size(35, 12);
this.label2.TabIndex = 4;
this.label2.Text = "VID:";
this.label3.AutoSize = true;
this.label3.Location = new Point(12, 88);
this.label3.Name = "label3";
this.label3.Size = new Size(35, 12);
this.label3.TabIndex = 6;
this.label3.Text = "PID:";
this.text_pid.Location = new Point(59, 79);
this.text_pid.Name = "text_pid";
this.text_pid.Size = new Size(97, 21);
this.text_pid.TabIndex = 7;
this.text_vid.Location = new Point(59, 52);
this.text_vid.Name = "text_vid";
this.text_vid.Size = new Size(97, 21);
this.text_vid.TabIndex = 8;
this.but_con.Location = new Point(84, 119);
this.but_con.Name = "but_con";
this.but_con.Size = new Size(72, 28);
this.but_con.TabIndex = 9;
this.but_con.Text = "连接";
this.but_con.UseVisualStyleBackColor = true;
this.but_con.Click = this.but_con_Click;
this.groupBox1.Controls.Add(this.text_vid);
this.groupBox1.Controls.Add(this.com_name);
this.groupBox1.Controls.Add(this.but_con);
this.groupBox1.Controls.Add(this.but_scan);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.text_pid);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Location = new Point(0, 28);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new Size(169, 168);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "设置";
this.groupBox2.Controls.Add(this.log);
this.groupBox2.Location = new Point(175, 28);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new Size(852, 315);
this.groupBox2.TabIndex = 11;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "接收";
this.log.Location = new Point(6, 13);
this.log.Name = "log";
this.log.Size = new Size(840, 296);
this.log.TabIndex = 0;
this.log.Text = "";
this.log.TextChanged = this.log_TextChanged;
this.statusStrip1.Items.AddRange(new ToolStripItem[1]
{
this.toolStripStatusLabel1
});
this.statusStrip1.Location = new Point(0, 449);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new Size(1067, 22);
this.statusStrip1.TabIndex = 12;
this.statusStrip1.Text = "statusStrip1";
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new Size(41, 17);
this.toolStripStatusLabel1.Text = "就绪...";
this.menuStrip1.Items.AddRange(new ToolStripItem[3]
{
this.文件ToolStripMenuItem,
this.帮助ToolStripMenuItem,
this.关于ToolStripMenuItem
});
this.menuStrip1.Location = new Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new Size(1067, 25);
this.menuStrip1.TabIndex = 13;
this.menuStrip1.Text = "menuStrip1";
this.menuStrip1.ItemClicked = this.menuStrip1_ItemClicked;
this.文件ToolStripMenuItem.Name = "文件ToolStripMenuItem";
this.文件ToolStripMenuItem.Size = new Size(44, 21);
this.文件ToolStripMenuItem.Text = "文件";
this.帮助ToolStripMenuItem.Name = "帮助ToolStripMenuItem";
this.帮助ToolStripMenuItem.Size = new Size(44, 21);
this.帮助ToolStripMenuItem.Text = "帮助";
this.关于ToolStripMenuItem.Name = "关于ToolStripMenuItem";
this.关于ToolStripMenuItem.Size = new Size(44, 21);
this.关于ToolStripMenuItem.Text = "关于";
this.rich_send.Location = new Point(175, 349);
this.rich_send.Name = "rich_send";
this.rich_send.Size = new Size(742, 66);
this.rich_send.TabIndex = 1;
this.rich_send.Text = "61 03 00 00 00 F0 01 03 00 00 00 00 00 00 00 00 00 00 01 9A 6D";
this.but_send.Location = new Point(923, 349);
this.but_send.Name = "but_send";
this.but_send.Size = new Size(104, 66);
this.but_send.TabIndex = 10;
this.but_send.Text = "发送";
this.but_send.UseVisualStyleBackColor = true;
this.but_send.Click = this.but_send_Click;
this.textBox4.Location = new Point(84, 123);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new Size(60, 21);
this.textBox4.TabIndex = 7;
this.button4.Location = new Point(6, 169);
this.button4.Name = "button4";
this.button4.Size = new Size(67, 28);
this.button4.TabIndex = 1;
this.button4.Text = "清空发送";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click = this.button4_Click;
this.button3.Location = new Point(79, 169);
this.button3.Name = "button3";
this.button3.Size = new Size(73, 28);
this.button3.TabIndex = 9;
this.button3.Text = "清空接收";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click = this.button3_Click;
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new Point(7, 32);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new Size(72, 16);
this.checkBox1.TabIndex = 10;
this.checkBox1.Text = "时间显示";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged = this.checkBox1_CheckedChanged;
this.groupBox3.Controls.Add(this.checkBox4);
this.groupBox3.Controls.Add(this.checkBox3);
this.groupBox3.Controls.Add(this.checkBox2);
this.groupBox3.Controls.Add(this.checkBox1);
this.groupBox3.Controls.Add(this.button3);
this.groupBox3.Controls.Add(this.button4);
this.groupBox3.Controls.Add(this.textBox4);
this.groupBox3.Location = new Point(0, 202);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new Size(169, 213);
this.groupBox3.TabIndex = 14;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "设置";
this.checkBox4.AutoSize = true;
this.checkBox4.Location = new Point(6, 125);
this.checkBox4.Name = "checkBox4";
this.checkBox4.Size = new Size(72, 16);
this.checkBox4.TabIndex = 13;
this.checkBox4.Text = "连续发送";
this.checkBox4.UseVisualStyleBackColor = true;
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new Point(7, 95);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new Size(66, 16);
this.checkBox3.TabIndex = 12;
this.checkBox3.Text = "HEX发送";
this.checkBox3.UseVisualStyleBackColor = true;
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new Point(6, 63);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new Size(66, 16);
this.checkBox2.TabIndex = 11;
this.checkBox2.Text = "HEX接收";
this.checkBox2.UseVisualStyleBackColor = true;
this.checkBox2.CheckedChanged = this.checkBox2_CheckedChanged;
base.AutoScaleDimensions = new SizeF(6f, 12f);
base.AutoScaleMode = AutoScaleMode.Font;
base.ClientSize = new Size(1067, 471);
base.Controls.Add(this.groupBox3);
base.Controls.Add(this.but_send);
base.Controls.Add(this.rich_send);
base.Controls.Add(this.statusStrip1);
base.Controls.Add(this.menuStrip1);
base.Controls.Add(this.groupBox2);
base.Controls.Add(this.groupBox1);
base.MainMenuStrip = this.menuStrip1;
base.Name = "Form1";
this.Text = "USB调试助手";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
base.ResumeLayout(false);
base.PerformLayout();
}
public Form1()
{
this.InitializeComponent();
this.devices = HIDDevice.getConnectedDevices();
for (int i = 0; i < this.devices.Length; i )
{
this.com_name.Items.Add(this.devices[i].product);
}
}
public void DebugTrack(string str, Color color)
{
if (this.log.InvokeRequired)
{
Action<string, Color> method = this.DebugTrack;
this.log.Invoke(method, str, color);
}
else
{
if (this.log.TextLength > 2147482647)
{
this.log.Clear();
}
this.log.SelectionStart = this.log.Text.Length;
this.log.SelectionColor = color;
if (str != ".")
{
this.log.AppendText(DateTime.Now.ToString("【yyyy-MM-dd HH:mm:ss】 ") str "\r\n");
}
else
{
this.log.AppendText(str);
}
this.log.ScrollToCaret();
}
}
private void but_scan_Click(object sender, EventArgs e)
{
string empty = string.Empty;
this.com_name.Items.Clear();
this.devices = HIDDevice.getConnectedDevices();
DateTime now;
for (int i = 0; i < this.devices.Length; i )
{
this.com_name.Items.Add(this.devices[i].product);
RichTextBox richTextBox = this.log;
now = DateTime.Now;
richTextBox.AppendText(now.ToString("【yyyy-MM-dd HH:mm:ss】 ") this.devices[i].product "\r\n");
}
RichTextBox richTextBox2 = this.log;
now = DateTime.Now;
richTextBox2.AppendText(now.ToString("【yyyy-MM-dd HH:mm:ss】 ") "扫描完毕,请选取设备,再连接\r\n");
}
private void but_con_Click(object sender, EventArgs e)
{
DateTime now;
if (this.com_name.SelectedIndex == -1)
{
RichTextBox richTextBox = this.log;
now = DateTime.Now;
richTextBox.AppendText(now.ToString("【yyyy-MM-dd HH:mm:ss】 ") "尚未选取设备\r\n");
}
else
{
this.text_pid.Text = this.devices[this.com_name.SelectedIndex].PID.ToString();
this.text_vid.Text = this.devices[this.com_name.SelectedIndex].VID.ToString();
this.device = new HIDDevice(this.devices[this.com_name.SelectedIndex]);
this.device.dataReceived = this.device_dataReceived;
if (this.device.deviceConnected)
{
RichTextBox richTextBox2 = this.log;
now = DateTime.Now;
richTextBox2.AppendText(now.ToString("【yyyy-MM-dd HH:mm:ss】 ") "设备连接成功!\r\n");
}
else
{
RichTextBox richTextBox3 = this.log;
now = DateTime.Now;
richTextBox3.AppendText(now.ToString("【yyyy-MM-dd HH:mm:ss】 ") "设备连接失败!重新扫描端口\r\n");
}
}
}
private byte[] strToHexBytes(string strToConv)
{
if (strToConv.Length % 2 != 0)
{
strToConv = strToConv.Insert(strToConv.Length - 1, "0");
}
byte[] array = new byte[strToConv.Length / 2];
for (int i = 0; i < strToConv.Length / 2; i )
{
array[i] = Convert.ToByte(strToConv.Substring(2 * i, 2), 16);
}
return array;
}
private void com_name_SelectedIndexChanged(object sender, EventArgs e)
{
this.text_pid.Text = this.devices[this.com_name.SelectedIndex].PID.ToString();
this.text_vid.Text = this.devices[this.com_name.SelectedIndex].VID.ToString();
}
private void device_dataReceived(byte[] message)
{
string text = string.Empty;
string empty = string.Empty;
byte[] array = new byte[128];
int num = message.Length - 1;
Array.Copy(message, 1, array, 0, message.Length - 1);
for (int i = 0; i < num; i )
{
empty = Convert.ToString(array[i], 16);
empty = empty.PadLeft(2, '0');
text = empty.ToUpper();
text = " ";
}
this.DebugTrack(text, Color.Blue);
}
private void but_send_Click(object sender, EventArgs e)
{
string text = this.rich_send.Text.ToString();
text = text.Replace(" ", "");
byte[] data = this.strToHexBytes(text);
this.device.write(data);
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
this.log.Text = "";
}
private void log_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
this.rich_send.Text = "";
}
}
}