protected bool IfReceived(SerialPortManager sp, ProtocolBase protocol, IOBuffer buffer, SynchronizationContext syncContext) { byte receivedByte; if (sp.ReadByte(out receivedByte)) { switch (step) { case 0: if (receivedByte == protocol.Header) { step++; buffer[counterOfTheReceivedBytes++] = receivedByte; } break; case 1: if ((receivedByte & 0x80) == 0x80) RestartReceiv(); else { buffer[counterOfTheReceivedBytes++] = receivedByte; if (counterOfTheReceivedBytes == buffer.Size) { if (protocol.UnPack(buffer)) { syncContext.Post(delegate { logger.Log(string.Format("Rx: {0}", buffer.ToString()), Category.Debug, Priority.None); }, null); return true; } else RestartReceiv(); } } break; } } return false; }
protected void Transmit(SerialPortManager sp, ProtocolBase protocol, SynchronizationContext syncContext) { var buffer = protocol.Pack(); syncContext.Post(delegate { logger.Log(string.Format("Tx: {0}", buffer.ToString()), Category.Debug, Priority.None); }, null); sp.Write(protocol.Pack()); }