1 #ifndef _OBDPID_H__H_H_
2 #define _OBDPID_H__H_H_
6 #include <vehicleproperty.h>
13 typedef std::vector<unsigned char> ByteArray;
15 ObdPid(VehicleProperty::Property prop, std::string p,
int i)
16 :property(prop), pid(p), id(i), type(0x41)
20 static ByteArray cleanup(ByteArray replyVector)
24 for (
int i=0;i<replyVector.size();i++)
26 if ((replyVector[i] != 0x20) && (replyVector[i] !=
'\r') && (replyVector[i] !=
'\n'))
28 tmp.push_back(replyVector[i]);
33 static ByteArray compress(ByteArray replyVector)
36 for (
int i=0;i<replyVector.size()-1;i++)
38 tmp.push_back(obdLib::byteArrayToByte(replyVector[i], replyVector[i+1]));
43 virtual ObdPid* create() = 0;
45 bool tryParse(ByteArray replyVector)
47 if (!isValid(replyVector))
54 virtual void parse(
const ByteArray &replyVector) = 0;
55 virtual bool isValid(
const ByteArray &replyVector) = 0;
56 virtual bool needsCompress() {
return true; }
58 VehicleProperty::Property property;
71 CopyMe(VehicleProperty::Property prop, std::string p,
int i)
80 t->isValidVal = isValidVal;
91 :
CopyMe(VehicleProperty::VehicleSpeed,
"010D1\r", 0x0D)
95 bool isValid(
const ByteArray &replyVector)
98 if (replyVector[1] !=
id)
100 return isValidVal =
false;
105 void parse(
const ByteArray &replyVector)
113 int mph = replyVector[2];
114 value = boost::lexical_cast<std::string>(mph);
127 bool isValid(
const ByteArray &replyVector)
129 if (replyVector[1] !=
id)
137 void parse(
const ByteArray &replyVector)
144 double rpm = ((replyVector[2] << 8) + replyVector[3]) / 4.0;
145 value = boost::lexical_cast<std::string>(rpm);
154 :
CopyMe(VehicleProperty::EngineCoolantTemperature,
"01051\r",0x05)
158 bool isValid(
const ByteArray &replyVector)
160 if (replyVector[1] !=
id)
168 void parse(
const ByteArray &replyVector)
175 int temp = replyVector[2] - 40;
176 value = boost::lexical_cast<std::string>(temp);
185 :
CopyMe(VehicleProperty::MassAirFlow,
"01101\r", 0x10)
189 bool isValid(
const ByteArray &replyVector)
191 if (replyVector[1] !=
id)
199 void parse(
const ByteArray &replyVector)
207 maf = ((replyVector[2] << 8) + replyVector[3]) / 100.0;
208 value = boost::lexical_cast<std::string>(maf);
224 bool isValid(
const ByteArray &replyVector)
226 return isValidVal = MassAirFlowPid::isValid(replyVector);
228 void parse(
const ByteArray & replyVector)
237 clock_gettime(CLOCK_REALTIME, &t);
239 double currentTime = t.tv_sec + t.tv_nsec / 1000000;
241 double diffTime = currentTime - oldTime;
242 oldTime = currentTime;
244 double consumption = 1 / (14.75 * 6.26) * maf * diffTime/60;
246 value = boost::lexical_cast<std::string>(consumption);
251 static double oldTime;
260 :
CopyMe(VehicleProperty::VIN,
"0902\r",0x02)
264 bool isValid(
const ByteArray & replyVector)
268 if (replyVector[0] != 0x49 || replyVector[1] != 0x02)
275 void parse(
const ByteArray & replyVector)
282 std::string vinstring;
283 for (
int j=0;j<replyVector.size();j++)
285 if(replyVector[j] == 0x49 && replyVector[j+1] == 0x02)
290 if (replyVector[j] != 0x00)
292 vinstring += (char)replyVector[j];
310 bool isValid(
const ByteArray & replyVector)
312 return isValidVal = VinPid::isValid(replyVector);
314 void parse(
const ByteArray &replyVector)
321 VinPid::parse(replyVector);
322 value = value.substr(0,3);
330 :
CopyMe(VehicleProperty::AirIntakeTemperature,
"010F1\r",0x0F)
334 bool isValid(
const ByteArray & replyVector)
336 if (replyVector[1] !=
id)
344 void parse(
const ByteArray & replyVector)
351 int temp = replyVector[2] - 40;
352 value = boost::lexical_cast<std::string>(temp);
360 :
CopyMe(VehicleProperty::EngineLoad,
"01041\r",0x04)
364 bool isValid(
const ByteArray & replyVector)
366 if (replyVector[1] !=
id)
374 void parse(
const ByteArray &replyVector)
381 int load = replyVector[2]*100.0/255.0;
382 value = boost::lexical_cast<std::string>(load);
390 :
CopyMe(VehicleProperty::ThrottlePosition,
"01111\r",0x11)
394 bool isValid(
const ByteArray & replyVector)
396 if (replyVector[1] !=
id)
404 void parse(
const ByteArray & replyVector)
411 int temp = replyVector[2]*100.0/255.0;
412 value = boost::lexical_cast<std::string>(temp);
425 bool needsCompress() {
return false; }
427 bool isValid(
const ByteArray & replyVector)
429 if(replyVector[replyVector.size() - 1] ==
'V')
431 return isValidVal =
true;
436 void parse(
const ByteArray & replyVector)
439 for(
int i=0; i<replyVector.size() - 1; i++)
441 value += replyVector[i];