problema connessione shield obd2 con arduino uno


salve tutti,
sto utilizzando arduino uno e un prodotto sparkfun
link shield sparkfun -> https://www.sparkfun.com/products/9555
sto seguendo un tutorial sul sito sparkfun e ho alcuni dubbi e problemi che non riesco risolvere
link tutorial -> https://learn.sparkfun.com/tutorials/obd-ii-uart-hookup-guide
il mio scopo e' quello di collegare la shield alla macchina per effettuare un servizio di diagnostica
sto provando il codice che e' allegato all'interno del tutorial
code: [select]
#include <softwareserial.h>

//create instance of new soft serial library control serial serial
//note, digital pin 3 of arduino should connected rx of serial serial.


//this character buffer store data serial port
char rxdata[20];
char rxindex=0;

softwareserial obd(5,6);
//variables hold speed , rpm data.
int vehiclespeed=0;
int vehiclerpm=0;

void setup(){
  //both serial serial , obd-ii-uart use 9600 bps.
  serial.begin(9600);
  obd.begin(9600);
  //clear old data serial.
   

  //put speed header on first row.
  serial.print("speed: ");
  //put rpm header on second row.
 
  serial.print("rpm: ");

  //wait little while before sending reset command obd-ii-uart
  delay(1500);
  //reset obd-ii-uart
  obd.println("atz");
  serial.println("risposta elm327:");

   serial.println(obd.read());
  //wait bit before starting send commands after reset.
  delay(10000);

  //delete data may in serial port before begin.
  serial.flush();
}
void loop(){
  //delete data may in serial port before begin. 
  serial.flush();
  //set cursor in position want speed data.
 
  //clear out old speed data, , reset cursor position.
  serial.print("        ");
 
  //query obd-ii-uart vehicle speed
  obd.println("010d");
  //get response obd-ii-uart board. 2 responses
  //because obd-ii-uart echoes command sent.
  //we want data in second response.
  getresponse();
  getresponse();
  //convert string data integer
  vehiclespeed = strtol(&rxdata[6],0,16);
  //print speed data serial
  serial.print(vehiclespeed);
  serial.print(" km/h");
  delay(100);

  //delete data may left on in serial port.
  serial.flush();
  //move serial cursor position want rpm data.
 
  //clear old rpm data, , move cursor position back.
  serial.print("          ");


  //query obd-ii-uart vehicle rpm
  obd.println("010c");
  //get response obd-ii-uart board
  getresponse();
  getresponse();
  //convert string data integer
  //note: rpm data 2 bytes long, , delivered in 1/4 rpm obd-ii-uart
  vehiclerpm = ((strtol(&rxdata[6],0,16)*256)+strtol(&rxdata[9],0,16))/4;
  //print rpm data serial
  serial.print("giri minuto=");
  serial.print(vehiclerpm);

  //give obd bus rest
  delay(1000);

}

//the getresponse function collects incoming data uart rxdata buffer
// , exits when carriage return character seen. once carriage return
// string detected, rxdata buffer null terminated (so can treat string)
// , rxdata index reset 0 next string can copied.
void getresponse(void){
  char inchar=0;
  serial.println("sono nella getresponde");
  //keep reading characters until carriage return
  while(inchar != '\r'){
    serial.println("leggo e aspetto il carattere di escape");
    serial.println(inchar);
    //if character comes in on serial port, need act on it.
    if(obd.available() > 0){
      serial.println("arriva qualcosa");
      //start checking if we've received end of message character ('\r').
      if(obd.peek() == '\r'){
        //clear serial buffer
        inchar=obd.read();
        //put end of string character on our data string
        rxdata[rxindex]='\0';
        //reset buffer index next character goes @ beginning of string.
        rxindex=0;
      }
      //if didn't end of message character, add new character string.
      else{
        //get new character serial port.
        inchar = obd.read();
       
        //add new character string, , increment index variable.
        rxdata[rxindex++]=inchar;
      }
    }
  }
}


il codice l'ho lievemente modificato , le differenze principali sono che non uso un lcd per visualizzare dati e non ho collegato la shield alla seriale di arduino ma ho fatto una comunicazione seriale sui pin 5 e 6 tramite la libreria softwareserial. collegando il tutto alla macchina non ricevo dati cercati ma mi arrivano sequenze strane di caratteri e la risposta all' atc command "atz" e' -1. ho provato "googlare " il problema ma non ho trovato nulla riguardo , ho pensato solo che problemi possano essere dovuti o all'utilizzo della software serial o al baudrate .
qualcuno di voi ha mai riscontrato problemi del genere?
grazie tutti!



Arduino Forum > International > Italiano > Hardware (Moderator: leo72) > problema connessione shield obd2 con arduino uno


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support