I've written a c# app that reads data from my cars OBD port using the OBDPro scantool. My program loops through my "Command" objects and sends commands to a serial port connection object. I'm only sending 4 commands on each loop; Speed, RPM, CoolantTemp and Throttle position. After all four have been sent the application sleeps for 1 seconds and resumes the loop. Meanwhile I have an event listener on the Serial Port object that fires anytime data appears on the serial ports buffer. When it does I read all the characters until I find a carriage return and convert that hex string to a readable integer value and update my windows form. However whenever I send 4 or more commands on each iteration I often get "NO DATA" or ? from the cars OBD port for at least one of the commands. Before talk to the OBD port I first send ate0 and atl0 to suppress echoes and linefeeds. The baud rate is also @ 128000. ScanTool.net doesn't seem to behave this way. Can anyone give some pointers on how to make the responses more reliable? Here are a few snippets of my code:
My control loop:
Code:
private void BeginControlLoopThread()
{
while (true)
{
foreach (OBDCommand obdCommand in mOBDCommandRepository.OBDCommands)
{
mSerialPort.WriteLine(obdCommand.Mode + " " + obdCommand.PID);
mMainWindow.WriteToInputLogDisplay(obdCommand.Mode + " " + obdCommand.PID);
}
Thread.Sleep(1000);
}
}
My event listener:
Code:
private void SerialPort_DataReceived(object sender, EventArgs e)
{
string response = "";
try
{
response = mSerialPort.ReadTo("\r>");
}
catch (Exception exception)
{
// do nothing, exit
// exception.Message.ToString();
return;
}
response = response.Trim();
mMainWindow.WriteToOutputLogDisplay(response);
if (response.StartsWith("4"))
{
string[] translatedResponse = mOBDCommandRepository.TranslateResponse(response);
SendToUI(translatedResponse);
}
}