private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte c;

            while (serialPort.BytesToRead > 0)
            {
                bool acceptByte = false;

                c = (byte)serialPort.ReadByte();

                switch (receiverState)
                {
                    case SerialReceiverStates.IDLE_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            receiverState = SerialReceiverStates.SOF_RX_STATE;
                            currentReceiverMessage.Clear();
                            checkSum = 0;
                        }
                        break;

                    case SerialReceiverStates.SOF_RX_STATE:
                        if (SOF[1] == c)
                        {
                            receiverState = SerialReceiverStates.DATA_RX_STATE;
                        }
                        else
                        {
                            receiverState = SerialReceiverStates.IDLE_RX_STATE;
                        }
                        break;

                    case SerialReceiverStates.DATA_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            receiverState = SerialReceiverStates.MAGIC_RX_STATE;
                        }
                        else
                        {
                            acceptByte = true;
                        }
                        break;

                    case SerialReceiverStates.MAGIC_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            receiverState = SerialReceiverStates.DATA_RX_STATE;
                            acceptByte = true;
                        }
                        else if (EOF[1] == c)
                        {
                            receiverState = SerialReceiverStates.EOF_RX_STATE;
                        }
                        else
                        {
                            receiverState = SerialReceiverStates.IDLE_RX_STATE;
                            Debug.Print("INVALID FRAME RECEIVED IN " + PortName);
                        }
                        break;

                    case SerialReceiverStates.EOF_RX_STATE:
                        if (checkSum == c)
                        {
                            receiverState = SerialReceiverStates.IDLE_RX_STATE;
                            InputMessage inputMessage = new InputMessage();
                            inputMessage.FromBinary(currentReceiverMessage.ToArray());

                            OnOperationReceived(inputMessage);
                        }
                        else
                        {
                            receiverState = SerialReceiverStates.IDLE_RX_STATE;
                            Debug.Print("INVALID CHECKSUM RECEIVED IN " + PortName);
                        }
                        break;

                    default:
                        break;
                }

                checkSum += c;

                if (acceptByte)
                {
                    currentReceiverMessage.Add(c);
                }
            }
        }
        private void OnOperationReceived(InputMessage oper)
        {
            if (ConnectionState == ConnectionStates.Identifying)
            {
                if (oper.Content.OpCode == Operation.OPCodes.MacReadResponse)
                {
                    this.NodeAddress = oper.Content.SourceAddress;
                    this.NodeType = this.NodeAddress == MasterAddress ? NodeTypes.Master : NodeTypes.Slave;
                    this.ConnectionState = ConnectionStates.Connected;
                }
            }

            if (OperationReceived != null)
                OperationReceived(this, oper);
        }