Page 1 of 11 12345678910 ... LastLast
Results 1 to 10 of 106

Thread: OBDII / Elm MPG meter. Almost working but not quite!

  1. #1
    See me in my wet t-shirt. stevieg's Avatar
    Join Date
    Aug 2003
    Location
    Warwickshire, UK
    Posts
    1,887

    OBDII / Elm MPG meter for mediacar. Almost working but not quite!

    I've been working on a instant/average MPG meter to fit into MediaCar.

    I've got it almost working, I'm just having trouble on the last 1%

    The application appears to work out the MPG (UK gallon) correctly. It also works in 'test' mode where it reads from a logfile.

    I used jelm (http://www.sophonix.com/downloads/jelmemulator.asp) with some modifications to send back the pids I needs to test it while developing it.

    But when I get it in the car it doesn't work very fast (I had to turn echo on, linefeeds on even though it worked without in the emulator) and crashes eventually with dive by zero.

    I haven't found any VB code as a decent starting point really for the elm interface and now I seem to be going round in circles, developing it any further involves sitting in the car on the car pc with a keyboard so I can get live readings..

    Does anyone have any dcenet VB source code (VB6 or dot net) they are willing to share or can tell me how to fix the code I have got?

    I've attached the source code of what I have done so far. If anyone wants to use any of it by the way it is BSD license so basically you can do what you want with it.
    Attached Files Attached Files
    Ford Focus MP3 : www.stevieg.org/carpc Blog Updated 29 January 2009!
    Car PC Status: Complete - Undergoing Software Redevelopment

  2. #2
    Maximum Bitrate owenjh's Avatar
    Join Date
    Nov 2003
    Location
    NC
    Posts
    604
    I have an idea, if your using a com port use comfoolery and the port redirector and connect to it from your home PC if you have WiFi. That way you will have the luxury of your home PC with your live data.
    To resolve the divide by zero, try putting an if statement in - its probably because your speed is zero.
    How are you calculating your miles per gallon? When you finish it you should have an option for RPM's and have the user input how much gas there car uses at certain RPM's, or even better log the fuel (trim?) levels if its possible.
    Not that good at OBD-II yet, so it might sound like im talking out of my arse, but I plan on getting my interface soon.

    Im guessing I should buy one with the Elm interface since you have started some VB code :-P


    Looked at your code, won't work on this computer because jet isn't installed I see how your calculating now.
    CarComputer Status: New Car & Broken (Motherboard Fried)
    Owen JH | My Linux Blog | The Tech Fellows

  3. #3
    Raw Wave god_of_cpu's Avatar
    Join Date
    Jan 2004
    Location
    SilverSpring Maryland
    Posts
    2,960
    I wrote an OBD II plugin dll for the ELM that does all the hard work of talking to the COM port. You just tell it what PIDs you want to recieve, then you tell it to start polling. It will use overlapped I/O to contiously read in the values until you tell it to stop or want to change the PIDs its reading. Whenever you want a value for any of the PIDs you just call a function with the PID and it returns its last read in value. I'll try and post the plugin this weekend and how to use it.
    StreetDeck.com Developer (I am Chuck)
    Get StreetDeck at http://www.streetdeck.com
    The Official StreetDeck Forums have moved, please visit us at http://www.streetdeck.com/forum for official support for Streetdeck.

  4. #4
    Maximum Bitrate owenjh's Avatar
    Join Date
    Nov 2003
    Location
    NC
    Posts
    604
    Quote Originally Posted by god_of_cpu
    I wrote an OBD II plugin dll for the ELM that does all the hard work of talking to the COM port. You just tell it what PIDs you want to recieve, then you tell it to start polling. It will use overlapped I/O to contiously read in the values until you tell it to stop or want to change the PIDs its reading. Whenever you want a value for any of the PIDs you just call a function with the PID and it returns its last read in value. I'll try and post the plugin this weekend and how to use it.
    Which OBD-II reader are you using, so that I know the one I get will work with your pluggin? I will buy it A.S.A.P so I can get working with OBD-II and make some nifty little programs.


    Function Die(UserMessage As String, DebugMessage As String)
    Any reason why this ends the program when it can't communicate?
    CarComputer Status: New Car & Broken (Motherboard Fried)
    Owen JH | My Linux Blog | The Tech Fellows

  5. #5
    Raw Wave god_of_cpu's Avatar
    Join Date
    Jan 2004
    Location
    SilverSpring Maryland
    Posts
    2,960
    Also, the ELM interface won't give you a better response then 2.5 updates a second regardless of how good your code is and what you turn off.
    StreetDeck.com Developer (I am Chuck)
    Get StreetDeck at http://www.streetdeck.com
    The Official StreetDeck Forums have moved, please visit us at http://www.streetdeck.com/forum for official support for Streetdeck.

  6. #6
    Raw Wave god_of_cpu's Avatar
    Join Date
    Jan 2004
    Location
    SilverSpring Maryland
    Posts
    2,960
    Quote Originally Posted by owenjh
    Which OBD-II reader are you using, so that I know the one I get will work with your pluggin? I will buy it A.S.A.P so I can get working with OBD-II and make some nifty little programs.
    I'm currently using the scantool.net one, it should work with anything that uses the ELM chip though. However, the plugin interface is actually written so that the calling application writes to this single interface and multiple plugins can then be used. I've only written the one for the ELM interface, but ideally more plugins could be written for the different interfaces and the calling applications wouldn't have to change.

    The ELM is proving too slow to be really usable for me though, I plan on eventually buying a CAN based one and writing the plugin for it.
    StreetDeck.com Developer (I am Chuck)
    Get StreetDeck at http://www.streetdeck.com
    The Official StreetDeck Forums have moved, please visit us at http://www.streetdeck.com/forum for official support for Streetdeck.

  7. #7
    Maximum Bitrate owenjh's Avatar
    Join Date
    Nov 2003
    Location
    NC
    Posts
    604
    Quote Originally Posted by god_of_cpu
    I'm currently using the scantool.net one, it should work with anything that uses the ELM chip though. However, the plugin interface is actually written so that the calling application writes to this single interface and multiple plugins can then be used. I've only written the one for the ELM interface, but ideally more plugins could be written for the different interfaces and the calling applications wouldn't have to change.

    The ELM is proving too slow to be really usable for me though, I plan on eventually buying a CAN based one and writing the plugin for it.
    I do not believe I am advanced enough to write a plugin, although I may be able to figure it out if I could look at your source code. I might try to buy one with a different interface and write a module for it. It would be nice to have an application that can communicate to all types of OBD readers thru these plugins. I bet media player / mediacar would like that.

    How about the one from obddiagnostics.com
    I believe I might be able to write a plugin for that.
    CarComputer Status: New Car & Broken (Motherboard Fried)
    Owen JH | My Linux Blog | The Tech Fellows

  8. #8
    Raw Wave god_of_cpu's Avatar
    Join Date
    Jan 2004
    Location
    SilverSpring Maryland
    Posts
    2,960
    This is the exposed interface of the dll, most of these should be semi-self explainatory, but they are explained completely in the code files I'll post later.

    This code is in C++, you will have to covert the prototypes to VB.



    Code:
    INIT_DLL_FUNCTION(LPVOID, WINAPI, API_OBDII_Create,(HWND hWnd, IUnknown* lpUknown));
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Release,(LPVOID lpUser));
    
    INIT_DLL_FUNCTION_NAMED(VOID, WINAPI, GetModuleName,(CHAR* szName, INT iMaxLen), API_OBDII_GetModuleName)
    INIT_DLL_FUNCTION_NAMED(VOID, WINAPI, GetVersion,(CHAR* szVersion, INT iMaxLen), API_OBDII_GetVersion)
    INIT_DLL_FUNCTION_NAMED(VOID, WINAPI, GetGUID,(GUID* pguid), API_OBDII_GetGUID);
    INIT_DLL_FUNCTION_NAMED(INT, WINAPI, RequestedAPI,(INT iMaxAPI), API_OBDII_RequestedAPI);
    INIT_DLL_FUNCTION_NAMED(VOID, WINAPI, GetModuleComment,(CHAR* szComment, INT iMaxLen), API_OBDII_GetModuleComment);
    
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_SetMode, (LPVOID lpUser, enumOBDModes eOBDMode));
    INIT_DLL_FUNCTION(FLOAT, WINAPI, API_OBDII_Mode1_GetRefreshRate, (LPVOID lpUser));
    	
    INIT_DLL_FUNCTION(VOID, WINAPI, API_OBDII_Mode1_ResetData, (LPVOID lpUser));
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Mode1_IsPIDSupported, (LPVOID lpUser, enumOBDMode1PIDs eOBDPID));
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Mode1_AddOBDPID, (LPVOID lpUser, enumOBDMode1PIDs eOBDPID));
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Mode1_RemoveAllOBDPIDs, (LPVOID lpUser));
    
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Mode1_Start, (LPVOID lpUser));
    INIT_DLL_FUNCTION(VOID, WINAPI, API_OBDII_Mode1_GetPacket, (LPVOID lpUser, enumOBDMode1PIDs eOBDPID, API_OBDII_MSGPACKET* lpMsgPacket));
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Mode1_Stop, (LPVOID lpUser));
    
    
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Mode3_IsMILLightOn, (LPVOID lpUser));
    INIT_DLL_FUNCTION(INT, WINAPI, API_OBDII_Mode3_GetNumTroubleCodes, (LPVOID lpUser));
    INIT_DLL_FUNCTION(VOID, WINAPI, API_OBDII_Mode3_GetTroubleCodes, (LPVOID lpUser, DWORD* lpTroubleCodes, INT iMaxNumTrobleCodes));
    INIT_DLL_FUNCTION(BOOL, WINAPI, API_OBDII_Mode3_ClearTroubleCodes, (LPVOID lpUser));
    
    //*** OPTIONAL FUNCTIONS ***
    INIT_DLL_FUNCTION(INT, WINAPI, API_OBDII_ConfigurationDialog, (LPVOID lpUser));
    To read real time data after calling create, you would set the mode to read current data, then reset the PIDs to read and add only the PIDs you now want, then just start the plugin and it begins polling the COM port in a seperate thread, returning from the call immediatly.

    i.e.
    Code:
    	if (!m_lpAD->m_vdb.GetOBDIIPlugin()->SetMode(eOBDModeCurrentData))
    		TV_THROWMSGBOX("Could not set the OBD II mode to read current data!");
    
    	if (!m_lpAD->m_vdb.GetOBDIIPlugin()->Mode1_RemoveAllOBDPIDs())
    		TV_THROWMSGBOX("Could not remove PIDs from the OBD II module");
    
    	//Just monitor speed for now
    	if (!m_lpAD->m_vdb.GetOBDIIPlugin()->Mode1_AddOBDPID(eOBDPIDVehicleSpeed))
    		TV_THROWMSGBOX("Could not add a PID to monitor the OBD II plug-in for!");
    
    	if (!m_lpAD->m_vdb.GetOBDIIPlugin()->Mode1_Start())
    	{
    		TV_THROWMSGBOX("Could not start the OBD II plug-in!");
    	}
    In a timer in your program you would call Mode1_GetPacket with the PID you want and it would return the data in the following structure:

    Code:
    //OBD message packet recieved from the vehicle
    typedef struct API_OBDII_MSGPACKET {
    	DWORD m_dwTimeRecieved;
    	BYTE m_iNumDataBytes;
    	BYTE m_Header[API_OBDII_NUMHEADERBYTES];
    	BYTE m_Data[API_OBDII_NUMDATABYTES];
    };
    StreetDeck.com Developer (I am Chuck)
    Get StreetDeck at http://www.streetdeck.com
    The Official StreetDeck Forums have moved, please visit us at http://www.streetdeck.com/forum for official support for Streetdeck.

  9. #9
    See me in my wet t-shirt. stevieg's Avatar
    Join Date
    Aug 2003
    Location
    Warwickshire, UK
    Posts
    1,887
    Thanks, I can't wait for your DLL. 2.5 times a second will be OK for what I want to do.
    Ford Focus MP3 : www.stevieg.org/carpc Blog Updated 29 January 2009!
    Car PC Status: Complete - Undergoing Software Redevelopment

  10. #10
    See me in my wet t-shirt. stevieg's Avatar
    Join Date
    Aug 2003
    Location
    Warwickshire, UK
    Posts
    1,887
    Quote Originally Posted by owenjh
    I have an idea, if your using a com port use comfoolery and the port redirector and connect to it from your home PC if you have WiFi.
    Well to be honest I have been sitting in my house at my laptop wi-fi-ing into the car and using it from their. Simialr thing really, but a pain to have your car running with no-one in it.

    Quote Originally Posted by owenjh
    To resolve the divide by zero, try putting an if statement in - its probably because your speed is zero.
    Nope as if speed is zero it doesn't try to calculate but the thing is it worked fine in the emulator.
    Quote Originally Posted by owenjh
    How are you calculating your miles per gallon? When you finish it you should have an option for RPM's and have the user input how much gas there car uses at certain RPM's, or even better log the fuel (trim?) levels if its possible.
    the code is in the program. It gives in instant MPG for any given moment. It would be possible to log RPM along with the MPG data, and the debug mode already logs fuel trim to the DB, I just want something working first!

    Quote Originally Posted by owenjh
    Function Die(UserMessage As String, DebugMessage As String)
    Any reason why this ends the program when it can't communicate?
    it should always be communnicating and although there is a rally crap routine to reset it and try again if it loses communication, if it still has trouble I wanted it to just die and log the error. Once I get it working I need to get it to sort itself out on resume from standby, and find the PID for my ford's fuel level as the whole point of it to me is so I can see almost exactly how many miles are left in the tank, as well as when I'm being economical.
    Ford Focus MP3 : www.stevieg.org/carpc Blog Updated 29 January 2009!
    Car PC Status: Complete - Undergoing Software Redevelopment

Page 1 of 11 12345678910 ... LastLast

Similar Threads

  1. Anyone have workign VB code for elm scan?
    By infinkc in forum Engine Management, OBD-II, Engine Diagnostics, etc.
    Replies: 12
    Last Post: 06-11-2005, 06:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •