private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) { while (port.BytesToRead >= 4 && port.BytesToRead % 4 == 0) { byte[] packet = new byte[4]; int read = port.Read(packet, 0, 4); InterfaceDebugger.DataReceived(packet); switch (packet[0]) { case 0x00: { break; } case 0x01: { IVirtualDevice device = GetDevice(packet); bool found = false; for (int i = 0; i < Subdevices.Count; i++) { if (Subdevices[i].Id == packet[2]) //if the device already exists, update it { Subdevices[i] = device; found = true; } } //if the device doesn't already exist, add it if (!found) { devices.Add(device); } //sort the list on the device ID devices = devices.OrderBy(o => o.Id).ToList(); DeviceAttached?.Invoke(this, new VirtualDeviceEventArgs(device)); break; } case 0x02: { //device sends a report if (packet[1] > devices.Count - 1) { break; } devices[packet[1]].ReceivePacket(packet); break; } } } }
public void SendPacket(byte[] packet) { InterfaceDebugger.DataSent(packet); port.Write(packet, 0, 4); }