User Tools

Site Tools


doc:lua:tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
doc:lua:tutorial [2011/06/26 19:02] admindoc:lua:tutorial [2014/03/02 07:27] (current) – removed admin
Line 1: Line 1:
-====== The OOBD Lua Tutorial ====== 
  
-FIXME under construction 
- 
-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,id) 
- identifyOOBDInterface() 
- openPage("VIN Test") 
- addElement("VIN Number", "vin","-",0x2, "") 
- pageDone() 
- return oldvalue 
-end 
-  
-  
------------------ Do the initial settings -------------- 
-  
-Start("","") 
-return 
-</code> 
- 
-So what happens here? When the Lua interpreter runs through the script, it finally finds the  
- 
-   Start("","") 
-    
-command at the end. This is one of the naming conventions in OOBD: The function, which initializes everything, must be called "Start". 
- 
-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. 
- 
-//identifyOOBDInterface// evaluates, which OBD hardware dongle is connected to make the further dongle commands inside the library more hardware independent. 
- 
- 
-The command sequence //openPage("VIN Test")//, //addElement("VIN Number", "vin","-",0x2, "")// and //pageDone()// create and show a menu, containing the only list entry "VIN Number". The //addElement()// command assigns the lua function //vin// to this menu entry: 
- 
- 
-<code lua> 
-function vin(oldvalue,id) 
- echoWrite("0902\r") 
- 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]>31 then 
- res=res..string.char(udsBuffer[pos]) 
- end 
- pos= pos +1 
- end 
- return res 
- else 
- return "Error" 
- end 
- else 
- return "NO DATA" 
- end 
-end 
-</code> 
- 
-This function is called when the user selects the Menu entry "VIN Number". Inside this function first the command 
- 
-  echoWrite("0902\r") 
- 
-sends the text "0902" as hexadecimal data string to the OBD dongle. The dongle itself forwards this data sequence as binary telegram onto the vehicle diagnostics bus and waits for an answer from the vehicle module. 
- 
-The command 
- 
-  udsLen=receive() 
-   
-picks up the module answer from the OBD dongle. When //udsLen// is g 
-   
doc/lua/tutorial.1309107739.txt.gz · Last modified: 2011/06/26 19:02 by admin