Code Sample
private void ISRDecode(byte[] byteData)
{
byte rpmLowRaw = byteData[6]; // Low Byte of RPM
byte rpmHighRaw = byteData[7]; // High Byte of RPM
byte ignRaw = byteData[19]; // Ignition Timing Raw
byte o2Raw = byteData[2]; // Oxygen Sensor Byte
byte vtecRaw = byteData[23]; // Vtec Engaged Yes/No
byte vssRaw = byteData[16]; // Vehicle Speed Sensor Byte
byte batteryRaw = byteData[25]; // Battery Byte
float mapRaw = byteData[4]; // Map Sensor Byte
byte tpsRaw = byteData[5]; // Throttle Position Sensor Byte
byte iatRaw = byteData[1]; // Intake Air Temp Byte
byte ectRaw = byteData[0]; // Engine Coolant Temp Byte
byte injLowRaw = byteData[17]; // Injector low Byte
byte injHighRaw = byteData[18]; // Injector high Byte
byte knockraw = byteData[10]; // Knock sensor Byte
byte cel1 = FixMil(byteData[12]); // Cel Byte1
byte cel2 = FixMil(byteData[13]); // Cel Byte2
byte cel3 = FixMil(byteData[14]); // Cel Byte3
byte cel4 = FixMil(byteData[15]); // Cel Byte4
//RPM
float rpm = 1875000 / ((rpmHighRaw * 256) + rpmLowRaw); //valid
double injms = ((256.0 * injHighRaw) + injLowRaw) * 3.20000004768372 / 1000.0;
double dutycycle = Math.Round(rpm * injms / 1200, 0, MidpointRounding.AwayFromZero);
//Knock retard
double knock = (0.25 * knockraw);
//Ignition Advance
double ign = (0.25 * ignRaw) - 6;
//O2 sensor voltage 0-5V
double O2 = o2Raw / 51.0;
//TPS %
double TPS = (0.472637 * tpsRaw) - 11.46119;
//Battery Voltage
double battery = (26.0 * batteryRaw) / 270.0;
//Map Voltage
float mapvolt = mapRaw / 51;
//Map Mbar
float map = (1764 / 255) * (mapRaw) * 2 + 6;
//Map inghg
double inHg = -10.87 * ((mapRaw) * 5 / 256) + 30.48;
//vtec code
var bits = new BitArray(vtecRaw);
bool vtec = bits[0];
//Speed kph
double speed = vssRaw; //Do what ever conversion here for ratio of wheels.
//IAT C
double iat = iatRaw;
iat = iat / 51;
iat = (0.1423 * Math.Pow(iat, 6)) - (2.4938 * Math.Pow(iat, 5)) + (17.837 * Math.Pow(iat, 4)) - (68.698 * Math.Pow(iat, 3)) + (154.69 * Math.Pow(iat, 2)) - (232.75 * iat) + 284.24;
iat = ((iat - 32) * 5) / 9;
//ECT C
double ect = ectRaw;
ect = ect / 51;
ect = (0.1423 * Math.Pow(ect, 6)) - (2.4938 * Math.Pow(ect, 5)) + (17.837 * Math.Pow(ect, 4)) - (68.698 * Math.Pow(ect, 3)) + (154.69 * Math.Pow(ect, 2)) - (232.75 * ect) + 284.24;
ect = ((ect - 32) * 5) / 9;
// CRC CHECKING CODE HERE
int checksum = byteData[0];
int x = 0;
while (x < 50)
{
x++;
checksum = checksum + byteData[x];
}
byte calchecksum = (byte)(checksum & 0xff);
byte realchecksum = (byte)(byteData[51] & 0xff);
if (calchecksum == realchecksum)
{
//Check sum matched code here
//MessageBox.Show("checksum match");
}
else
{
//Checksum failed code here.
}
// Read error codes
string errorstring = Reverse(ToBitsString(FixMil(cel1))) + Reverse(ToBitsString(cel2)) + Reverse(ToBitsString(cel3)) + Reverse(ToBitsString(cel4)); ;
char[] OBD1DTCL = errorstring.ToCharArray();
int i = 0;
while (i < 31)
{
if (OBD1DTCL[i].ToString() == "1")
{
int milfix = i + 1;
switch (milfix)
{
case 24:
MessageBox.Show("30");
break;
case 25:
MessageBox.Show("31");
break;
case 26:
MessageBox.Show("36");
break;
case 27:
MessageBox.Show("41");
break;
case 28:
MessageBox.Show("43");
break;
case 29:
MessageBox.Show("45");
break;
case 30:
MessageBox.Show("48");
break;
default:
MessageBox.Show((i + 1).ToString());
break;
}
}
i++;
}
}
public byte FixMil(byte Value)
{
switch (Value)
{
case 0x18:
return 0x30;
case 0x19:
return 0x1f;
case 0x1a:
return 0x24;
case 0x1b:
return 0x29;
case 0x1c:
return 0x2b;
default:
return Value;
}
}
//Converts a byte to a 8 bit
public static string ToBitsString(byte value)
{
return Convert.ToString(value, 2).PadLeft(8, '0');
}
//Inverts the bit string to make storing error codes as a 0-32 bit array easier.
public string Reverse(string text)
{
if (text == null) return null;
char[] array = text.ToCharArray();
Array.Reverse(array);
return new String(array);
}
}