コード例 #1
0
ファイル: CBusCALCommand.cs プロジェクト: ardy30/CBus4Net
        public CBusCALCommand(CBusProtcol.CBusConfigurationParameters Parameter, byte ParameterValue, bool IncludeChecksum)
        {
            this.IncludeChecksum = IncludeChecksum;
            this.Parameter       = Parameter;
            rawCommand           = new byte[] { 0xA3, 0x00, 0x00, 0x00, 0x00 };

            rawCommand[1] = Convert.ToByte(Parameter);
            rawCommand[3] = ParameterValue;
            rawCommand[4] = CBusSALCommand.CalculateChecksum(rawCommand, rawCommand.Length - 1);
        }
コード例 #2
0
        /// <summary>
        /// Expects CBus message bytes with ETX and STX stripped and check sum already verified
        /// </summary>
        /// <param name="CommandBytes"></param>
        /// <param name="Command"></param>
        /// <returns></returns>
        public bool TryParseCommand(byte[] CommandBytes, int CommandLength, bool IsMonitoredSAL, bool IsShortFormMessage, out CBusSALCommand Command)
        {
            int  dataPointer;
            byte CBusApplicationAddress;

            if (CBusSALCommand.TryParseApplicationId(CommandBytes, CommandLength, IsMonitoredSAL, IsShortFormMessage, out dataPointer, out CBusApplicationAddress))
            {
                //var appAddress = IsShortFormMessage ? CommandBytes[1] : CommandBytes[2];

                var maping = AddressMap.SingleOrDefault(m => m.Address == CBusApplicationAddress);
                if (maping != null)
                {
                    switch (maping.ApplicationType)
                    {
                    case CBusProtcol.ApplicationTypes.LIGHTING:
                    {
                        CBusLightingCommand lightingCommand;
                        if (CBusLightingCommand.TryParseReply(CommandBytes, CommandLength, IsShortFormMessage, CBusApplicationAddress, ref dataPointer, out lightingCommand))
                        {
                            Command = lightingCommand;
                            return(true);
                        }
                        break;
                    }

                    case CBusProtcol.ApplicationTypes.TRIGGER:
                    {
                        CBusTriggerCommand triggerCommand;
                        if (CBusTriggerCommand.TryParse(CommandBytes, CommandLength, IsShortFormMessage, CBusApplicationAddress, ref dataPointer, out triggerCommand))
                        {
                            Command = triggerCommand;
                            return(true);
                        }
                        break;
                    }

                    default:
                        break;
                    }
                }
                else
                {
                    //Valid message but unknown application type
                }
            }
            else
            {
                //Could not get application address
            }

            Command = null;
            return(false);
        }
コード例 #3
0
ファイル: CBus.cs プロジェクト: ardy30/CBus4Net
        bool ProcessReceivedCharacter(char currentCharacter, CBusStateMachine state)
        {
            switch (state.ParserState)
            {
            case CBusStateMachine.StateMachineState.MESSAGE_COMPLETE:
            case CBusStateMachine.StateMachineState.NONE:
            {
                if (currentCharacter != STX_CHAR)
                {
                    //Serial interface seems to strip leading '\' character
                    //so just assume that the STX was received and that this is the first data character.
                    //So the next received character will be data 2
                    if (IsValidCharacter(currentCharacter))
                    {
                        state.ParserState = CBusStateMachine.StateMachineState.DATA2;
                    }
                    else
                    {
                        state.ParserState = CBusStateMachine.StateMachineState.ACK_RECEIVED;
                    }
                }
                else
                {
                    state.ParserState = CBusStateMachine.StateMachineState.STX_FOUND;
                }
                break;
            }

            case CBusStateMachine.StateMachineState.ACK_RECEIVED:
            {
                switch (currentCharacter)
                {
                case ACK_CHAR:
                    //Store the ack character...
                    state.ACK_Character = state.PreviousRxCharacter;
                    state.MessageType   = CBusMessageType.ACK;
                    return(true);

                //NAK Responses
                case NAK:
                    state.MessageType = CBusMessageType.NAK;
                    return(true);

                case NAK_CORRUPTED:
                    state.MessageType = CBusMessageType.NAK_CORRUPTED;
                    return(true);

                case NAK_NO_CLOCK:
                    state.MessageType = CBusMessageType.NAK_NO_CLOCK;
                    return(true);

                case NAK_MSG_TOO_LONG:
                    state.MessageType = CBusMessageType.NAK_MESSAGE_TOO_LONG;
                    return(true);

                default:
                    //Unexpected character
                    state.Reset();
                    break;
                }
                break;
            }

            case CBusStateMachine.StateMachineState.DATA1:
            {
                if (currentCharacter == ETX_CHAR_1)
                {
                    state.ParserState        = CBusStateMachine.StateMachineState.MESSAGE_COMPLETE;
                    state.CalculatedChecksum = CBusSALCommand.CalculateChecksum(state.CommandBytes, state.CommandLength);
                    state.MessageType        = CBusMessageType.SAL_MESSAGE_RECEIVED;
                    return(true);
                }
                else
                {
                    if (IsValidCharacter(currentCharacter))
                    {
                        state.ParserState = CBusStateMachine.StateMachineState.DATA2;
                    }
                    else
                    {
                        state.Reset();
                    }
                }
                break;
            }

            case CBusStateMachine.StateMachineState.DATA2:
            {
                var hex = string.Format("{0}{1}", state.PreviousRxCharacter, currentCharacter);

                byte data;
                if (byte.TryParse(hex, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out data))
                {
                    state.CommandBytes[state.CommandLength++] = data;
                    state.ParserState = CBusStateMachine.StateMachineState.DATA1;
                }
                else
                {
                    state.Reset();
                }

                break;
            }
            }

            if (state.ParserState != CBusStateMachine.StateMachineState.NONE)
            {
                state.MessageType = CBusMessageType.MESSAGE_PENDING;
            }

            return(false);
        }