Exemple #1
0
        internal IOBDResponse <TResult> PerformQuery <TResult>(IOBDCommand <TResult> cmd, bool force = false)
        {
            lock (_syncLock)
            {
                //primary API function. Sends commands to the car, and
                //protects against sending unsupported commands.

                if (Status == OBDStatus.NotConnected)
                {
                    _logger.Error("Query failed, no connection available");
                    return(OBDResponse <TResult> .Empty);
                }

                if (!force && !Supports(cmd))
                {
                    _logger.Error($"'{cmd}' is not supported");
                    return(OBDResponse <TResult> .Empty);
                }

                // send command and retrieve message
                _logger.Debug($"Sending command: {cmd}");
                var cmdString = BuildCommandString(cmd);
                var messages  = _port.SendAndParse(cmdString);

                // if we're sending a new command, note it
                if (string.IsNullOrWhiteSpace(cmdString))
                {
                    _lastCommand = cmdString;
                }

                if (messages == null || messages.Length == 0)
                {
                    _logger.Error("No valid OBD Messages returned");
                    return(OBDResponse <TResult> .Empty);
                }

                // Trim bytes based on cmd.Bytes size
                foreach (var message in messages)
                {
                    message.Data = message.Data.Take(cmd.Bytes).ToArray();
                }

                var result = cmd.Decoder(messages);
                result.Messages = messages;
                result.Time     = DateTime.UtcNow;
                result.Command  = cmd;
                return(result);
            }
        }
Exemple #2
0
        /// <summary>
        ///     assembles the appropriate command string
        /// </summary>
        private string BuildCommandString(IOBDCommand cmd)
        {
            var cmdString = cmd.Command;

            // only wait for as many ECUs as we've seen
            if (_port.Config.Fast && cmd.Fast)
            {
                cmdString += _port.Ecus.Count().ToString(); // TODO: ?? Check this syntax on multi ecu's
            }

            // if we sent this last time, just send
            if (_port.Config.Fast && cmdString == _lastCommand)
            {
                cmdString = "";
            }

            return(cmdString);
        }
 public static int Pid(this IOBDCommand obdCommand)
 {
     return(Utils.Unhex(obdCommand.Command, 2));
 }
Exemple #4
0
 public bool Supports(IOBDCommand cmd)
 {
     return(_supportedCommands.Contains(cmd));
 }
Exemple #5
0
 public IOBDResponse <TResult> Query <TResult>(IOBDCommand <TResult> cmd, bool force = false)
 {
     //var cmd = _commands.Get<TCommand, TResult>();
     return(PerformQuery(cmd, force));
 }