Git Product home page Git Product logo

obd-parser's Introduction

obd-parser

Circle CI

A module for interacting with the OBD (On Board Diagnostics) of vehicles via ELM 327 connections.

Install

npm install obd-parser --save

After this you will need to install a module that facilitates connecting to the ECU. At present only obd-parser-serial-connection is available - this requires a USB to OBD connection such as these.

npm install obd-parser-serial-connection --save

Usage

This example uses TypeScript, but the examples folder has the JavaScript code, the JavaScript code is also pasted below for reference.

// In your code this should be changed to 'obd-parser'
import * as OBD from 'obd-parser';

// Use a serial connection to connect
var getConnector = require('obd-parser-serial-connection');

// Returns a function that will allow us to connect to the serial port
var connectorFn:Function = getConnector({
  // This might vary based on OS - this is the Mac OSX example
  serialPath: '/dev/tty.usbserial',

  // Might vary based on vehicle. This is the baudrate for a MK6 VW GTI
  serialOpts: {
    baudrate: 38400
  }
});

// Need to initialise the OBD module with a "connector" before starting
OBD.init(connectorFn)
  .then(function () {
    // We've successfully connected. Can now create ECUPoller instances
    const rpmPoller:OBD.ECUPoller = new OBD.ECUPoller({
      // Pass an instance of the RPM PID to poll for RPM
      pid: new OBD.PIDS.Rpm(),
      // Poll every 1500 milliseconds
      interval: 1500
    });

    // Bind an event handler for anytime RPM data is available
    rpmPoller.on('data', function (output: OBD.OBDOutput) {
      console.log('==== Got RPM Output ====');
      // Timestamp (Date object) for wheb the response was received
      console.log('time: ', output.ts);
      // The bytes returned from the ECU when asked from RPM
      console.log('bytes: ', output.bytes);
      // A value that's usuall numeric e.g 1200
      console.log('value: ', output.value);
      // This will be a value such as "1200rpm"
      console.log('pretty: ', output.pretty);
    });

    // Start polling (every 1500ms as specified above)
    rpmPoller.startPolling();
  });

Supported PIDs

Currently the module has support for a limited number of PIDs. PID support can easily be added by adding a new PID definition in lib/pids/pid.ts. You can find information that will assist PID implementation on this Wiki.

For the most up to date list see this directory, or the below list:

  • ENGINE_COOLANT_TEMPERATURE (05)
  • FUEL_LEVEL_INPUT (2F)
  • ENGINE_RPM (0C)
  • VEHICLE_SPEED (0D)

If using TypeScript you can also type "OBD.PIDS" and intellisense will display available options.

API

OBD.init(connectorFn)

Initialise the parser. connectorFn should be a connector function generated by a module such as obd-parser-serial-connection.

OBD.ECUPoller(args: PollerArgs) (Class)

A class that can be used to create ECUPoller instances. These must be passed an args Object that contains:

  • pid - An instance of any PID, e.g new OBD.PIDS.Rpm()
  • interval - The number of milliseconds two wait bewteen polling if startPolling() is called.

You should only create one instance of a given ECUPoller and PID combination at a time unless you're sure about what you're doing.

ECUPoller.poll()

Sends a poll to the ECU for this ECUPoller's PID. Returns Promise that will resolve with an Object matching the OBDOutput interface. If you want you can ignore the Promise and instead bind a "data" listener like the example in this README file.

ECUPoller.startPolling() and ECUPoller.stopPolling()

Starts a constant poll loop that queries as near to the args.interval passed to this ECUPoller. If it is not receiving responses it will not send new polls even if it reaches the interval since we want to prevent flooding the ECU.

OBD.PIDS

This is an Object with PID Classes attached, currently valid options are demonstrated below:

import * as OBD from 'obd-parser';

new OBD.PIDS.FuelLevel();
new OBD.PIDS.Rpm();
new OBD.PIDS.VehicleSpeed();
new OBD.PIDS.CoolantTemp();

// This is the base class used by all above PIDS. You might want to extend
// this to create your own PIDs (don't forget to contribute them here!)
new OBD.PIDS.PID();

OBD.OBDOutput

Used in TypeScript environments to apply typing to poll "data" events and Promise results.

Pure JavaScript Example

Pure JavaScript example. It is almost identical to the TypeScript version from above in this README.

'use strict';

var OBD = require('obd-parser');

var getConnector = require('obd-parser-serial-connection');

var connect = getConnector({
  serialPath: '/dev/tty.usbserial',
  serialOpts: {
    baudrate: 38400
  }
});

OBD.init(connect)
  .then(function () {
    var rpmPoller = new OBD.ECUPoller({
      pid: new OBD.PIDS.Rpm(),
      interval: 1500
    });

    rpmPoller.on('data', function (output) {
      console.log('==== Got RPM Output ====');
      console.log('time: ', output.ts);
      console.log('bytes: ', output.bytes);
      console.log('value: ', output.value);
      console.log('pretty: ', output.pretty);
    });

    rpmPoller.startPolling();
});

CHANGELOG

  • 0.2.1

    • Ensure definition files are included in published code
  • 0.2.0

    • Rewrite using TypeScript and Classes (inner conflict regarding Classes)
    • Simplify getting and creating ECUPollers and PIDS
    • Upadted documentation and example
  • < 0.2.0 - (ಠ_ಠ)

obd-parser's People

Contributors

evanshortiss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

obd-parser's Issues

Could not locate the bindings file /node_modules/serialport/

Could not locate the bindings file. Tried: → /node_code/node_modules/serialport/build/serialport.node → /node_code/node_modules/serialport/build/Debug/serialport.node → /node_code/node_modules/serialport/build/Release/serialport.node → /node_code/node_modules/serialport/out/Debug/serialport.node → /node_code/node_modules/serialport/Debug/serialport.node → /node_code/node_modules/serialport/out/Release/serialport.node → /node_code/node_modules/serialport/Release/serialport.node → /node_code/node_modules/serialport/build/default/serialport.node → /node_code/node_modules/serialport/compiled/5.8.0/linux/arm/serialport.node at bindings (/node_code/node_modules/bindings/bindings.js:88:9) at Object.<anonymous> (/node_code/node_modules/serialport/lib/bindings.js:3:35) at Module._compile (module.js:413:34) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) at require (internal/module.js:16:19) at Object.<anonymous> (/node_code/node_modules/serialport/lib/serialport.js:16:25) at Module._compile (module.js:413:34)

Example

`var pollers = obd.pollers;
^

ReferenceError: obd is not defined
at Object. (/node_code/obd-parser/obd2.js:3:15)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:141:18)
at node.js:933:3
`

When using the example.

Reliability of stopPolling() with small ECUPoller intervals

I am writing an application that uses obd-parser along with obd-parser-serial-connection to query an ELM 327 bluetooth adapter (currently connected to an Arduino with the Seeed Studio CAN-Bus shield serving as a hardware emulator).

I query the ECU & get the 8 PIDs that I have written support for in the Arduino sketch, then initialise pollers for these 8 PIDs. If I set the interval of each ECUPoller instance to something fairly slow like 500ms, I can start & stop them via startPolling() & stopPolling() with no problems. However if I set the interval to something faster like 200ms I can start them fine but when I try to stop them, some or all of them do not stop & instead continue polling. If I set the interval to something very fast like 50ms, then all of them fail to stop polling when I call stopPolling().

After adding debug console.log statements to obd-parser/lib/poller.js I can see that the stopPolling() function is in fact being called 8 times, but I cannot work out why some of these calls are not having the documented effect.

I put together a much simpler test file which simply starts one poller (Mode 01 PID 12/0x0C - RPM), increments a count each time a response is received, then calls stopPolling() once the count reaches 5. I added debug console.log statements to both the startPolling() & stopPolling() functions. In this test, the poller never stops polling?

Here is my test file --> https://gist.github.com/CJ-Davies/5a810072245a26bf99fb0a7893933a15

And the console output --> https://gist.github.com/CJ-Davies/4f334ec73f6159e96e08bac579d3cb5c

I hope I am just missing something obvious & this issue can be closed with 'problem exists between keyboard and chair'...

Unable to build application(obd-parser) with ionic frmework

Hi

I have created a new application in ionic and when I run command ionic serve then its build and ran fine.
Now I install the obd-parser and on home.ts I have added the below line at top.

import * as OBD from 'obd-parser';

After adding this line my application its stop working. Please refer the attached screen shot.
iconic

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.