public void DecodeMessage_OneMessageDifferentStream()
        {
            byte controllerID = 0x01;
            byte msgID        = 0x34;
            byte cmd          = (byte)Command.GetVersion;

            byte[]      commandData = new byte[] { 0x49, 0x03, 0x92, 0x96, 0x00 };
            byte[]      inputData   = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 };
            List <byte> dataList    = inputData.ToList();

            dataList.InsertRange(4, commandData);
            inputData = dataList.ToArray();
            inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData);
            inputData[0] = STX;
            inputData[inputData.Length - 1] = ETX;
            List <ProtocolMessage> myMsgs = new List <ProtocolMessage>();

            MessageCodec.DecodeMessages(inputData.Take(4).ToArray(), out List <ProtocolMessage> msgs, out byte[] outputRemainder);
            myMsgs.AddRange(msgs);
            MessageCodec.DecodeMessages(inputData.Skip(4).ToArray(), out List <ProtocolMessage> msgs2, out byte[] outputRemainder2, outputRemainder);
            myMsgs.AddRange(msgs2);

            Assert.IsTrue(myMsgs.Count == 1);
            foreach (ProtocolMessage msg in myMsgs)
            {
                Assert.AreEqual(msg.ControllerID, controllerID);
                Assert.AreEqual(msg.MsgID, msgID);
                Assert.AreEqual((byte)msg.Command, cmd);
                CollectionAssert.AreEqual(commandData, msg.CommandData);
            }
            CollectionAssert.AreEqual(inputData.Take(4).ToArray(), outputRemainder);
            CollectionAssert.AreEqual(new byte[0], outputRemainder2);
        }
        public void DecodeMessage_MultipleEscapeCharactersEndingIn_f_x5()
        {
            byte    controllerID = 0x01;
            byte    msgID        = 0x90;
            Command cmd          = Command.DebugString;

            byte[] commandData = Encoding.UTF8.GetBytes("fffff");
            byte[] expectedMsg = new byte[] { 0x00, controllerID, msgID, (byte)cmd,
                                              0x66, 0x00, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00,
                                              0x00, 0x00 };
            byte[] expectedMsgNotEscaped = new byte[] { 0x00, controllerID, msgID, (byte)cmd,
                                                        0x66, 0x66, 0x66, 0x66, 0x66,
                                                        0x00, 0x00 };
            expectedMsg[expectedMsg.Length - 2] = MessageCodec.CalculateCRC(expectedMsgNotEscaped);
            expectedMsg[expectedMsg.Length - 1] = ETX;
            expectedMsg[0] = STX;

            byte[] msg = MessageCodec.EncodeMessage(new ProtocolMessage(controllerID, msgID, cmd, commandData));
            MessageCodec.DecodeMessages(msg, out List <ProtocolMessage> msgs, out byte[] outputRemainder);
            ProtocolMessage myMessage = msgs.First();

            Assert.AreEqual(expectedMsg.Length, msg.Length);
            CollectionAssert.AreEqual(expectedMsg, msg);
            CollectionAssert.AreEqual(commandData, myMessage.CommandData);
        }
        public void DecodeMessage_TwoMessagesSameStream_GarbageInBetween()
        {
            byte controllerID = 0x01;
            byte msgID        = 0x34;
            byte cmd          = (byte)Command.GetVersion;

            byte[]      commandData = new byte[] { 0x49, 0x03, 0x92, 0x96, 0x00 };
            byte[]      inputData   = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 };
            List <byte> dataList    = inputData.ToList();

            dataList.InsertRange(4, commandData);
            inputData = dataList.ToArray();
            inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData);
            inputData[0] = STX;
            inputData[inputData.Length - 1] = ETX;
            dataList = inputData.ToList();
            dataList.AddRange(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF });
            dataList.AddRange(inputData);
            inputData = dataList.ToArray();

            MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder);
            Assert.IsTrue(msgs.Count == 2);

            foreach (ProtocolMessage msg in msgs)
            {
                Assert.AreEqual(msg.ControllerID, controllerID);
                Assert.AreEqual(msg.MsgID, msgID);
                Assert.AreEqual((byte)msg.Command, cmd);
                CollectionAssert.AreEqual(commandData, msg.CommandData);
            }
            CollectionAssert.AreEqual(new byte[0], outputRemainder);
        }
        public void DecodeMessage_OneMessageCommandNoData_Remainder()
        {
            byte controllerID = 0x01;
            byte msgID        = 0x34;
            byte cmd          = (byte)Command.GetVersion;

            byte[]      commandData = new byte[0];
            byte[]      inputData   = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 };
            List <byte> dataList    = inputData.ToList();

            dataList.InsertRange(4, commandData);
            inputData = dataList.ToArray();
            inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData);
            inputData[0] = STX;
            inputData[inputData.Length - 1] = ETX;
            dataList = inputData.ToList();
            dataList.AddRange(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
            inputData = dataList.ToArray();

            MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder);
            Assert.IsTrue(msgs.Count == 1);

            ProtocolMessage msg = msgs.First();

            Assert.AreEqual(msg.ControllerID, controllerID);
            Assert.AreEqual(msg.MsgID, msgID);
            Assert.AreEqual((byte)msg.Command, cmd);
            CollectionAssert.AreEqual(commandData, msg.CommandData);
            CollectionAssert.AreEqual(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, outputRemainder);
        }
        public void DecodeMessage_DecodeEncodedMessageDataEscaped()
        {
            ProtocolMessage msg = new ProtocolMessage(STX, 0x02, Command.ConfigChannel, new byte[] { ESC, STX, ETX });

            byte[] encodedMsg = MessageCodec.EncodeMessage(msg);
            MessageCodec.DecodeMessages(encodedMsg, out List <ProtocolMessage> msgs, out byte[] remainder);

            Assert.IsTrue(msgs.Count == 1);
            Assert.IsTrue(remainder.Length == 0);
            Assert.AreEqual(msg.ControllerID, msgs.First().ControllerID);
            Assert.AreEqual(msg.MsgID, msgs.First().MsgID);
            Assert.AreEqual(msg.Command, msgs.First().Command);
            CollectionAssert.AreEqual(msg.CommandData, msgs.First().CommandData);
        }
        protected void ReceiveMessage(BytesReceivedEventArgs e)
        {
            MessageCodec.DecodeMessages(e.Message, out List <ProtocolMessage> messages, out this.remainderBytes, this.remainderBytes);
            foreach (ProtocolMessage protocolMessage in messages)
            {
                switch (protocolMessage.Command)
                {
                case Command.GetVersion:
                    this.DispatchVersionMessage(protocolMessage);
                    break;

                case Command.GetInfo:
                    break;

                case Command.WriteRegister:
                    break;

                case Command.QueryRegister:
                    this.DispatchQueryRegisterMessage(protocolMessage);
                    break;

                case Command.ConfigChannel:
                    break;

                case Command.Decimation:
                    break;

                case Command.ResetTime:
                    break;

                case Command.ReadChannelData:
                    DispatchReadChannelDataMessage(protocolMessage);
                    break;

                case Command.DebugString:
                    break;

                case Command.EmbeddedConfiguration:
                    break;

                case Command.Tracing:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
        public void DecodeMessage_OneMessage_UnexpectedSTX()
        {
            byte controllerID = 0x01;
            byte msgID        = 0x34;
            byte cmd          = (byte)Command.GetVersion;

            byte[]      commandData = new byte[] { 0x49, STX, 0x67, 0x96, 0x00 };
            byte[]      inputData   = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 };
            List <byte> dataList    = inputData.ToList();

            dataList.InsertRange(4, commandData);
            inputData = dataList.ToArray();
            inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData);
            inputData[0] = STX;
            inputData[inputData.Length - 1] = ETX;

            MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder);
            Assert.IsFalse(msgs.First().Valid);
        }
        public void DecodeMessage_OneMessageNoCommandData_NoRemainder()
        {
            byte controllerID = 0x01;
            byte msgID        = 0x34;
            byte cmd          = (byte)Command.GetVersion;

            byte[] inputData = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 };
            inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData);
            inputData[0] = STX;
            inputData[inputData.Length - 1] = ETX;

            MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder);
            Assert.IsTrue(msgs.Count == 1);

            ProtocolMessage msg = msgs.First();

            Assert.AreEqual(msg.ControllerID, controllerID);
            Assert.AreEqual(msg.MsgID, msgID);
            Assert.AreEqual((byte)msg.Command, cmd);
            CollectionAssert.AreEqual(new byte[0], outputRemainder);
        }
        public void EncodeMessage_EncodeDecodedMessage_NormalData()
        {
            byte controllerID = 0x01;
            byte msgID        = 0x34;
            byte cmd          = (byte)Command.GetVersion;

            byte[]      commandData = new byte[] { 0x49, 0x05, 0x67, 0x96, 0x00 };
            byte[]      inputData   = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 };
            List <byte> dataList    = inputData.ToList();

            dataList.InsertRange(4, commandData);
            inputData = dataList.ToArray();
            inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData);
            inputData[0] = STX;
            inputData[inputData.Length - 1] = ETX;

            MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] remainder);
            byte[] msg = MessageCodec.EncodeMessage(msgs.First());

            Assert.IsTrue(msgs.Count == 1);
            Assert.IsTrue(remainder.Length == 0);
            CollectionAssert.AreEqual(inputData, msg);
        }