USB デバイスが USB-HID ではなく VCP で通信することがわかったときは、シリアル接続が理解しやすいため、朗報です。
デバイスが (Virtual Com Port) で動作している場合、タイプVCP
を使用するのと同じくらい簡単です。System.IO.Ports.SerialPort
デバイスに関するいくつかの基本的な情報を知っておく必要があります。そのほとんどは、Windows の管理 (デバイス マネージャー) から収集できます。そのように構築した後:
SerialPort port = new SerialPort(portNo, baudRate, parity, dataBits, stopBits);
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)