Lua Variable functions

dw.variable.read

Parameters
status, value = dw.variable.read(dev, var, count, type)

Description

Reads a variable from a device, optionally the count and type can be specified to read arrays and use the "read as" functionality within the device manager. The function returns the status, and the value. If the user reads an array, then the returned value is an array with the values in it.

Inputs

  • dev: a STRING name of the device
  • var: a STRING name of the variable in the device
  • count: enables reading an array (optional, if type is also not specified)
  • type: a STRING value of the type to Read As (optional)

Outputs

  • status: a status code. Limited to codes returned through variable read.
  • value: the value returned from the read. When count is greater than 1, a Lua table (array) is returned with the values.
Usage
local s, v = dw.variable.read("SampleDevice","var1");
local s, v = dw.variable.read("SampleDevice","var1","10");
local s, v = dw.variable.read("SampleDevice","var1","10","INT2");
DWOutput["status"], DWOutput["value"] = dw.variable.read("Local CPU 1","D[0]","1","INT2");


Example

This example computes the sum and average of values from D[1] through D[10] of the Local CPU 1 device. The average is returned as an output variable.

local sum = 0
local status
local value
for index=1,10,1 do
    -- index is incremented by 1 each time
    status,value = dw.variable.read("Local CPU 1","D[" .. index .. "]","1","INT2")
    sum = sum + value
end
DWOutput["average"] = sum/10

dw.variable.write

Parameters
status = dw.variable.write(dev, var, value, count, type)

Description

Writes a variable to a device, optionally the count and type can be specified to write arrays and use the “write as” functionality within the device manager. The function returns the status of the write operation. If the user writes an array then the value should be a Lua array.

Inputs

  • dev: a STRING name of the device
  • var: a STRING name of the variable on the device
  • value: value of the variable. Can be a Lua table if writing to an array
  • count: enables reading an array (optional)
  • type: a STRING value of the type to Read As (optional)

Outputs

  • status: a status code. Limited to codes returned through variable write.
Usage
local s = dw.variable.write("SampleDevice","var1","abc")
local s = dw.variable.write("SampleDevice","var1","abc","10")
local s = dw.variable.write("SampleDevice","var1","abc","10","INT4")
DWOutput["status"] = dw.variable.write("Local CPU 1","D[0]",DWInput["input"],"1","FLOAT4");
local s,v = dw.variable.read("Local CPU 1","D[0]","1","FLOAT4");
DWOutput["output"] = v;


Example

This example writes the increasing values 0 to 10 to the device variables D[0] to D[10] in the Local CPU 1 device.

local value = 0
for index=0,10,1 do
    -- index is incremented by 1 each time
    dw.variable.write("Local CPU 1","D[" .. index .. "]",value,"1","INT2")
    value = value + 1
end

Limitations

  • The Read As STRING functionality is not supported for variables of types other than STRING.

Unsupported Types

The following types are unsupported in dw.variable.read() and dw.variable.write() functions:

  • TIMESTAMP
  • BINARY
  • STRUCT
Related topics

See the specific Device types section for specific device read and write error codes.