LibusbDotNet Read/Write(Polling)

LibusbDotNet Read/Write(Polling)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using LibUsbDotNet;
using LibUsbDotNet.Main;

namespace LibUsbDotNet_Learn
{
    class Program
    {
        public static UsbDevice MyUsbDevice;
        //设置USB的Vendor Product ID
        public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x1f3b, 0x1000);


        static void Main(string[] args)
        {
            //保存异常数据
            ErrorCode ec = ErrorCode.None;
            try
            {
                //找到并打开USB设备
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                if(MyUsbDevice==null)
                {
                    throw new Exception("Device Not Found");
                }
                //如果设备打开and ready

                //libusb-win32是"whole"USB device,为IUsbDevice interface,不是(WinUSB),则变量
                //wholeUSBDevice变量为null,是device interface,不需要configuration 和 interface
                //as is 判断两个变量是否相等,is 返回TRUE/FALSE as 相同返回结果,不同返回null
                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if(!ReferenceEquals(wholeUsbDevice,null))
                {
                    //这是个"whole"USB device,使用前选择configuration interface
                    //选中配置1
                    wholeUsbDevice.SetConfiguration(1);
                    wholeUsbDevice.ClaimInterface(0);
                    //
                }
                
                //打开并读取read endpoint1
                UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
                UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

                // Remove the exepath/startup filename text from the begining of the CommandLine.
                string cmdline = Regex.Replace(Environment.CommandLine, "^\".+?\"^.*? |^.*? ", "", RegexOptions.Singleline);

                if (!String.IsNullOrEmpty(cmdline))
                {
                    int bytesWritten;
                    ec = writer.Write(Encoding.Default.GetBytes(cmdline), 2000, out bytesWritten);
                    if (ec != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);

                    byte[] readBuffer = new byte[1024];

                    //如果5秒内设备没有发生数据,发生timeout error(ec=IoTimedOut)
                    while (ec == ErrorCode.None)
                    {
                        int bytesRead;
                        ec = reader.Read(readBuffer, 100, out bytesRead);

                        if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes", ec));
                        Console.WriteLine("{0} bytes read", bytesRead);

                        //将结果输出到控制台上
                        Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
                    }
                    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
            {
                if(MyUsbDevice!=null)
                {
                    if (MyUsbDevice.IsOpen)
                    {
                        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                        if(!ReferenceEquals(wholeUsbDevice,null))
                        {
                            //释放interface 0
                            wholeUsbDevice.ReleaseInterface(0);
                        }
                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;
                    //释放usb资源
                    UsbDevice.Exit();
                }

                Console.ReadKey();
            }
        }
    }
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值