Class: ModbusDevice

ModbusDevice

Provides methods and properties for communicating with a single Modbus server


A Modbus device requires a memory map in order to start. A memory map is XML that describes how the memory is organized. When applied, the attributes described in the map become accessible to JavaScript. JavaScript can then write to the outputs and connect to changed events on the inputs. Memory maps can be created and edited using the memory map editor tool. See Memory Map for more detail. Setting up a Modbus device memory map is very similar to FLNet. See FLNet Memory Map for an example of how to set up an FLNet memory map.

As seen in the example below, the elements within the 'map' element must use the tag 'coils', 'discreteinputs', 'inputregisters' or 'holdingregisters', to specify the type of Modbus register. The addr attribute specifies the starting address (within the registers of that type), while the count attribute specifies the number of registers (for coils and discrete inputs only).

Fires:
Example
<types>
   <udt name="Coils" size="2">
      <u8 index="0">
         <bool name="Coil0" index="0" />
         <bool name="Coil1" index="1" />
         <bool name="Coil2" index="2" />
         <bool name="Coil3" index="3" />
         <bool name="Coil4" index="4" />
         <bool name="Coil5" index="5" />
         <bool name="Coil6" index="6" />
         <bool name="Coil7" index="7" />
      </u8>
      <u8 index="1">
         <bool name="Coil8" index="0" />
         <bool name="Coil9" index="1" />
      </u8>
   </udt>
   <udt name="Inputs" size="1">
      <u8 index="0">
         <bool name="Input0" index="0" />
         <bool name="Input1" index="1" />
         <bool name="Input2" index="2" />
         <bool name="Input3" index="3" />
      </u8>
   </udt>
   <udt name="Status" size="4">
      <u16 name="InputRegister1" index="0" />
      <u16 name="InputRegister2" index="2" />
   </udt>
   <udt name="Settings" size="6">
      <u16 name="HoldingRegister1" index="0" />
      <u16 name="HoldingRegister2" index="2" />
      <u8 name="HoldingRegister3a" index="4" />
      <u8 name="HoldingRegister3b" index="5" />
   </udt>
   <udt name="Registers" size="4">
      <u16 name="Register1" index="0" />
      <u16 name="Register2" index="2" />
   </udt>
</types>
<map>
   <coils name="Coils" type="Coils" addr="100" count="10" />
   <discreteinputs name="Inputs" type="Inputs" addr="100" count="4" />
   <inputregisters name="Status" type="Status" addr="100" />
   <holdingregisters name="Settings" type="Settings" addr="100" />
   <holdingregisters name="Registers" type="Registers" addr="200" />
</map>

Members


<readonly> coils :modbus.UDT

Gets an object that can be used to access the coils area of the IO memory map.

Type:
  • modbus.UDT
Since:
  • 0.1 (WAT 2.4)

<readonly> discreteInputs :modbus.UDT

Gets an object that can be used to access the discrete inputs area of the IO memory map.

Type:
  • modbus.UDT
Since:
  • 0.1 (WAT 2.4)

<readonly> holdingRegisters :modbus.UDT

Gets an object that can be used to access the holdingRegisters area of the IO memory map.

Type:
  • modbus.UDT
Since:
  • 0.1 (WAT 2.4)

<readonly> inputRegisters :modbus.UDT

Gets an object that can be used to access the input registers area of the IO memory map.

Type:
  • modbus.UDT
Since:
  • 0.1 (WAT 2.4)

<readonly> map :object

Gets an object representation of the memory map. This must be used in WSAPI applications, in order to access the coils, discreteInputs, inputRegisters, and holdingRegisters sections of the defined memory map.

Type:
  • object
Since:
  • 0.1 (WAT 2.4)
See:
Example
// standard JavaScript application
device.holdingRegisters.Settings.readSuccess.connect(onSettingsReadSuccess);
device.holdingRegisters.Settings.read()
device.holdingRegisters.Settings.autoWrite = true;
device.holdingRegisters.Settings.data.HoldingRegister1.v = 1;

// equivalent WSAPI application
device.map.holdingRegisters.Settings.obj.readSuccess.connect(onSettingsReadSuccess);
device.map.holdingRegisters.Settings.obj.read();
device.map.holdingRegisters.Settings.obj.autoWrite = true;
device.map.holdingRegisters.Settings.data.HoldingRegister1.v = 1;

mmap :string

Gets/sets the XML description of the memory map. When load is called, this description is used to create an object tree that can be used to directly access the attributes from JavaScript.

This is typically set using AJAX to retrieve an XML memory map file.

Type:
  • string
Since:
  • 0.1 (WAT 2.4)
Example
var modbus = $wat.load('modbus')[0];
var client = modbus.createTcpClient(host, port);
var device = client.createDevice(address);

function initModbus(mmap)
{
   device.mmap = mmap;
   // set up the other modbus device settings here

   if (device.load() == false)
   {
      alert("Failed to load Modbus memory map");
      return;
   }
}

// called after the page loads, for example in the body onload
function getXML()
{
   xmlhttp = new XMLHttpRequest();
   xmlhttp.open("GET", "modbusDevice.xml", true);
   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState == 4) {
         initModbus(xmlhttp.responseText);
      }
   }
   xmlhttp.send("");
}

mmapFile :string

The name of a file in the file system of the uP device (e.g. /wat/mmap.xml) to read the memory map from if mmap is empty when load is called.

It is often more convenient to use AJAX and mmap to load a memory map.

Type:
  • string
Since:
  • 0.1 (WAT 2.4)
See:

serverAddress :int

The Modbus server address (1 to 247)

Type:
  • int
Since:
  • 0.1 (WAT 2.4)

Methods


load()

Loads the memory map that has been set using mmap or mmapFile and creates an object tree to access the defined instances and attributes.

Since:
  • 0.1 (WAT 2.4)

Events


mapChanged

Indicates that the map property has changed.

Since:
  • 0.1 (WAT 2.4)

mmapChanged

Emitted when the XML description of the memory map changes. See mmap for more details.

Parameters:
Name Type Description
mmapFile string

XML description of the memory map

Since:
  • 0.1 (WAT 2.4)

mmapFileChanged

Emitted when the memory map filename changes. See mmapFile for more details.

Parameters:
Name Type Description
mmapFile string

Name of the memory map file

Since:
  • 0.1 (WAT 2.4)