Skip to main content
Skip table of contents

25.7 Passing data into/out of Lua with User RAM

Passing data into/out of Lua

User RAM consists of a list of volatile Modbus addresses where data can be sent to, and read from, a Lua script. Lua writes to the Modbus registers, and then a host device can read that information. 

There are a total of 200 registers of pre-allocated RAM, which is split into several groups so that users may access it conveniently with different data types.

Use the following USER_RAM registers to store information:

USER_RAM example script - lua

LUA
LJ.IntervalConfig(0, 1000)
previous = MB.readName("AIN0")
MB.writeName("USER_RAM0_U16", 1)

while true do
  if LJ.CheckInterval(0) then
    enable = MB.readName("USER_RAM0_U16") --Host may disable portion of the script
    if enable >= 1 then
      current = MB.readName("AIN0")
      twoValueAvg = (previous + current) / 2
      print("Rolling average of the last two measurements: ", twoValueAvg)
      MB.writeName("USER_RAM0_F32", twoValueAvg) --Provide a new value to host
      previous = current
    end
  end
end

For MB.writeName and MB.readName, the T7 requires firmware 1.0287 or later and the T4 requires firmware 1.0027 or later.

There is also a more advanced system for passing data to/from a Lua script referred to as FIFO buffers. These buffers are useful if you want to send an array of information in sequence to/from a Lua script. Usually 2 buffers are used for each endpoint, one buffer dedicated for each communication direction (read and write). For example, a host may write new data for the Lua script into FIFO0, then once the script reads the data out of that buffer, it responds by writing data into FIFO1, and then the host may read the data out of FIFO1.

Note that the following _DATA registers are buffered registers.

USER_RAM_FIFO example script - lua

LUA
aF32_Out= {}  --array of 5 values(floats)
aF32_Out[1] = 10.0
aF32_Out[2] = 20.1
aF32_Out[3] = 30.2
aF32_Out[4] = 40.3
aF32_Out[5] = 50.4

aF32_In = {}
numValuesFIO0 = 5
ValueSizeInBytes = 4
numBytesAllocFIFO0 = numValuesFIO0*ValueSizeInBytes
MB.W(47900, 1, numBytesAllocFIFO0) --allocate USER_RAM_FIFO0_NUM_BYTES_IN_FIFO to 20 bytes

LJ.IntervalConfig(0, 2000)
while true do
  if LJ.CheckInterval(0) then
    --write out to the host with FIFO0
    for i=1, numValuesFIO0 do
      ValOutOfLua = aF32_Out[i]
      numBytesFIFO0 = MB.R(47910, 1)
      if (numBytesFIFO0 < numBytesAllocFIFO0) then
        MB.W(47030, 3, ValOutOfLua)  --provide a new array to host
        print ("Next Value FIFO0: ", ValOutOfLua)
      else
        print ("FIFO0 buffer is full.")
      end
    end
    --read in new data from the host with FIFO1
    --Note that an external computer must have previously written to FIFO1
    numBytesFIFO1 = MB.R(47912, 1) --USER_RAM_FIFO1_NUM_BYTES_IN_FIFO
    if (numBytesFIFO1 == 0) then
      print ("FIFO1 buffer is empty.")
    end
    for i=1, ((numBytesFIFO1+1)/ValueSizeInBytes) do
      ValIntoLua = MB.R(47032, 3)
      aF32_In[i] = ValIntoLua
      print ("Next Value FIFO1: ", ValIntoLua)
    end
  end
end

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.