基本信息
源码名称:USBHID通信
源码大小:0.19M
文件格式:.rar
开发语言:C#
更新时间:2020-12-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 3 元 
   源码介绍

USBHID通信




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace USBHIDControl
{
    public partial class Form1 : Form
    {
        USBHID usbHID = null;
        public Form1()
        {
            InitializeComponent();
            usbHID = new USBHID();

            foreach (string device in usbHID.GetDeviceList())
                list_UsbHID.Items.Add(device);
            usbHID.DataReceived  = usbHID_DataReceived;
            usbHID.DeviceRemoved  = usbHID_DeviceRemoved;
        }

        void usbHID_DeviceRemoved(object sender, EventArgs e)
        {
            report myRP = (report)e;
            if (InvokeRequired)
            {
                Invoke(new EventHandler(usbHID_DeviceRemoved), new object[] { sender, e });
            }
            else
            {
                tb_information.Text = "设备连接";
            }
        }

        void usbHID_DataReceived(object sender, EventArgs e)
        {
            report myRP = (report)e;
            if (InvokeRequired)
            {
                Invoke(new EventHandler(usbHID_DataReceived), new object[] { sender, e });
            }
            else
            {
                tb_information.Text  =  "\r\n"  USBHID.ByteToHexString(myRP.reportBuff);
            }
        }

        /// <summary>
        /// open USB HID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_connect_Click(object sender, EventArgs e)
        {
            if (list_UsbHID.SelectedItem == null) {
                tb_information.Text  = "\r\n vendorID和productID不能为空";
                return;
            }

            if (usbHID.OpenUSBHid(list_UsbHID.SelectedItem.ToString()))
                tb_information.Text  = "\r\n open success";
            else
                tb_information.Text  = "\r\n open fail";
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            tb_information.Text  ="\r\n w: "  usbHID.WriteUSBHID(tb_send.Text);
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            tb_information.Text = "";
        }
    }
}