OBD2 interfacing problem
Posted: Wed Mar 05, 2014 12:44 pm
Hi guys,
I am trying to get speed data from the car through OBD interface port. But my program is not working. I am new to raspberry pi programming so could you please help me. I have used WiringPi because the programming is similar to arduino and pretty much simple.
The output I get is
numchar=3
ELM Version=ATI
numchar=4
Speed Data=010D
I think it is looping back the data I transmit.
I am trying to get speed data from the car through OBD interface port. But my program is not working. I am new to raspberry pi programming so could you please help me. I have used WiringPi because the programming is similar to arduino and pretty much simple.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
int main()
{
int fd;
int i,j,k,numchar;
char elm[10],data[10];
fd=serialOpen("/dev/ttyUSB0",38400);
if(fd<0)
{
fprintf(stderr,"Unable to open serial device:%s\n",strerror(errno));
return 1;
}
while(1)
{
for(i=0;i<10;i++)
data[i]='\0';
for(i=0;i<10;i++)
elm[i]='\0';
serialPrintf(fd,"ATI");
delay(1000);
numchar=serialDataAvail(fd);
printf("numchar=%d\n",numchar);
if(numchar!=-1)
{
for(j=0;j<numchar;j++)
elm[j]=serialGetchar(fd);
printf("ELM Version=%s\n",elm);
}
else
printf("Read Error");
serialFlush(fd);
serialPrintf(fd,"010D");
delay(1000);
numchar=serialDataAvail(fd);
printf("numchar=%d\n",numchar);
if(numchar!=-1)
{
for(k=0;k<numchar;k++)
data[k]=serialGetchar(fd);
printf("Speed Data=%s\n",data);
}
else
printf("Read Error");
}
serialClose(fd);
return(0);
}
numchar=3
ELM Version=ATI
numchar=4
Speed Data=010D
I think it is looping back the data I transmit.