Exemplo n.º 1
0
        /// <summary>
        /// Create a new command object
        /// </summary>
        /// <param name="command"></param>
        /// <param name="data"></param>
        public TpiCommand(RequestCommand command, String data = null)
        {
            Command     = command;
            CommandData = data;

            if (data != null)
            {
                TPIData = CheckSumHelper.AppendCheckSumAndEndOfRequest(command.GetStringValue() + data);
            }
            else
            {
                TPIData = CheckSumHelper.AppendCheckSumAndEndOfRequest(command.GetStringValue());
            }
        }
Exemplo n.º 2
0
        private void Parse(string data)
        {
            if (data == null)
            {
                data = "";
            }
            data = data.Replace("\0", "");
            //check if it's prefixed with date:
            if (data.Length > 9)
            {
                int number;
                Int32.TryParse(data.Substring(0, 1), out number);
                if (number > -1)
                {
                    //prefixed with date.
                    var date = data.Substring(0, 8);

                    DateTime tmp;
                    if (DateTime.TryParse(date, out tmp))
                    {
                        data = data.Substring(9); // remove date + space
                        Date = tmp;
                    }
                    else
                    {
                        Date = DateTime.Now;
                    }
                }
                else
                {
                    Date = DateTime.Now;
                }
            }
            else
            {
                Date = DateTime.Now;
            }

            //get ResponseEnum.
            ResponseCommand command;

            Enum.TryParse(data.Substring(0, CommandSize), out command);

            var numBytes = command.GetNumberOfBytes();

            var payload = ""; // can be 0-length

            if (numBytes > 0)
            {
                payload = data.Substring(CommandSize, numBytes);
            }

            var checksum = data.Substring(CommandSize + numBytes, ChecksumSize);

            if (!CheckSumHelper.VerifyChecksum(command, payload, checksum))
            {
                throw new Exception("Wrong checksum in response detected.");
            }

            Command = command;
            Data    = payload;
        }