salve a 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 a risolvere
link tutorial -> OBD II UART Hookup Guide - SparkFun Learn
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
#include <SoftwareSerial.h>
//Create an instance of the new soft serial library to control the serial Serial
//Note, digital pin 3 of the Arduino should be connected to Rx of the serial Serial.
//This is a character buffer that will store the data from the serial port
char rxData[20];
char rxIndex=0;
SoftwareSerial OBD(5,6);
//Variables to hold the speed and RPM data.
int vehicleSpeed=0;
int vehicleRPM=0;
void setup(){
//Both the Serial Serial and the OBD-II-UART use 9600 bps.
Serial.begin(9600);
OBD.begin(9600);
//Clear the old data from the Serial.
//Put the speed header on the first row.
Serial.print("Speed: ");
//Put the RPM header on the second row.
Serial.print("RPM: ");
//Wait for a little while before sending the reset command to the OBD-II-UART
delay(1500);
//Reset the OBD-II-UART
OBD.println("ATZ");
Serial.println("Risposta ELM327:");
Serial.println(OBD.read());
//Wait for a bit before starting to send commands after the reset.
delay(10000);
//Delete any data that may be in the serial port before we begin.
Serial.flush();
}
void loop(){
//Delete any data that may be in the serial port before we begin.
Serial.flush();
//Set the cursor in the position where we want the speed data.
//Clear out the old speed data, and reset the cursor position.
Serial.print(" ");
//Query the OBD-II-UART for the Vehicle Speed
OBD.println("010D");
//Get the response from the OBD-II-UART board. We get two responses
//because the OBD-II-UART echoes the command that is sent.
//We want the data in the second response.
getResponse();
getResponse();
//Convert the string data to an integer
vehicleSpeed = strtol(&rxData[6],0,16);
//Print the speed data to the Serial
Serial.print(vehicleSpeed);
Serial.print(" km/h");
delay(100);
//Delete any data that may be left over in the serial port.
Serial.flush();
//Move the serial cursor to the position where we want the RPM data.
//Clear the old RPM data, and then move the cursor position back.
Serial.print(" ");
//Query the OBD-II-UART for the Vehicle rpm
OBD.println("010C");
//Get the response from the OBD-II-UART board
getResponse();
getResponse();
//Convert the string data to an integer
//NOTE: RPM data is two bytes long, and delivered in 1/4 RPM from the OBD-II-UART
vehicleRPM = ((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4;
//Print the rpm data to the Serial
Serial.print("Giri minuto=");
Serial.print(vehicleRPM);
//Give the OBD bus a rest
delay(1000);
}
//The getResponse function collects incoming data from the UART into the rxData buffer
// and only exits when a carriage return character is seen. Once the carriage return
// string is detected, the rxData buffer is null terminated (so we can treat it as a string)
// and the rxData index is reset to 0 so that the next string can be copied.
void getResponse(void){
char inChar=0;
Serial.println("sono nella getresponde");
//Keep reading characters until we get a carriage return
while(inChar != '\r'){
Serial.println("leggo e aspetto il carattere di escape");
Serial.println(inChar);
//If a character comes in on the serial port, we need to act on it.
if(OBD.available() > 0){
Serial.println("arriva qualcosa");
//Start by checking if we've received the end of message character ('\r').
if(OBD.peek() == '\r'){
//Clear the Serial buffer
inChar=OBD.read();
//Put the end of string character on our data string
rxData[rxIndex]='\0';
//Reset the buffer index so that the next character goes back at the beginning of the string.
rxIndex=0;
}
//If we didn't get the end of message character, just add the new character to the string.
else{
//Get the new character from the Serial port.
inChar = OBD.read();
//Add the new character to the string, and increment the index variable.
rxData[rxIndex++]=inChar;
}
}
}
}
il codice l'ho lievemente modificato , le differenze principali sono che non uso un lcd per visualizzare i 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 i dati cercati ma mi arrivano sequenze strane di caratteri e la risposta all' atc command "atz" e' -1. Ho provato a "googlare " il problema ma non ho trovato nulla a riguardo , ho pensato solo che i problemi possano essere dovuti o all'utilizzo della software serial o al baudrate .
Qualcuno di voi ha mai riscontrato problemi del genere?
grazie a tutti!