Ejemplo n.º 1
0
        private static CmdParseResult SerialParseProc(IList <byte> buffer, out Int32 parsedHeaderIndex, out Int32 parsingIndex)
        {
            parsedHeaderIndex = 0;
            parsingIndex      = 0;

            CmdParseResult result = CmdParseResult.IncompleteCmd;

            var index = BytesHelper.IndexOf(buffer, Header);

            if (buffer.Count > 0)
            {
                Logger.Debug("inderx:" + index);
                for (int i = 0; i < buffer.Count; i++)
                {
                    Logger.Debug("buffer:" + buffer[i].ToString());
                }
            }

            if (index >= 0)
            {
                parsedHeaderIndex = index;

                Boolean completeCmd = (buffer.Count - index) >= (Header.Length + 4);
                //协议数据是完整的
                if (completeCmd)
                {
                    parsingIndex = index + Header.Length + 4;

                    var checkIndex = parsingIndex - 2;

                    var checkValid = buffer[checkIndex] == 0x81 && buffer[checkIndex + 1] == 0x82;
                    Logger.Debug(buffer[checkIndex].ToString() + "@@@" + buffer[checkIndex + 1]);
                    if (checkValid)
                    {
                        result = CmdParseResult.Ok;
                        var msg = "检测到有效的串口数据:{Environment.NewLine}{BitConverter.ToString(buffer.ToArray())}";

                        Logger.Debug(msg);

                        var state = GetDeviceState(buffer, checkIndex - 2);

                        OnDeviceStateChanged(state);
                    }
                    else
                    {
                        result = CmdParseResult.Fail;

                        var msg = "检测到无效的串口数据:{Environment.NewLine}{BitConverter.ToString(buffer.ToArray())}";

                        Logger.Debug(msg);
                    }
                }
                else
                {
                    Logger.Debug("协议是不完整的");
                }
            }
            return(result);
        }