Ejemplo n.º 1
0
        private void InternalMessageProc()
        {
            int          bytesToRead;
            int          i;
            IFlMessage   parsedMessage;
            FlParseState parseState;

            byte[] data = new byte[128];

            _logger.Debug("InternalMessageProc started");

            while (_messageLoop == true)
            {
                bytesToRead = _serialPort.BytesToRead;
                if (bytesToRead > 0)
                {
                    _serialPort.Read(data, 0, bytesToRead);
                    for (i = 0; i < bytesToRead; i++)
                    {
                        parseState = _parser.ParseResponseEvent(data[i], out parsedMessage);
                        if (parseState == FlParseState.ParseOk)
                        {
                            lock (_generalLock)
                            {
                                switch (parsedMessage.MessageCategory)
                                {
                                case FlMessageCategory.Response:
                                    int commandCount = 0;
                                    lock (_generalLock)
                                    {
                                        commandCount = _commandQ.Count;
                                    }

                                    if (commandCount > 0)
                                    {
                                        FlTxtMessageResponse response = (FlTxtMessageResponse)parsedMessage;
                                        _responseQ.Add(response);
                                    }
                                    else
                                    {
                                        _logger.Debug("Timeout response received");
                                    }
                                    break;

                                case FlMessageCategory.Event:
                                    FlTxtMessageEvent evt = (FlTxtMessageEvent)parsedMessage;
                                    OnEventReceived?.Invoke(this, evt);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            _logger.Debug("InternalMessageProc stopped");
        }
Ejemplo n.º 2
0
        public FlParseState ParseResponseEvent(byte data, out IFlMessage message)
        {
            FlParseState ret = FlParseState.Parsing;

            message = null;
            if (_fullPacketLength < FlConstant.FL_TXT_MSG_MAX_LENGTH)
            {
                _fullPacket[_fullPacketLength++] = data;
            }

            switch (_receiveState)
            {
            case ReceiveState.MessageId:
                if (IsMsgIdChar(data) == true)
                {
                    _buf[_bufPos++] = data;
                    if (_bufPos > FlConstant.TXT_MSG_ID_MAX_LEN)
                    {
                        ret = FlParseState.ParseFail;
                    }
                }
                else if (data == FlConstant.FL_TXT_MSG_ID_DEVICE_ID_DELIMITER)
                {
                    _msgId = GetMessageId();
                    if (_msgId != FlMessageId.Unknown)
                    {
                        _receiveState = ReceiveState.DeviceId;
                        ClearReceiveBuffer();
                    }
                    else
                    {
                        ret = FlParseState.ParseFail;
                    }
                }
                else
                {
                    ret = FlParseState.ParseFail;
                }
                break;

            case ReceiveState.DeviceId:
                if (IsDeviceIdChar(data) == true)
                {
                    _buf[_bufPos++] = data;
                    if (_bufPos > FlConstant.TXT_DEVICE_ID_MAX_LEN)
                    {
                        ret = FlParseState.ParseFail;
                    }
                }
                else if (data == FlConstant.FL_TXT_MSG_ARG_DELIMITER)
                {
                    if (ProcessResponseEventData() == true)
                    {
                        _receiveState = ReceiveState.Data;
                        ClearReceiveBuffer();
                    }
                    else
                    {
                        ret = FlParseState.ParseFail;
                    }
                }
                else if (IsTail(data) == true)
                {
                    if (ProcessResponseEventData() == true)
                    {
                        _receiveState = ReceiveState.Tail;
                        ret           = FlParseState.ParseOk;
                    }
                    else
                    {
                        ret = FlParseState.ParseFail;
                    }
                }
                else
                {
                    ret = FlParseState.ParseFail;
                }
                break;

            case ReceiveState.Data:
                if (IsTail(data) != true)
                {
                    if (data != FlConstant.FL_TXT_MSG_ARG_DELIMITER)
                    {
                        _buf[_bufPos++] = data;
                        if (_bufPos >= FlConstant.FL_TXT_MSG_MAX_LENGTH)
                        {
                            ret = FlParseState.ParseFail;
                        }
                    }
                    else
                    {
                        if (ProcessResponseEventData() == true)
                        {
                            ClearReceiveBuffer();
                        }
                        else
                        {
                            ret = FlParseState.ParseFail;
                        }
                    }
                }
                else
                {
                    if (ProcessResponseEventData() == true)
                    {
                        _receiveState = ReceiveState.Tail;
                        ClearReceiveBuffer();
                        ret = FlParseState.ParseOk;
                    }
                    else
                    {
                        ret = FlParseState.ParseFail;
                    }
                }
                break;

            default:
                ret = FlParseState.ParseFail;
                break;
            }

            if (ret != FlParseState.Parsing)
            {
                if (ret == FlParseState.ParseOk)
                {
                    if (OnParseDone != null)
                    {
                        OnParseDone?.Invoke(this, null);
                    }
                    else
                    {
                        if (_msgId == FlMessageId.ButtonEvent)
                        {
                            message = new FlTxtMessageEvent()
                            {
                                MessageId   = _msgId,
                                Arguments   = new List <object>(),
                                ReceiveTime = DateTime.UtcNow
                            };
                        }
                        else
                        {
                            message = new FlTxtMessageResponse()
                            {
                                MessageId   = _msgId,
                                Arguments   = new List <object>(),
                                ReceiveTime = DateTime.UtcNow
                            };
                        }


                        while (_arguments.Count > 0)
                        {
                            message.Arguments.Add(_arguments[0]);
                            _arguments.RemoveAt(0);
                        }
                    }
                }

                Clear();
            }

            return(ret);
        }