OBD Source Code -Section 1- Without Car / Section 2 - With car

Dependencies:   mbed

Revision:
0:e36d80703ed0
Child:
1:c23c05b36e33
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/obd_libraries.cpp	Mon Feb 27 06:12:23 2017 +0000
@@ -0,0 +1,259 @@
+#include "mbed.h"
+#include "obd_libraries.h"
+#include "common_definitions.h"
+
+Serial pco(USBTX, USBRX);
+Serial OBD_UART(PA_0, PA_1);
+
+char pass = 0;
+char reception_complete = 0;
+
+char obd_reset_cmd[]            =   "ATZ\r";
+char battery_voltage_cmd[]      =   "ATRV\r";
+char protocol_auto_detect_cmd[] =   "ATSP0\r";
+char read_CAN_protocol_cmd[]    =   "ATDPN\r";
+char allow_long_cmd[]           =   "ATAL\r";
+char engine_rpm_cmd[]           =   "010C\r";
+char vehicle_speed_cmd[]        =   "010D\r";
+char vin_number_cmd[]           =   "0902 5\r";
+
+float car_battery_voltage;
+long vehicle_speed;
+char OBD_UART_RX_Buffer[50];
+extern char OBD_RxBuffer_End_Pos;
+//char OBD_UART_RX_Size = 50;
+
+
+//Serial pco(USBTX, USBRX);
+//Serial OBD_UART(PA_0, PA_1);
+
+void OBD_onDataRx()
+{
+    //pcm.printf("\r\n ENTERED \r\n");
+     if(OBD_UART.readable()) {
+       
+        pco.putc(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos++] = OBD_UART.getc());
+        /*
+        if(OBD_RxBuffer_End_Pos >= OBD_UART_RX_Size) {
+            // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation.
+            // For now just throw everything away.
+            OBD_RxBuffer_End_Pos  = 0;
+        }
+        */
+    }
+}
+
+
+//************************************************************************************************************************
+
+void received_data_verification(char *rcv_data_pointer, char *ref_data_pointer, char num)
+{
+    char dummy_data[num], count;
+    for(count = 0; count < num; count++)
+    {
+        pco.putc(*rcv_data_pointer);
+        if(*rcv_data_pointer++ == *ref_data_pointer++)
+            pass = 1;
+        else   
+        {
+            pass = 0;
+            return;
+        }
+    } 
+}
+      
+//************************************************************************************************************************
+/*
+char* copy_characters(char *copy_char_pointer, char start_position, char end_position)
+{
+    char total_num;
+    char count;
+    total_num = end_position - start_position;
+    char copied_data[total_num];
+    copy_char_pointer += start_position;
+    
+    for(count = 0; count < total_num; count++)
+    {
+        copied_data[count] = *copy_char_pointer++;
+    }
+    
+    pco.printf("\r\n%s", copied_data);
+    
+    copy_char_pointer = copied_data;    // Shifting the pointer to copied_data array
+    
+    return copy_char_pointer;
+}
+*/        
+    
+
+//************************************************************************************************************************
+
+void process_battery_voltage(char *battery_voltage_pointer)
+{
+    char battery_voltage_data[4];   // One decimal point precision ( For ex : 12.5 )
+    char count;
+    battery_voltage_pointer += 5;   // ATRV<CR> counts to 5
+    for(count = 0; count < 4; count++)
+    {
+        battery_voltage_data[count] = *battery_voltage_pointer++;
+    }
+    car_battery_voltage = atof(battery_voltage_data);   // Converts the Battery Volatge from String to Float data type
+    
+    printf("\r\nCAR BATTERY VOLTAGE = %f",car_battery_voltage);
+}
+
+//************************************************************************************************************************
+
+void process_vehicle_speed(char *vehicle_speed_pointer)
+{
+    char vehicle_speed_data[2];     // Vehicle speed data is returned by a 2 byte value
+    char *strtol_pointer;
+    char count;
+    //"010D\r41 0D 4F\r\r>"
+    vehicle_speed_pointer += 11;
+    for(count = 0; count < 2; count++)
+    {
+        vehicle_speed_data[count] = *vehicle_speed_pointer++;
+    }
+    
+    vehicle_speed =  strtol(vehicle_speed_data, &strtol_pointer, 16);
+    
+    printf("\r\nVEHICLE SPEED = %ld\r\n", vehicle_speed);
+}
+
+//************************************************************************************************************************
+
+
+void fetch_battery_voltage()
+{
+    OBD_UART.printf(battery_voltage_cmd);
+    wait(1);
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    pco.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, battery_voltage_cmd, (strlen(battery_voltage_cmd)-1));
+    
+    process_battery_voltage(OBD_UART_RX_Buffer);
+
+    if(pass == 1)
+        printf("\r\nOBD READ BATTERY VOLTAGE SUCCESSFUL \r\n\r\n");
+    else
+        printf("\r\nOBD READ BATTERY VOLTAGE FAILED \r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+}
+    
+//************************************************************************************************************************   
+
+void fetch_vehicle_speed()
+{
+    char virtual_rx_speed_buffer[] = "010D\r41 0D 4F\r\r>";     
+    /*
+    OBD_UART.printf(vehicle_speed_cmd);
+    wait(1);
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    pco.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, allow_long_cmd, (strlen(vehicle_speed_cmd)-1));
+    */
+    process_vehicle_speed(virtual_rx_speed_buffer);
+
+    if(pass == 1)
+        printf("\r\VEHICLE SPEED DATA RECEIVED SUCCESSFULLY \r\n\r\n");
+    else
+        printf("\r\nVEHICLE SPEED DATA RECEPTION FAILED\r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+
+}  
+
+//************************************************************************************************************************
+/*
+void fetch_vin_number()
+{
+    char virtual_rx_vin_buffer[] = "0902 5\r014 \r0: 49 02 01 54 4D 42 \r1: 46 4B 4A 35 4A 32 43 \r2: 47 30 31 34 37 33 33 \r\r>"; 
+    */
+    
+    /*
+    Chassis Number : TMBFKJ5J2CG014733
+     
+    [TX] - 0902 5<CR>
+
+    [RX] - 0902 5<CR>
+    014 <CR>
+    0: 49 02 01 54 4D 42 <CR>
+    1: 46 4B 4A 35 4A 32 43 <CR>
+    2: 47 30 31 34 37 33 33 <CR>
+    <CR>
+    >
+    */
+    /*
+    OBD_UART.printf(vin_number_cmd);
+    wait(1);
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    pco.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, allow_long_cmd, (strlen(vin_number_cmd)-1));
+    */  
+
+//************************************************************************************************************************
+/*
+void check_for_dtc()
+{
+*/    
+
+//************************************************************************************************************************
+
+void initialize_obd()
+{
+    char data[3];
+    char *data_pointer;  
+
+    data_pointer = data;
+    
+    pco.baud(38400);
+    OBD_UART.baud(38400);
+    
+    OBD_UART.attach(&OBD_onDataRx);
+    
+    OBD_UART.printf("%s",obd_reset_cmd);
+
+    wait(1);
+    
+    /*
+    pco.printf("Reception not completed");
+    pco.printf("\r\n%d",OBD_RxBuffer_End_Pos);
+    pco.printf("\r\n%c",OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1]);
+    */
+    
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    pco.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, obd_reset_cmd, (strlen(obd_reset_cmd)-1));
+
+    if(pass == 1)
+        printf("\r\nOBD RESET SUCCESSFUL \r\n\r\n");
+    else
+        printf("\r\nOBD RESET FAILED \r\n\r\n");
+        
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+    
+//--------------------------------------------------------------------------------------------------------------------------  
+
+    OBD_UART.printf(allow_long_cmd);
+    wait(1);
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    pco.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, allow_long_cmd, (strlen(allow_long_cmd)-1));
+
+    if(pass == 1)
+        printf("\r\nLONG DATA RECEPTION ENABLED SUCCESSFULLY \r\n\r\n");
+    else
+        printf("\r\nLONG DATA RECEPTION ENABLING FAILED\r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+    
+}
+        
+//************************************************************************************************************************
+
+
+
+