实例介绍
【实例简介】
【实例截图】
【核心代码】
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 = "";
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论