我不想阅读串口或其他可能的简单快捷方式。我想知道如何使用C#读取笔记本电脑中的USB端口。 无论您是建议网站还是解释流程,我都将非常感谢您的帮助
答案 0 :(得分:3)
答案 1 :(得分:0)
这里的假设是您想在Windows上读取数据。这是一个代码示例:
public override async Task<byte[]> ReadAsync()
{
return await Task.Run(() =>
{
var bytes = new byte[ReadBufferSize];
//TODO: Allow for different interfaces and pipes...
var isSuccess = WinUsbApiCalls.WinUsb_ReadPipe(_DefaultUsbInterface.Handle, _DefaultUsbInterface.ReadPipe.WINUSB_PIPE_INFORMATION.PipeId, bytes, ReadBufferSize, out var bytesRead, IntPtr.Zero);
HandleError(isSuccess, "Couldn't read data");
Tracer?.Trace(false, bytes);
return bytes;
});
}
[DllImport("winusb.dll", SetLastError = true)]
public static extern bool WinUsb_ReadPipe(SafeFileHandle InterfaceHandle, byte PipeID, byte[] Buffer, uint BufferLength, out uint LengthTransferred, IntPtr Overlapped);
所有代码和示例(包括其他平台)都可以在这里找到:https://github.com/MelbourneDeveloper/Device.Net