java-obd-adapter

java-obd-adapter - OBD-II Java Adapter API

Requisites

Supported Commands

AT Commands

Diagnostic Commands

Usage

<dependency>
  <groupId>com.devesion.commons.obd</groupId>
  <artifactId>obd-adapter</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>
    private void invokeInitialCommands() {
        invokeCommandQuietly(new ResetCommand());
        invokeCommand(new SetEchoCommand(false));
        invokeCommand(new SetLineFeedCommand(false));
        invokeCommand(new SetHeadersCommand(false));
        invokeCommand(new SetSpacesCommand(false));
        invokeCommand(new AdaptiveTimeoutProtocolCommand());
        invokeCommand(new SelectProtocolCommand());
    }

    private int readEngineRpm() {
        EngineRpmCommand engineRpmCommand = new EngineRpmCommand();
        invokeCommand(engineRpmCommand);
        return engineRpmCommand.getValue().getIntValue();
    }

    private int readVehicleSpeed() {
        SpeedCommand speedCommand = new SpeedCommand();
        invokeCommand(speedCommand);
        return speedCommand.getValue().getIntValue();
    }

    private float readThrottlePositionPercentage() {
        ThrottlePositionCommand throttlePositionCommand = new ThrottlePositionCommand();
        invokeCommand(throttlePositionCommand);
        return throttlePositionCommand.getValue().getIntValue();
    }

    private double readMassAirFlow() {
        MassAirFlowCommand massAirFlowObdCommand = new MassAirFlowCommand();
        invokeCommand(massAirFlowObdCommand);
        return massAirFlowObdCommand.getValue().getFloatValue();
    }

    private void invokeCommand(ObdCommand command) {
        try {
            CommandInvoker commandInvoker = createCommandInvoker();
            log.info("invoking command {}", command);
            commandInvoker.invoke(command);
        } catch (Exception e) {
            throw new DiagnosticStatusReadException(e);
        }
    }

    private void invokeCommandQuietly(ObdCommand command) {
        CommandInvoker commandInvoker = createCommandInvoker();
        log.info("invoking command {}", command);
        commandInvoker.invokeQuietly(command);
    }

    private CommandInvoker createCommandInvoker(InputStream inputStream, OutputStream outputStream) {
        ObdLink obdLink = new ElmLink(inputStream, outputStream);
        return new CommandInvoker(obdLink);
    }

Bitdeli Badge