C# LibUsbDotNet库的写入和读取数据

下载安装

下载并安装 LibUsbDotNet

查看所需打开设备的PID和VID

using System;

using LibUsbDotNet;
using LibUsbDotNet.Main;

namespace LibUSBDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            GetUSBInfo();
            Console.ReadLine();
        }

        public static void GetUSBInfo()
        {
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;
            Console.WriteLine("Found {0} devices", allDevices.Count);
            foreach (UsbRegistry usb in allDevices)
            {
                Console.WriteLine("----------------");
                Console.WriteLine($"Device info: {usb.Device.Info.ProductString}");
                Console.WriteLine($"Pid: { usb.Pid}, VID: {usb.Vid}");
            }
            Console.WriteLine(allDevices.Count);
        }
    }
}

LibUSBDotNet写和读的两种方式

第一种
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using LibUsbDotNet;
using LibUsbDotNet.Main;

namespace LibUSBDemo
{
    class LibUSB
    {
        private UsbDevice usbDevice;
        private UsbEndpointReader epReader;
        private UsbEndpointWriter epWriter;

        public void Open(int vid, int pid)
        {
            UsbDeviceFinder usbFinder = new UsbDeviceFinder(vid, pid);
            usbDevice = UsbDevice.OpenUsbDevice(usbFinder);


            if (usbDevice == null)
            {
                // 多次尝试连接USB设备
                int count = 0;
                while (count < 10 && usbDevice == null)
                {
                    Thread.Sleep(100);
                    usbDevice = UsbDevice.OpenUsbDevice(usbFinder);
                    count++;
                }
            }

            if (usbDevice == null)
            {
                Console.WriteLine("打开USB设备失败");
            }
            else
            {
                // If this is a "whole" usb device (libusb-win32, linux libusb)
                // it will have an IUsbDevice interface. If not (WinUSB) the 
                // variable will be null indicating this is an interface of a 
                // device.
                IUsbDevice wholeUSBDevice = usbDevice as IUsbDevice;
                if (wholeUSBDevice != null)
                {
                    // This is a "whole" USB device. Before it can be used, 
                    // the desired configuration and interface must be selected.

                    // Select config #1
                    wholeUSBDevice.SetConfiguration(1);

                    // Claim interface #0.
                    wholeUSBDevice.ClaimInterface(0);
                }

                // open read endpoint 1.
                epReader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep01);

                // open write endpoint 1.
                epWriter = usbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
            }
        }


        public void Close()
        {
            if (IsOpen())
            {
                // If this is a "whole" usb device(libusb-win32, linux libusb-1.0)
                // it exposes an IUsbDevice interface. If not (WinUSB) the 
                // 'wholeUsbDevice' variable will be null indicating this is 
                // an interface of a device; it does not require or support 
                // configuration and interface selection.
                IUsbDevice wholeUsbDevice = usbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // Release interface #0.
                    wholeUsbDevice.ReleaseInterface(0);
                }

                usbDevice.Close();
                usbDevice = null;
            }

            // Free usb resource
            UsbDevice.Exit();
        }

        public bool IsOpen()
        {
            if (usbDevice == null)
            {
                return false;
            }

            return usbDevice.IsOpen;
        }

        public List<byte> SendCommand(string cmd)
        {
            List<byte> result = new List<byte>();
            int byteWrite;
            ErrorCode ec = epWriter.Write(Encoding.Default.GetBytes(cmd), 3000,out byteWrite);
            if(ec != ErrorCode.None)
            {
                Console.WriteLine($"Write Error, {UsbDevice.LastErrorString}");
                return null;
            }

            byte[] readBuffer = new byte[1024];
            int byteRead;

            while (ec == ErrorCode.None)
            {
                // If the device hasn't sent data in the last 1000 milliseconds,
                // a timeout error (ec = IoTimedOut) will occur. 
                ec = epReader.Read(readBuffer, 1000, out byteRead);
                // 只有当超时的时候才会有byteRead为0,也就是结束
                if(byteRead != 0)
                {
                    byte[] buffer = new byte[byteRead];
                    Array.Copy(readBuffer, buffer, byteRead);
                    result.AddRange(buffer);
                }
                else
                {
                    Console.WriteLine("读取数据结束");
                    return result;
                }
            }
            return null;
        }
    }
}

上述代码在SendCommand发送命令获取返回结果的时候,结束条件byteRead == 0 的时候也就是TimeOut的时候才会出现,这会造成时间浪费,因为超时时间一般来说设置的都会比较长。那有没有什么方式可以避免这种情况呢?可以使用异步的情况接受数据。

第二种
using System;
using System.Collections.Generic;
using System.Text;
using LibUsbDotNet;
using LibUsbDotNet.Main;

using System.Timers;

namespace LibUSBDemo
{
   class LibUSB
   {
       private UsbDevice usbDevice;
       private UsbEndpointReader epReader;
       private UsbEndpointWriter epWriter;
       private List<byte> recvData;

       public LibUSB()
       {
           recvData = new List<byte>();
       }

       public void Open(int vid, int pid)
       {
           UsbDeviceFinder usbFinder = new UsbDeviceFinder(vid, pid);
           usbDevice = UsbDevice.OpenUsbDevice(usbFinder);


           if (usbDevice == null)
           {
               // 多次尝试连接USB设备
               int count = 0;
               while (count < 10 && usbDevice == null)
               {
                   System.Threading.Thread.Sleep(100);
                   usbDevice = UsbDevice.OpenUsbDevice(usbFinder);
                   count++;
               }
           }

           if (usbDevice == null)
           {
               Console.WriteLine("打开USB设备失败");
           }
           else
           {
               // If this is a "whole" usb device (libusb-win32, linux libusb)
               // it will have an IUsbDevice interface. If not (WinUSB) the 
               // variable will be null indicating this is an interface of a 
               // device.
               IUsbDevice wholeUSBDevice = usbDevice as IUsbDevice;
               if (wholeUSBDevice != null)
               {
                   // This is a "whole" USB device. Before it can be used, 
                   // the desired configuration and interface must be selected.

                   // Select config #1
                   wholeUSBDevice.SetConfiguration(1);

                   // Claim interface #0.
                   wholeUSBDevice.ClaimInterface(0);
               }

               // open read endpoint 1.
               epReader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
               epReader.ReadBufferSize = 1024; // 默认是4096

               // 设置以下内容将不能使用Read()方法来读取返回值
               epReader.DataReceived += EpReader_DataReceived;
               epReader.DataReceivedEnabled = true;

               // open write endpoint 1.
               epWriter = usbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
           }
       }

       private void EpReader_DataReceived(object sender, EndpointDataEventArgs e)
       {
           byte[] byteRead = new byte[e.Count];
           Array.Copy(e.Buffer, byteRead, e.Count);
           recvData.AddRange(byteRead);
       }

       public void Close()
       {
           if (IsOpen())
           {
               // If this is a "whole" usb device(libusb-win32, linux libusb-1.0)
               // it exposes an IUsbDevice interface. If not (WinUSB) the 
               // 'wholeUsbDevice' variable will be null indicating this is 
               // an interface of a device; it does not require or support 
               // configuration and interface selection.
               IUsbDevice wholeUsbDevice = usbDevice as IUsbDevice;
               if (!ReferenceEquals(wholeUsbDevice, null))
               {
                   // Release interface #0.
                   wholeUsbDevice.ReleaseInterface(0);
               }

               usbDevice.Close();
               usbDevice = null;
           }

           // Free usb resource
           UsbDevice.Exit();
       }

       public bool IsOpen()
       {
           if (usbDevice == null)
           {
               return false;
           }

           return usbDevice.IsOpen;
       }

       public List<byte> SendCommand(string cmd)
       {
           List<byte> result = new List<byte>();
           int byteWrite;
           ErrorCode ec = epWriter.Write(Encoding.Default.GetBytes(cmd), 3000, out byteWrite);
           if (ec != ErrorCode.None)
           {
               Console.WriteLine($"Write Error, {UsbDevice.LastErrorString}");
               return null;
           }
           int recvDataLength = 0;
           int count = 0;
           bool isEnd = false;
           Timer timer = new Timer(10);
           timer.AutoReset = true;
           timer.Start();
           timer.Elapsed += delegate
           {
               if ((count >= 10 && recvData.Count == 0) || (recvData.Count == recvDataLength && recvData.Count != 0))
               {
                   timer.Stop();
                   timer = null;
                   isEnd = true;
               }
               recvDataLength = recvData.Count;
           };

           while (!isEnd) ;
           return recvData;
       }
   }
}

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要读取和写入XMP元数据,你可以使用Adobe XMP SDK。以下是一些简单的示例代码,帮助你入门: 读取XMP元数据: ```csharp using System; using Adobe.XMP; using Adobe.XMP.Files; class Program { static void Main(string[] args) { // 定义文件路径 string filePath = "example.jpg"; // 创建XmpFile对象 XmpFile xmpFile = new XmpFile(filePath, FileOpenMode.ReadOnly); // 获取XMP包 XmpPacketWrapper xmpPacket = xmpFile.GetXmpPacket(); // 获取XMP元数据 string xmpMetadata = xmpPacket.Serialize(XmpSerializationOptions.UseCompactFormat); // 输出XMP元数据 Console.WriteLine(xmpMetadata); } } ``` 写入XMP元数据: ```csharp using System; using Adobe.XMP; using Adobe.XMP.Files; class Program { static void Main(string[] args) { // 定义文件路径 string filePath = "example.jpg"; // 创建XmpFile对象 XmpFile xmpFile = new XmpFile(filePath, FileOpenMode.ReadWrite); // 获取XMP包 XmpPacketWrapper xmpPacket = xmpFile.GetXmpPacket(); // 创建XMP元数据 XmpMeta xmpMeta = XmpMetaFactory.Parse("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description rdf:about=\"\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"><dc:title>example title</dc:title></rdf:Description></rdf:RDF></x:xmpmeta>"); // 设置XMP元数据 xmpPacket.SetXmpMeta(xmpMeta); // 保存文件 xmpFile.Close(FileCloseOptions.WriteThrough); } } ``` 注意:在使用这些代码之前,你需要先下载和安装Adobe XMP SDK,并将其添加到你的项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值