Lua Device functions

dw.device.start

Parameters
status = dw.device.start(name)

Description

The user specifies the name of the device to start. A numerical error indicating any problems will be returned, or 0 on success.

Inputs

  • name: a STRING name of the device

Outputs

  • status: a numerical error indicating any problems will be returned, or 0 on success.
Usage
local ret = dw.device.start("Local CPU 1")
DWOutput["status"] = dw.device.start(DWInput["name"])
function test_device_start()
  local ret = dw.device.start(DWInput["name"])
  return ret
end
Information

After the start command is sent, the function finishes and does not wait for the actual device to start. The device might take a long time to start or be disabled, but the Lua function returns immediately.

Common Error Codes
  • -6202: Device does not exist.
  • -6208: Device is not stopped.

dw.device.stop

Parameters
status = dw.device.stop(name)

Description

The user specifies the name of the device to stop. A numerical error indicating any problems will be returned, or 0 on success.

Inputs

  • name: a STRING[64] that is a valid device name

Outputs

  • status: a numerical error indicating any problems will be returned, or 0 on success.
Usage
local ret = dw.device.start("ControlLogix")
DWOutput["status"] = dw.device.stop(DWInput["name"])
function test_device_stop()
  local ret = dw.device.stop(DWInput["name"])
  return ret
end
Commond Error Codes
  • -6202: Device does not exist.
  • -6207: Device is not started.
  • -6206: Device is starting.

dw.device.add

Parameters
status = dw.device.add(name, type, properties)

Description

The user can supply a name and type as string, as well as a Lua object of properties (1 key, and 1 string or number value pairs) with the properties needed to define the device (such as ip_address). A numerical error indicating any problems will be returned, or 0 on success.

Inputs

  • name: a STRING name of the device
  • type: the type of the device
  • properties: a Lua object of properties (1 key, and 1 string or number value pairs)

Outputs

  • status: a numerical error indicating any problems will be returned, or 0 on success.
Usage
function test_device_add()
  local props = {}
  local name = DWInput["name"]
  props.peer_address="192.168.1.133"
  props.peer_user="admin"
  props.peer_password_encoded="iCkBo1r/a4+d+63+C6xLgw=="
  props.per_var_security="false"
  local ret = dw.device.add(name,"Node Connection",props)
  return ret
end


Adding devices is advanced functionality. Having incorrect device properties may cause crashes and loss of data. Ensure you have fully tested any device separately with the device properties before using this function.

Device Type

The device type is the string type visible to the user when viewing the list of available device types, such as "Device Connection" or "Global Variables". See Device types.

Device Properties

The best way to get the device properties of a device type is to first define the device using the Workbench. Export the device to a *.dwx file. Next, edit the export file using a text editor to see all the properties of a device. See Defining, viewing, and controlling devices and using a similar process to Exporting a project or trigger for devices.

Examples

Shown is an example of defining a Global Variables device using Lua. Because certain characters (such as ",<,>) have to be escaped in a *.dwx export file, if you use an export file of a device, you will need to replaced the escaped characters. In this example, you can use the entire XML definition of a Global Variables device using bracket characters to input characters that otherwise have to be escaped in Lua.

local props = {}
local name = "Global_Lua"
props.Variables=[[<Variables><VariableInfo length="32" name="name" options="7" type="STRING"/><VariableInfo name="value" options="7" type="INT1"/><VariableInfo name="array" options="7" type="INT1" xdim="10"/><VariableInfo name="structure" options="7" struct_id="1" type="STRUCT"/></Variables>]]
props.Structures=[[<Structures><StructureInfo name="Structure1" struct_id="1"><VariableInfo name="value2" options="7" type="INT2"/></StructureInfo></Structures>]]
props.persist_values="false"
props.sort_vars="false"
props.default_value="0x00"
DWOutput["status"] = dw.device.add(name,"Global Variables",props)
Guidelines
  • After you define a device in Lua, always use the Workbench to edit the device. Ensure that all your properties appear correctly. Saving the device again using the Workbench will ensure the device definition doesn't have errors.
Common Error Codes
  • -6201: Device already exists.

dw.device.delete

Parameters
status = dw.device.delete(name)

Description

The user specifies the name of the device to delete. A numerical error indicating any problems will be returned, or 0 on success.

Inputs

  • name: a STRING name of the device

Outputs

  • status: a numerical error indicating any problems will be returned, or 0 on success.

Deleting devices is permanent. Ensure you are familiar with Backing up and Restoring a node's configuration before starting to delete devices.

Usage
local ret = dw.device.delete("ControlLogix2")
DWOutput["status"] = dw.device.delete(DWInput["name"])
function test_device_delete()
  local ret = dw.device.delete(DWInput["name"])
  return ret
end
Common Error Codes
  • -6202: Device does not exist.
  • -6208: Device is not stopped.

Additional Information

To look up a description for an error code returned from these functions, see the Error Lookup section in Touring the Workbench.