Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of CAN_bus_reader_pub by
ecu_reader.cpp
00001 #include "mbed.h" 00002 #include "ecu_reader.h" 00003 #include "globals.h" 00004 00005 00006 // Use a timer to see if things take too long 00007 Timer CANTimer; 00008 namespace mbed { 00009 00010 00011 ecu_reader::ecu_reader(int can_speed) 00012 { 00013 can2.frequency(can_speed); 00014 } 00015 00016 //#define TIMEOUT 200 00017 unsigned char ecu_reader::request(unsigned char pid, char *buffer) 00018 { 00019 char can_msg[8]; 00020 float engine_data; 00021 00022 led1 = 1; 00023 00024 can_msg[0] = 0x02; 00025 can_msg[1] = 0x01; 00026 can_msg[2] = pid; 00027 can_msg[3] = 0; 00028 can_msg[4] = 0; 00029 can_msg[5] = 0; 00030 can_msg[6] = 0; 00031 can_msg[7] = 0; 00032 00033 if (can2.write(CANMessage(PID_REQUEST, can_msg, 8))) { 00034 00035 } 00036 00037 led1 = 0; 00038 CANTimer.reset(); 00039 CANTimer.start(); 00040 00041 while(CANTimer.read_ms() < TIMEOUT) { 00042 00043 if (can2.read(can_MsgRx)) { 00044 00045 if((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid)) 00046 { 00047 switch(can_MsgRx.data[2]) 00048 { /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */ 00049 case ENGINE_RPM: // ((A*256)+B)/4 [RPM] 00050 engine_data = ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/4; 00051 sprintf(buffer,"%d rpm ",(int) engine_data); 00052 break; 00053 00054 case ENGINE_COOLANT_TEMP: // A-40 [degree C] 00055 engine_data = can_MsgRx.data[3] - 40; 00056 sprintf(buffer,"%d degC ",(int) engine_data); 00057 00058 break; 00059 00060 case VEHICLE_SPEED: // A [km] 00061 engine_data = can_MsgRx.data[3]; 00062 sprintf(buffer,"%d km ",(int) engine_data); 00063 00064 break; 00065 00066 case MAF_SENSOR: // ((256*A)+B) / 100 [g/s] 00067 engine_data = ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/100; 00068 sprintf(buffer,"%d g/s",(int) engine_data); 00069 00070 break; 00071 00072 case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc) 00073 engine_data = can_MsgRx.data[3]*0.005; 00074 sprintf(buffer,"%d v ",(int) engine_data); 00075 00076 case THROTTLE: // 00077 engine_data = (can_MsgRx.data[3]*100)/255; 00078 sprintf(buffer,"%d %% ",(int) engine_data); 00079 00080 00081 break; 00082 } 00083 00084 return 1; 00085 00086 } 00087 00088 } 00089 } 00090 00091 return 0; 00092 00093 00094 00095 00096 } 00097 } // namespace mbed
Generated on Mon Aug 1 2022 03:16:42 by
1.7.2
