Lua scripting

Hello world! in Lua


--atxmain function is the start of the script

--argc indicates the number of available arguments

--args is array with argc arguments. (see command SCRIPT_ARG)

function atxmain(argc, args)

  ATX_Command("ATX7006_DISPLAYMSG Hello world!")

  return 1 --return status code (see command SCRIPT_RETURN?)

end--end of function

How to copy my Lua file to the ATX7006?


Please read this faq

How to debug a Lua script file?


A Lua scripting file is started with the command EXECUTE_SCRIPT. With the 3rd parameter it is possible to enable debugging. Please read this faq for more information about the commands.

Where can I find the Lua reference manual?


The Lua reference manual can be found here

Is there a Lua editor available with syntax highlighting?


A free editor supporting the Lua syntax is Notepad++

How to get input parameters?


function atxmain(argc, args)

  --argc is number of arguments

  --args is the argument (string) array set with the command SCRIPT_ARG

  for i =1, argc do

    --display argument value on atx7006 display

    ATX_Command("ATX7006_DISPLAYMSG arg: "..args[i])

  end

  return 1 --return status code

end

How to read the signal from a module?


function atxmain(argc, args)

  local samples = 8192 --no. of samples to read

  local startaddr = 0 --start address of module memory

  local shift = 0 --shift data (only of DIO data)


  --select the card, e.g. read voltage from AWG

  ATX_Command("CSELECT2")


  --read signal

  signal, statusmsg = ATX_GetCardSignal(startaddr,samples,shift)

  --check for errors

  --message available with command SCRIPT_STATUSMSG?

  assert( #statusmsg == 0,"Read error occured!")

  --store signal (voltages) in first Lua result array

  --result array can be read with the command SCRIPT_RESULT

  ATX_StoreResults(1,ATX_RESULTTYPE_DOUBLE,signal)

  return 1 --return status code

end

How to read or write module instrument memory?


function atxmain(argc, args)

  local length = 128 --size of array

  local location = 0 --module location

  local startaddr = 0 --start address of module memory


  --write memory

  local warray = {} --declare (empty) array

  for i =1, length do --fill array

    warray[i] = i

  end

  statusmsg = ATX_WriteModuleMemory(location,startaddr,warray) --write module memory

  --check for errors

  --message available with command SCRIPT_STATUSMSG?

  assert( #statusmsg == 0,"Write error occured!")


  --read memory

  rarray, statusmsg = ATX_ReadModuleMemory(location,startaddr,length) --read module memory

  --check for errors

  --message available with command SCRIPT_STATUSMSG?

  assert( #statusmsg == 0,"Read error occured!")

  return 1 --return status code

end

How to run a command file from Lua?


function atxmain(argc, args)

  --executed command file in the subfolder cmdfiles of the userdata path

  result = ATX_Command("EXECUTE_CMDFILE cmdfiles/myfile.cmd")

  --show results on atx7006 display if there are any

  ATX_Command("ATX7006_DISPLAYMSG result: "..result)

  return 1 --return status code

end

How to write data to a file?


function atxmain(argc, args)

--request last dynamic test results parameters

  local result = ATX_Command("MR_DYN?")

  --replace CRs by hypen

  result = string.gsub(result,"\r","-")

  --write results to file in the the userdata path (ATX_DATAPATH)

  local f = io.open(ATX_DATAPATH.."luaresults.txt","w")

  f:write(result)

  f:close()

  return 1 --return status code

end

How to communicate with an other ATX7006?

function atxmain(argc, args)

  local host = "atx7006-2" --netbios or ip address

  local port = 30111 --default port of ATX7006

  local atx = socket.tcp() --create socket

  atxcon, err = atx:connect(host, port) --connect

  if err then --check for error

    ATX_Command("ATX7006_DISPLAYMSG error")

  else

    ATX_Command("ATX7006_DISPLAYMSG connected!")

    --Set message on display of remote ATX7006

    --Command should include a termination character (CR, LF or semicolon)

    atx:send("ATX7006_DISPLAYMSG hello atx7006;")

    atx:close()--close socket when finished

  end

  return 1 --return status code

end