#c# #winform #上位机 #UsbDevice #LibUsbDotNet
您好,各位朋友们,目前我在编写调试Win USB Device的上位机是遇到了几个问题,想问一下。

这是我想做成的界面。
我在网上查找资料调用的LibUsbDotNet库并添加在项目的一个类Readpolling.cs中,代码如下
using LibUsbDotNet;
using LibUsbDotNet.Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace boxed
{
internal class Readpolling
{
public static UsbDevice MyUsbDevice; //公共静态定义的类名和对象
#region SET YOUR USB Vendor and Product ID! 设置您的usb供应商和产品ID // #region是分块预处理命令,它主要用于编译代码的分段,在编译时会被自动删除,必须以 #endregion终止
public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(1234, 1); //公共静态定义类名和对象并且赋值
#endregion
//c#程序入口地址(同c的main()),访问修饰符(访问权限) + 关键字(静态) + 返回类型(不需要返回值) + 方法名(主程序) + string类(从控制台收参数) + 字符串()
static void Main(string[] args)
{
//保存异常数据
ErrorCode ec = ErrorCode.None;
try
{
//找到并打开USB设备
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
//如果设备已经打开准备好
if (MyUsbDevice == null)
{
//若引发新的异常对象,显示设备没有被发现
throw new Exception("Device Not Found");
}
// 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 = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null)) //比较两个引用类型的对象是否是对于同一个对象的引用
{
//这是一个“whole”USB device,使用前选择configuration interface
//选择配置
wholeUsbDevice.SetConfiguration(1);
//接口
wholeUsbDevice.ClaimInterface(0);
}
//打开并读取endpoint1
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
byte[] readBuffer = new byte[1024]; //在内存中开辟块缓冲区,首地址readBuffer
while (ec == ErrorCode.None) //一直在读取
{
int bytesRead;
//如果5秒内没有发生数据,发生timeout error(ec = IoTimeOut)
ec = reader.Read(readBuffer, 5000, out bytesRead);
if (bytesRead == 0)
//引发新异常,将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项
throw new Exception(string.Format("{0}:No more bytes!", ec));
Console.WriteLine("{0} bytes read", bytesRead);
//将结果输出到控制台上,把当前编码字符串按指定编码方式编码为其他编码
Console.WriteLine(Encoding.Default.GetString(readBuffer, 0, bytesRead));
Console.WriteLine("\r\nDone!\r\n");
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message); //空字符串
}
//读取数据后执行
finally
{
if (MyUsbDevice != null)
{
if (!MyUsbDevice.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 = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
//释放interface 0
wholeUsbDevice.ReleaseInterface(0);
}
MyUsbDevice.Close();
}
MyUsbDevice = null;
//释放usb资源
UsbDevice.Exit();
}
Console.ReadKey();
}
}
}
}
1.我不确定网上这个类的资料是否可以扫描到我的WinUSB Device设备。
2.在我的主体设计中,我不清楚如何通过serialport工具来调用ReadPolling类以及扫描出WinUSB Device设备。
@Java小白白又白 @根号五 @IT小生lkc @不良高须
希望获得朋友们的解答,感谢!