当我发现 USB 设备在 VCP 而不是 USB-HID 中通信时,这是个好消息,因为串行连接很容易理解。
如果设备在VCP(虚拟通信端口)中运行,那么它就像使用System.IO.Ports.SerialPort 类型一样简单。您将需要了解有关设备的一些基本信息,其中大部分可以从 Windows 管理(设备管理器)中收集。像这样构建之后:
SerialPort port = new SerialPort(portNo, baudRate, parity, dataBits, stopBits);
你may or may not需要设置一些额外的标志,例如Request to send (RTS)和Data Terminal Ready (DTR)
port.RtsEnable = true;
port.DtrEnable = true;
然后,打开端口。
port.Open();
要监听,您可以将事件处理程序附加到port.DataReceived,然后使用port.Read(byte[] buffer, int offset, int count)
port.DataReceived += (sender, e) =>
{
byte[] buffer = new byte[port.BytesToRead];
port.Read(buffer,0,port.BytesToRead);
// Do something with buffer
};
要发送,您可以使用port.Write(byte[] buffer, int offset, int count)