コード例 #1
0
        public void AddChar(byte newChar)
        {
            switch (uART_MsgComposer_State)
            {
            case UART_MsgComposer_State_t.WAITING_FOR_BC:
                if (newChar == 0xBC)
                {
                    actualFrame            = new FrameFormat();
                    actualFrame.Header     = 0xBC;
                    uART_MsgComposer_State = UART_MsgComposer_State_t.WAITING_FOR_MSG_COMPLETE;
                }
                break;

            case UART_MsgComposer_State_t.WAITING_FOR_MSG_COMPLETE:
                composeFrame(newChar);
                break;
            }
        }
コード例 #2
0
        private void composeFrame(byte newChar)
        {
            switch (messageIndexes_T)
            {
            case MessageIndexes_t.FRAMEVERSION:
                actualCRC = (byte)(actualCRC ^ newChar);

                if (newChar == 0x01)
                {
                    actualFrame.FrameVersion = 0x01;
                    messageIndexes_T++;
                }
                else
                {
                    /* caso di errore resetto la macchina a stati */
                    uART_MsgComposer_State = UART_MsgComposer_State_t.WAITING_FOR_BC;
                    messageIndexes_T       = MessageIndexes_t.FRAMEVERSION;
                }
                break;

            case MessageIndexes_t.MESSAGETYPE:
                actualCRC = (byte)(actualCRC ^ newChar);
                actualFrame.MessageType = newChar;
                messageIndexes_T++;
                break;

            /*
             * 00000000 01111010
             * 11111100 00000100
             * 11111100 01111110
             * */
            case MessageIndexes_t.COMMAND_MSB_NIBBLE:
                actualCRC           = (byte)(actualCRC ^ newChar);
                actualFrame.Command = 0;
                actualFrame.Command = (UInt16)(actualFrame.Command | newChar << 8);
                messageIndexes_T++;
                break;

            case MessageIndexes_t.COMMAND_LSB_NIBBLE:
                actualCRC           = (byte)(actualCRC ^ newChar);
                actualFrame.Command = (UInt16)(actualFrame.Command | newChar);
                messageIndexes_T++;
                break;

            case MessageIndexes_t.DATALENGTH:
                actualCRC = (byte)(actualCRC ^ newChar);
                actualFrame.DataLength = newChar;
                subBufferIndex         = 0;
                if (actualFrame.DataLength == 0)
                {
                    messageIndexes_T = MessageIndexes_t.CRC;
                }
                else
                {
                    messageIndexes_T++;
                }
                break;

            case MessageIndexes_t.OPTIONALDATA:
                actualCRC = (byte)(actualCRC ^ newChar);
                actualFrame.OptionalData[subBufferIndex++] = newChar;
                if (subBufferIndex == actualFrame.DataLength)
                {
                    messageIndexes_T++;
                }
                break;

            case MessageIndexes_t.CRC:
                /*
                 * 101110 ^
                 * 001010 =
                 * 100100
                 */
                actualCRC            = (byte)~actualCRC;
                actualFrame.IsCRC_ok = ((actualCRC ^ newChar) == 0);

                if (OnFrameReceived != null)
                {
                    OnFrameReceived(this, actualFrame);
                }

                /* Resetto le macchina a stati perchè ho finito */
                uART_MsgComposer_State = UART_MsgComposer_State_t.WAITING_FOR_BC;
                messageIndexes_T       = MessageIndexes_t.FRAMEVERSION;
                break;
            }
        }