📄 trouble_code_reader.c
字号:
break;
case MSG_READY:
// if we're not reading or clearing codes, and the button is disabled,
if (d->flags & D_DISABLED)
{
d->flags &= ~D_DISABLED; // enable it
return D_REDRAWME;
}
break;
}
ret = d_button_proc(msg, d, c);
if (ret == D_CLOSE)
{
if (simulate)
trouble_codes_simulator(TRUE);
else
read_codes();
return D_REDRAWME;
}
return ret;
}
int clear_codes_proc(int msg, DIALOG *d, int c)
{
int ret;
switch (msg)
{
case MSG_READ_CODES: case MSG_CLEAR_CODES:
// if we are currently reading or clearing codes, and the button is enabled,
if (!(d->flags & D_DISABLED))
{
d->flags |= D_DISABLED; // disable the button
return D_REDRAWME;
}
break;
case MSG_READY:
// if we're not reading or clearing codes, and the button is disabled,
if (d->flags & D_DISABLED)
{
d->flags &= ~D_DISABLED; // enable it
return D_REDRAWME;
}
break;
}
ret = d_button_proc(msg, d, c);
if (ret == D_CLOSE)
{
while (!simulate && comport.status != READY)
{
if (alert("Port is not ready.", "Please check that you specified the correct port", "and that no other application is using it", "Configure &Port", "&Cancel", 'p', 'c') == 1)
display_options(); // let the user choose correct settings
else
return D_REDRAWME;
}
// if user confirmed that he wants to erase the codes,
if(popup_dialog(confirm_clear_dialog, 6) == 5)
{
if (simulate)
trouble_codes_simulator(FALSE);
else
broadcast_dialog_message(MSG_CLEAR_CODES, 0); //send everyone MSG_CLEAR_CODES
}
return D_REDRAWME;
}
return ret;
}
// heart of the trouble_code_reader module:
int tr_code_proc(int msg, DIALOG *d, int c)
{
static char vehicle_response[1024]; // character buffer for car response
static int first_read_occured = FALSE;
static int receiving_response = FALSE; // flag, "are we receiving response?"
static int verifying_connection = FALSE; // flag, "are we verifying connection?"
static int current_request = CRQ_NONE; // NUM_OF_CODES, READ_CODES, CLEAR_CODES
int response_status = EMPTY; // EMPTY, DATA, PROMPT
int response_type; // BUS_BUSY, BUS_ERROR, DATA_ERROR, etc.
int pending_codes_cnt = 0;
char comport_buffer[256]; // temporary storage for comport data
char buf1[64];
char buf2[64];
switch (msg)
{
case MSG_IDLE:
if (!first_read_occured)
{
if (!simulate)
read_codes();
first_read_occured = TRUE;
return D_O_K;
}
if (simulate)
break;
if (comport.status == READY)
{
if (!receiving_response)
{
if (verifying_connection)
{
send_command("0100"); // send request that requires a response
receiving_response = TRUE; // now we're waiting for response
vehicle_response[0] = 0; //get buffer ready for the response
start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer
}
else if (current_request == READ_PENDING)
{
send_command("07"); // request "pending" codes
receiving_response = TRUE; // and receiving response
vehicle_response[0] = '\0'; // clear the buffer
start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer...
}
}
else
{
response_status = read_comport(comport_buffer);
if (response_status == DATA) // if data detected in com port buffer
{
// append contents of comport_buffer to vehicle_response:
strcat(vehicle_response, comport_buffer);
start_serial_timer(OBD_REQUEST_TIMEOUT); // we got data, reset the timer
}
else if (response_status == PROMPT) // if ">" is detected
{
receiving_response = FALSE; // we're not waiting for response any more
stop_serial_timer(); // stop the timer
// append contents of comport_buffer to vehicle_response:
strcat(vehicle_response, comport_buffer);
if (verifying_connection) // *** if we are verifying connection ***
{ // NOTE: we only get here if we got "NO DATA" somewhere else
response_type = process_response("0100", vehicle_response);
verifying_connection = FALSE; // we're not verifying connection anymore
if (response_type == HEX_DATA) // if everything seems to be fine now,
{
if (current_request == CLEAR_CODES)
alert("There may have been a temporary loss of connection.", "Please try clearing codes again.", NULL, "OK", NULL, 0, 0);
else if (current_request == NUM_OF_CODES)
alert("There may have been a temporary loss of connection.", "Please try reading codes again.", NULL, "OK", NULL, 0, 0);
else if (current_request == READ_CODES)
{
current_request = READ_PENDING;
break;
}
}
else if (response_type == ERR_NO_DATA)
{
if (current_request == CLEAR_CODES) // if we were clearing codes,
alert("Communication problem: vehicle did not confirm successful", "deletion of trouble codes. Please check connection to the vehicle,", "make sure the ignition is ON, and try clearing the codes again.", "OK", NULL, 0, 0);
else // if we were reading codes or requesting number or DTCs
alert("There may have been a loss of connection.", "Please check connection to the vehicle,", "and make sure the ignition is ON", "OK", NULL, 0, 0);
}
else
display_error_message(response_type, FALSE);
broadcast_dialog_message(MSG_READY, 0); // tell everyone we're done
}
else if (current_request == NUM_OF_CODES) // *** if we are getting number of codes ***
{
response_type = process_response("0101", vehicle_response);
if (response_type == ERR_NO_DATA) // if we received "NO DATA"
verifying_connection = TRUE; // verify connection
else if (response_type != HEX_DATA) // if we got an error,
handle_errors(response_type, NUM_OF_CODES); // handle it
else // if process response returned HEX_DATA (i.e. there are no errors)
{ // extract # of codes from vehicle_response
num_of_codes_reported = handle_num_of_codes(vehicle_response);
send_command("03"); // request "stored" codes
current_request = READ_CODES; // we're reading stored codes now
receiving_response = TRUE; // and receiving response
vehicle_response[0] = '\0'; // clear the buffer
start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer...
}
}
else if (current_request == READ_CODES) // if we are reading codes,
{
response_type = process_response("03", vehicle_response);
if (response_type == ERR_NO_DATA) // vehicle didn't respond, check connection
{
if (num_of_codes_reported > 0)
verifying_connection = TRUE;
else
current_request = READ_PENDING;
}
else if (response_type == HEX_DATA)
{
handle_read_codes(vehicle_response, FALSE);
current_request = READ_PENDING;
}
else // if we got an error
handle_errors(response_type, READ_CODES);
}
else if(current_request == READ_PENDING) // if we are reading pending codes,
{
response_type = process_response("07", vehicle_response);
if (response_type == ERR_NO_DATA)
{
if (get_number_of_codes() == 0 && num_of_codes_reported == 0)
alert("No Diagnostic Trouble Codes (DTCs) detected", NULL, NULL, "OK", NULL, 0, 0);
}
else if(response_type != HEX_DATA) // if we got an error,
{
handle_errors(response_type, READ_PENDING);
break;
}
else // if there were *no* errors,
pending_codes_cnt = handle_read_codes(vehicle_response, TRUE);
// if number of DTCs reported by 0101 request does not equal either number or total DTCs or just stored DTCs
if ((get_number_of_codes() != num_of_codes_reported) && (get_number_of_codes() - pending_codes_cnt != num_of_codes_reported))
{
sprintf(buf1, "Vehicle reported %i Diagnostic Trouble Codes (DTCs).", num_of_codes_reported);
sprintf(buf2, "However, %i DTC(s) have been successfully read.", get_number_of_codes());
alert(buf1, buf2, "Try reading codes again.", "OK", NULL, 0, 0);
}
populate_trouble_codes_list();
broadcast_dialog_message(MSG_READY, 0); // tell everyone we're done
}
else if(current_request == CLEAR_CODES)
{
response_type = process_response("04", vehicle_response);
if (response_type == ERR_NO_DATA)// vehicle didn't respond, check connection
verifying_connection = TRUE;
else if(response_type != HEX_DATA) // if we got an error,
handle_errors(response_type, CLEAR_CODES);
else // if everything's fine (received confirmation)
{
clear_trouble_codes();
num_of_codes_reported = 0;
mil_is_on = FALSE;
broadcast_dialog_message(MSG_READY, 0);
}
}
}
else if (serial_time_out) // if request timed out,
{
stop_serial_timer();
receiving_response = FALSE;
num_of_codes_reported = 0;
mil_is_on = FALSE;
clear_trouble_codes();
broadcast_dialog_message(MSG_READY, 0);
if(alert("Device is not responding.", "Please check that it is connected", "and the port settings are correct", "OK", "&Configure Port", 27, 'c') == 2)
display_options(); // let the user choose correct settings
while (comport.status == NOT_OPEN)
{
if (alert("Port is not ready.", "Please check that you specified the correct port", "and that no other application is using it", "&Configure Port", "&Ignore", 'c', 'i') == 1)
display_options(); // let the user choose correct settings
else
comport.status = USER_IGNORED;
}
}
}
}
break; // end case MSG_IDLE
case MSG_START:
first_read_occured = FALSE;
num_of_codes_reported = 0;
mil_is_on = FALSE;
// fall through
case MSG_READY:
receiving_response = FALSE;
verifying_connection = FALSE;
current_request = CRQ_NONE;
break;
case MSG_READ_CODES:
if (comport.status == READY)
{
send_command("0101"); // request number of trouble codes
start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer
current_request = NUM_OF_CODES;
receiving_response = TRUE; // now we're waiting for response
vehicle_response[0] = 0;
clear_trouble_codes();
num_of_codes_reported = 0;
mil_is_on = FALSE;
}
else
serial_time_out = TRUE;
break;
case MSG_CLEAR_CODES:
if (comport.status == READY)
{
send_command("04"); // "clear codes" request
current_request = CLEAR_CODES;
receiving_response = TRUE; // now we're waiting for response
vehicle_response[0] = 0;
start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer
}
else
serial_time_out = TRUE;
break;
case MSG_END:
stop_serial_timer();
break;
}
return D_O_K;
} // end of tr_codes_proc()
int code_list_proc(int msg, DIALOG *d, int c)
{
static int curr_num_of_codes = 0;
int num_of_codes = get_number_of_codes();
int ret;
if (msg == MSG_READY)
{
if ((num_of_codes == 0) || (curr_num_of_codes != num_of_codes))
{
d->d1 = 0;
d->d2 = 0;
current_code_index = 0;
curr_num_of_codes = num_of_codes;
}
return D_REDRAWME;
}
ret = d_list_proc(msg, d, c);
if (current_code_index != d->d1)
{
current_code_index = d->d1;
broadcast_dialog_message(MSG_READY, 0);
}
return ret;
}
char* code_list_getter(int index, int *list_size)
{
if (index < 0)
{
*list_size = get_number_of_codes();
return NULL;
}
return get_trouble_code(index)->code;
}
// Mfr./SAE/ISO ranges are defined in SAE J2012 standard
int is_mfr_code(const char *code)
{
int num;
if (!code)
return FALSE;
switch (code[1])
{
case '0':
return FALSE;
case '1':
return TRUE;
case '2':
if (code[0] == 'P')
return FALSE;
return TRUE;
case '3':
num = strtol(code + 1, NULL, 16);
if (code[0] == 'P' && num >= 0x3000 && num <= 0x3399)
return TRUE;
return FALSE;
}
return TRUE;
}
int handle_num_of_codes(char *vehicle_response)
{
int temp;
char *response = vehicle_response;
char buf[16];
int ret = 0;
while (*response)
{
if (find_valid_response(buf, response, "4101", &response))
{
buf[6] = 0;
temp = (int)strtol(buf + 4, NULL, 16); // convert hex ascii string to integer
if (temp & 0x80)
mil_is_on = TRUE; // get MIL status from temp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -