doc:lua:tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
doc:lua:tutorial [2011/06/26 19:26] – admin | doc:lua:tutorial [2014/03/02 07:27] (current) – removed admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== The OOBD Lua Tutorial ====== | ||
- | While the page [[start|Lua scripts in OOBD]] tells you the basics about how Lua works inside OOBD, this tutorial will show you how to use Lua to realize your own ideas. | ||
- | |||
- | In this tutorial we'll read the VIN number from a vehicle. While doing that, we'll create a menu, identify the hardware, use the OOBD Lua function library, talk to the vehicle and show the result. | ||
- | |||
- | First we'll need a Menu on the screen to be able to start anything at all. That we'll do with the following code sequence | ||
- | |||
- | |||
- | <code lua> | ||
- | function Start(oldvalue, | ||
- | identifyOOBDInterface() | ||
- | openPage(" | ||
- | addElement(" | ||
- | pageDone() | ||
- | return oldvalue | ||
- | end | ||
- | |||
- | |||
- | ----------------- Do the initial settings -------------- | ||
- | |||
- | Start("","" | ||
- | return | ||
- | </ | ||
- | |||
- | So what happens here? When the Lua interpreter runs through the script, it finally finds the | ||
- | |||
- | | ||
- | |||
- | command at the end. This is one of the naming conventions in OOBD: The function, which initializes everything, must be called " | ||
- | |||
- | When we looking into the //Start// function, we'll find | ||
- | |||
- | identifyOOBDInterface() | ||
- | | ||
- | This a function out of the serial_dxm.lua support library. In there you can find several needful functions, which are used by more or less all OOBD scripts, so we've put these functions into a common file. | ||
- | |||
- | // | ||
- | |||
- | |||
- | The command sequence // | ||
- | |||
- | |||
- | <code lua> | ||
- | function vin(oldvalue, | ||
- | echoWrite(" | ||
- | udsLen=receive() | ||
- | if udsLen>0 then | ||
- | if udsBuffer[1]==73 then | ||
- | local pos=4 | ||
- | local res="" | ||
- | while pos <= udsLen and pos < 36 do | ||
- | if udsBuffer[pos]> | ||
- | res=res..string.char(udsBuffer[pos]) | ||
- | end | ||
- | pos= pos +1 | ||
- | end | ||
- | return res | ||
- | else | ||
- | return " | ||
- | end | ||
- | else | ||
- | return "NO DATA" | ||
- | end | ||
- | end | ||
- | </ | ||
- | |||
- | This function is called when the user selects the Menu entry "VIN Number" | ||
- | |||
- | echoWrite(" | ||
- | |||
- | sends the text " | ||
- | |||
- | The command | ||
- | |||
- | udsLen=receive() | ||
- | | ||
- | picks up the module answer from the OBD dongle. When //udsLen// is greater than 0, it means that something is received. The received bytes themself are stored in the byte array // | ||
- | |||
- | As we are expect an ASCII VIN number in the received data, we run through the // | ||
- | |||
- | return res | ||
- | |||
- | as the result of this function. | ||
- | |||
- | |||
- | This is also important to know: All Lua functions which are called from a menu entry must return a string value, as this string is than displayed in the menu as actual value, so it represents the result of the call as feedback to the user. | ||
- | |||
- | |||
- | And that's already the whole magic... Based on this scheme all wanted functionality can be realized in OOBD with Lua | ||
- | |||
- | * create a menu entry | ||
- | * link a function to it | ||
- | * do the wanted stuff inside the function | ||
- | * return the result as string | ||
- | |
doc/lua/tutorial.1309109199.txt.gz · Last modified: 2011/06/26 19:26 by admin