Example #1
0
        /// <summary>
        /// Send a command to the device
        /// </summary>
        /// <typeparam name="T">The struct to use for parameters</typeparam>
        /// <param name="command">Command to execute</param>
        /// <param name="payload">Command parameter data to send</param>
        //protected override void SendPacket<T>(byte command, T payload)
        //{
        //    if (!Connected)
        //        throw new Exception("Controller is not connected");

        //    try
        //    {
        //        byte[] packet = HexProtocol.CreatePacket<T>(command, payload);
        //        WritePacket(packet);
        //    }
        //    catch (Exception ex)
        //    {
        //        Disconnect();
        //        throw new ControllerConnectionException("Connection to controller lost", innerException:ex);
        //    }
        //}

        protected override void SendPacket(byte command, byte[] payload = null)
        {
            if (!Connected)
            {
                throw new Exception("Controller is not connected");
            }

            try
            {
                byte[] packet = HexProtocol.CreatePacket(command, payload);
                WritePacket(packet);
            }
            catch (Exception ex)
            {
                Disconnect();
                throw new ControllerConnectionException("Connection to controller lost", innerException: ex);
            }
        }
Example #2
0
        /// <summary>
        /// Wait for a reply from the device, after sending a command.
        /// If the command is not what was expected, an exception is raised.
        /// </summary>
        /// <param name="expected_command">The command that is expected</param>
        /// <returns>Raw payload bytes</returns>
        protected override byte[] ReadReply(byte expected_command)
        {
            try {
                byte[] response = ReceiveFrame();
                byte   command;
                byte[] payload;
                HexProtocol.ParsePacket(response, out command, out payload);

                if (command != expected_command)
                {
                    throw new HLDCProtocolException("Unexpected or invalid reply received");
                }

                return(payload);
            }
            catch (Exception ex)
            {
                Disconnect();
                throw new ControllerConnectionException("Connection to controller lost", innerException: ex);
            }
        }
Example #3
0
        /// <summary>
        /// Wait for a reply from the device, after sending a command.
        /// If the command is not what was expected, an exception is raised.
        /// </summary>
        /// <param name="expected_command">The command that is expected</param>
        /// <returns>Raw payload bytes</returns>
        protected override byte[] ReadReply(byte expected_command)
        {
            try
            {
                byte[] response = ReceiveFrame();
                byte   command;
                byte[] payload;
                HexProtocol.ParsePacket(response, out command, out payload);

                // Detect packet loss
                if (command == 0x00)
                {
                    throw new HLDCProtocolException("No response from the device - packet was lost?");
                }

                // Detect mismatched/corrupt packet
                if (command != expected_command)
                {
                    throw new HLDCProtocolException(String.Format(
                                                        "Unexpected or invalid reply received (Expected: 0x{0:X2}, Got: 0x{1:X2})",
                                                        expected_command, command));
                }

                return(payload);
            }
            catch (HLDCProtocolException ex)
            {
                // Packet error, allow TransferPacket to retry
                throw;
            }
            catch (Exception ex)
            {
                Disconnect();
                throw new ControllerConnectionException("Connection to controller lost", innerException: ex);
            }
        }