vishnu05nan
Posts: 1
Joined: Thu Aug 15, 2013 7:03 am

OBD2 interfacing problem

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.

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);
}

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.

DriftonAloft
Posts: 3
Joined: Sun Jun 01, 2014 7:00 am

Re: OBD2 interfacing problem

Sun Jun 01, 2014 7:09 am

so does the interface expect ascii character data or hex data ?

mrdebug
Posts: 72
Joined: Sun Apr 07, 2013 6:08 pm

Re: OBD2 interfacing problem

Mon Jun 09, 2014 7:37 am

Do you use a bluetooth interface?
www. ???
www. ???
GMT+1
Skype: mrdebug

Return to “C/C++”