• 大小: 2.27MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-09-26
  • 语言: 其他
  • 标签: lib  真实例子  

资源简介

结合实际项目通过libusb 读取电路板中的数据信息。

资源截图

代码片段和文件信息

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;
using System.Windows.Forms.DataVisualization.Charting;
using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.Info;
using System.Threading;
using System.Collections.objectModel;
using LibUsbDotNet.DeviceNotify;
using System.IO;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using USBHIDControl;

namespace alt
{
    public partial class Form1 : Form
    {
        private DateTime LastDataEventDate = DateTime.Now;
        private int pId = 0x5750;
        private int vId = 0x0483;
        private UsbDevice usbDevice = null;
        private UsbDeviceFinder usbFinder = null;
        public UsbEndpointWriter writer = null;
        public UsbEndpointReader reader = null;
        IDeviceNotifier deviceNotifier = LibUsbDotNet.DeviceNotify.DeviceNotifier.OpenDeviceNotifier();
        delegate void SetTextCallback(string text);//安全线程访问txtReadInt的值
        delegate void SetUsbCallback(byte[] b);
        public Form1()
        {
            InitializeComponent();
        }
 
  
        
      
        /**
         *  往设备发送信息
         * */
        private void func_writeToRead(string msg)
        {

            ErrorCode ec = ErrorCode.None;

            try
            {
                if (usbDevice == null)
                {
                    FindAndOpenUSB();
                }
                //打开并读取read endpoint1
                UsbEndpointReader reader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
                UsbEndpointWriter writer = usbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

                if (!String.IsNullOrEmpty(msg))
                {
                    reader.DataReceived += (OnRxEndPointData);
                    reader.DataReceivedEnabled = true;

                    int bytesWritten;
                    ec = writer.Write(Encoding.Default.GetBytes(msg) 2000 out bytesWritten);
                    if (ec != ErrorCode.None) { showMessage(“读取失败,重新插拔设备!“); }

                    LastDataEventDate = DateTime.Now;
                    //如果长时间内为收到数据,则结束
                    while ((DateTime.Now - LastDataEventDate).TotalMilliseconds < 100)
                    {

                    }

                    reader.DataReceivedEnabled = false;
                    reader.DataReceived -= (OnRxEndPointData);

                    Console.WriteLine(“\r\n Done! \r\n“);
                }
                else
                    throw new Exception(“Nothing to do.“);

            }
            catch (System.Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine((ec != ErrorCode.None ? ec + “:“ : string.Empty) + ex.Message);
            }
            //读取数据后执行
            finally
            {

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6872  2018-10-12 08:52  Form1.cs

     文件     150016  2018-09-12 15:27  LibUsbDotNet.dll

     文件    2323232  2018-09-12 15:20  LibUsbDotNet_Setup.2.2.8.rar

     文件        178  2018-10-12 08:54  说明.txt

----------- ---------  ---------- -----  ----

              2480298                    4


评论

共有 条评论