Git Product home page Git Product logo

ds2's Introduction

DS2 Library

  • Made to simplyfy the communication between arduino code and ECUs using DS2 k-line protocol ISO 9141. It was made and tested for L9637D but any interface should work the same way.

  • Look at example and drop 'libraries' folder into your library location.

  • More info about the project on RomRaider Forum <==

  • and MS4X.net wiki <==

Protocol DS2

  • Echo - if you send any command on DS2 it responds with whatever was send due to way K-line is like constructed.

  • Imagine K-line being a single guitar string and our TX is making it vibrate and our RX is constantly picking up this vibrations. This is echo

  • 1st byte in command/response is device which should receive command or which send the command 0x12 - ECU default

  • 2nd byte is command/response length

  • 3rd byte for response it's acknowledge byte A0 meaning positive response, B0 being negative and any other (usually FF) being error; for command is specific to command itself.

  • 4th - Xth bytes are for payload

  • Last byte is always XOR checksum. You can use this calculator to get it: https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/

  • Some commands for DS2 for BMW including MS41 / MS41.1 / MS41.2 / MS41.3 / MS42 / MS43:

    • ECU id - {0x12, 0x04, 0x00, 0x16}
    • General values - {0x12, 0x05, 0x0B, 0x03, 0x1F}

Copyright 2020 - Made by sorek.uk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ds2's People

Contributors

handmade0octopus 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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ds2's Issues

Simple exemple Serial

Hello :)
thank you for sharing your work it's really great !! :)
I tried to play with it just to get feedback on the serial port unfortunately I have nothing at all I can't communicate with the car at all (mini cooper R53)
I already use another library named ISO 9141 I happen to have some give back so my connection works
I wanted to know if your library was much faster because I can't manage to output more than 17 given per second
is it possible to help me to set up just a return for the serial port because I don't have a screen like you thank you very much in advance for your help

#include <Arduino.h>

#include "DS2.h"

boolean compareString(char a[], char b[]);
void printRps(float rps);
void printMessage(uint8_t array[], uint8_t length);
void printData(uint8_t command[]);

// We keep data there, 255 is reccomended for full compatibility, you can use void setMaxDataLength(uint8_t dataLength) if bugs happen
uint8_t data[255];

// Format for commands is always same, see DS3.h for more info
uint8_t ecuId[] = {0x12, 0x04, 0x00, 0x16};
uint8_t generalValues[] = {0x12, 0x05, 0x0B, 0x03, 0x1F};

// We store our ECU list and byte offset for our battery voltage
struct Ecu
{
  char *ecuName;
  uint8_t batteryOffset;
};

uint8_t batteryOffset;

#define ECUS_NUMBER 9

const struct Ecu ecuList[ECUS_NUMBER] PROGMEM = {
    {"default", 16},
    {"1429764", 20},
    {"1430844", 20},
    {"7526753", 20},
    {"7500255", 20},
    {"7511570", 22},
    {"7519308", 22},
    {"7545150", 22},
    {"7551615", 22},
};

// Simple function to match ECU to what we read
uint8_t matchEcu(char *id)
{
  Ecu ecu;
  for (uint8_t i = 0; i < ECUS_NUMBER; i++)
  {
    memcpy_P(&ecu, &ecuList[i], sizeof ecu);
    if (compareString(id, ecu.ecuName))
    {
      return ecu.batteryOffset;
    }
  }
  memcpy_P(&ecu, &ecuList[0], sizeof ecu);
  return ecu.batteryOffset;
}

boolean compareString(char a[], char b[])
{
  boolean match = true;
  for (uint8_t i = 0; i < 255; i++)
  {
    if (a[i] == 0 || b[i] == 0)
      break;
    if (a[i] != b[i])
    {
      match = false;
      break;
    }
  }
  return match;
}

DS2 DS3(Serial2);

void setup()
{
  Serial.begin(9600);
  // Serial2.begin(9600, SERIAL_8E1);
  Serial2.setTimeout(ISO_TIMEOUT);

  // You can set blocking if you want to test how it works.
  // DS3.setBlocking(true);

  // Loading and displaying ECU ID

  char loading[][3] = {"|", "/", "-", "\\"};
  Serial.println("CONNECTING");
  for (uint8_t i = 0; !DS3.obtainValues(ecuId, data); i++)
  {
    if (i > 3)
    {
      i = 0;
    }

    Serial.print(loading[i]);
  }

  // Displaying Ecu ID
  char ecuIdString[8];
  DS3.getString(data, ecuIdString, 0, 7);
  Serial.print(ecuIdString);

  // Matching battery voltage offset depending on our ECU
  batteryOffset = matchEcu(ecuIdString);
}

// Loop variables
uint32_t startTime;
float fps, lowestFps = 0, highestFps = 0;
boolean print = false;

void loop(void)
{
  startTime = micros();

  // You can compare performance using two types of commands

  // Send command
  if (DS3.sendCommand(generalValues) != 0) // do stuff if data sent

    /**
     * You can put some code in between while waiting for data for higher performance
     **/
    if (print)
      printData(generalValues);

  // Receive command
  if ((DS3.receiveData(data)) == RECEIVE_OK)
    print = true; // do stuff if data received

  // Blocked .obtainValues - generally slower but easier to use and always laids some response
  //	if(DS3.obtainValues(generalValues, data)) print = true;
}

// Prints data, gets batteryVoltage from data, you need to specify which command was sent
void printData(uint8_t command[])
{
  float batteryVoltage = 0.1 * DS3.getByte(data, batteryOffset);
  Serial.print(batteryVoltage);
  Serial.println(" V  ");

  //	Takes a lot of performance due to how .print is managed. Those screens doesn't like reprints

  /*
  printMessage(command, DS3.getEcho());
  tft.setCursor(0,20);
  printMessage(data, DS3.getResponseLength());
  */

  printRps(DS3.getRespondsPerSecond());
  print = false;
}

// Very slow way of printing message, good for debugging though
void printMessage(uint8_t array[], uint8_t length)
{
  for (uint8_t i = 0; i < 255; i++)
  {
    if (i < length)
      Serial.print((array[i]), HEX);
    Serial.print(" ");
  }
}

// Prints reesponses per second
void printRps(float rps)
{

  Serial.print(rps);
  Serial.println(" rps  ");
}

// Prints Fps on the screen with low, high and current. If touch pressed min/max fps are resetted

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.