+ DiagnosticRequest request = {
+ arbitration_id: OBD2_FUNCTIONAL_BROADCAST_ID,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request, NULL);
+
+ if(handle.completed) {
+ if(!handle.success) {
+ // something happened and it already failed - possibly we aren't
+ // able to send CAN messages
+ return;
+ } else {
+ // this should never occur right away - you need to receive a fresh
+ // CAN message first
+ }
+ } else {
+ while(true) {
+ // Continue to read from CAN, passing off each message to the handle.
+ // This will return a 'completed' DiagnosticResponse when the when
+ // the request is completely sent and the response is received
+ // (which may take more than 1 CAN frames)
+ DiagnosticResponse response = diagnostic_receive_can_frame(&shims,
+ &handle, can_message_id, can_data, sizeof(can_data));
+
+ if(response.completed && handle.completed) {
+ if(handle.success) {
+ if(response.success) {
+ // The request was sent successfully, the response was
+ // received successfully, and it was a positive response - we
+ // got back some data!
+ printf("The DTCs are: ");
+ for(int i = 0; i < response.payload_length; i++) {
+ printf("0x%x ", response.payload[i]);
+ }
+ } else {
+ // The request was sent successfully, the response was
+ // received successfully, BUT it was a negative response
+ // from the other node.
+ printf("This is the error code: %d", response.negative_response_code);
+ }
+ } else {
+ // Some other fatal error ocurred - we weren't able to send
+ // the request or receive the response. The CAN connection
+ // may be down.
+ }
+ }
+ }
+ }