protected override void bus_DataReceived(object sender, SerialDataReceivedEventArgs e) { ISerialPort port = (ISerialPort)sender; if (port.AvailableBytes == 0) { Logger.Trace("Available bytes lost! " + port.ToString()); return; } lock (bufferSync) { byte[] data = port.ReadAvailable(); if (messageBufferLength + data.Length > messageBuffer.Length) { Logger.Trace("Buffer overflow. Extending it. " + port.ToString()); byte[] newBuffer = new byte[messageBuffer.Length * 2]; Array.Copy(messageBuffer, newBuffer, messageBufferLength); messageBuffer = newBuffer; } if (data.Length == 1) { messageBuffer[messageBufferLength++] = data[0]; } else { Array.Copy(data, 0, messageBuffer, messageBufferLength, data.Length); messageBufferLength += data.Length; } while (messageBufferLength >= DBusMessage.PacketLengthMin || messageBufferLength >= DS2Message.PacketLengthMin) { Message dBusMessage = DBusMessage.TryCreate(messageBuffer, messageBufferLength); Message ds2Message = DS2Message.TryCreate(messageBuffer, messageBufferLength); Message m = ds2Message ?? dBusMessage; if (m == null) { if (!DBusMessage.CanStartWith(messageBuffer, messageBufferLength)) { Logger.Trace("Buffer skip: non-dBus data detected: " + messageBuffer[0].ToHex()); SkipBuffer(1); continue; } return; } ProcessMessage(m); //#if DEBUG //m.PerformanceInfo.TimeEnqueued = DateTime.Now; //#endif //messageReadQueue.Enqueue(m); SkipBuffer(m.PacketLength); } lastMessage = DateTime.Now; } }
protected override Message TryCreate(byte[] data) { return(DBusMessage.TryCreate(data)); }