Topic: DMD0298

MRX - Modbus Network Read


The Modbus Network Read (MRX) instruction is used to read data from a Modbus Server. It uses Modbus/TCP protocol to read data over the onboard Ethernet port, or Modbus/RTU protocol to read data over the onboard serial port or a port on a SERIO module.

 

Note: If the same Do-more Designer project will be using Modbus Read (MRX) instructions to read data from multiple Modbus/TCP Servers, the best performance will be had by creating a separate Modbus/TCP Client device for each of the Modbus/TCP Servers. For more information on creating Modbus/TCP Client devices see the Device Configuration help topic.

 

Except for broadcast messages, when the MRX instruction sends a query to a server it expects a normal response. One of four possible events can occur from the query:
 

  1. If the server receives the query without a communication error, and can handle the query normally, it returns a normal response.
     

  2. If the server does not receive the query due to a communication error, no response is returned. The MRX instruction will eventually process a timeout condition for the query. Note: the timeout value is defined in the Modbus Client device.
     

  3. If the server receives the query, but detects a communication error (parity, LRC, or CRC), no response is returned. The MRX instruction will eventually process a timeout condition for the query.
    Note:
    the timeout value is defined in the Modbus Client device.
     

  4. If the server receives the query without a communication error, but cannot handle it (for example, if the request is to read a non-existent coil or register), the server will return an exception response informing the MRX instruction of the nature of the error. This exception response value from the server can optionally be stored in a memory location.

    List of Modbus Exception Response Codes.

 

Element References:

Note: Use the F9 key (Element Browser) or Down-Arrow key (Auto-Complete) at any time to see a complete list of the memory locations that are valid in the current field of the instruction.

 

Device - designates which of the configured Devices to read the data from. Before this instruction can select a Device, that Device must be configured to accept data from an external source. Part of the configuration for a device is assigning a name to the device. It is that name which will show up in the Device selection drop-down menu. For more information on configuring devices go to the Device Configuration Section under System Configuration.
 

  • Ethernet Devices - selects one of the Devices configured to use the onboard Ethernet port (@IntEthernet) on the Do-more controller. Selecting a Device that uses an Ethernet port also selects the use of Modbus/TCP protocol.
     

  • Serial Port Devices - selects either the onboard serial port (@IntSerial), or one of the ports on a SERIO module. Selecting a Device that uses a serial port also selects the use of Modbus/RTU protocol.
     

  • create device - indicates that there are no Devices that have been configured to perform this instruction. For example, the CPU does not have an onboard Ethernet port, or the onboard serial port is NOT configured to be a Modbus Client. Selecting this entry will open the Create New Device dialog in the System Configuration so that a Modbus Client device can be created.

 


Modbus/TCP Addressing

 

  • IP Address - the IP Address of the Modbus Server to read the data from.
    Note:
    Invoking the Element Browser (F9) for this field will bring up the IP Address Lookup utility that can find the IP Address for a given name.
     

  • TCP Port Number - the port number of the Modbus Server to read the data from. The default value of 502 is typically the correct number for Modbus protocol.

 


Unit ID - (also known as the Slave ID) designates the Unit ID of the Modbus Server to read the data from. This can be any constant value between 1 and 255.

 

If this Modbus Network Read (MRX) instruction is configured to use a serial device, the Unit ID uniquely identifies the Modbus Server on the serial network.

 

If this Modbus Network Read (MRX) instruction is configured to use an Ethernet Device, the Unit ID is unused.
Note:
The one exception is a Modbus Server that is a Modbus/TCP -to- Modbus/RTU gateway (such as our MB-Gateway). In this instance the Unit ID will uniquely identify the Modbus/RTU Server on the serial network side of the gateway.

 

Function Code - designates the Modbus function code to use. The following function codes are available in the drop-down menu:

 

  • 1 - Read Coils

  • 2 - Read Discrete Inputs

  • 3 - Read Holding Registers

  • 4 - Read input Registers

  • 7 - Read Exception Status

 

From Modbus Offset Address - designates the offset of the Modbus address where you want to read the data from. This can be any constant value in the range of 1 to 65536.

Note: this field only specifies the offset for the location to begin reading, for example to read the value in Holding Register 40029 enter only the offset value 29, to read the status of the Discrete Input 10017, enter only the offset value 17.

 

Number of Elements - designates the number of elements of the selected type to read.

 

If Function Code 1 or 2 is selected this selection defines how many coils or inputs to read. This can be any constant value in the range of 1 to 2008, or any readable numeric location.

 

If Function Code 3 or 4 is selected this selection defines how many registers (16-bit words) to read. This can be any constant value in the range of 1 to 125, or any readable location.

 

If Function Code 7 is selected this selection will be grayed out because the value is fixed at 1 (16-bit words) to read.

 

To Do-more Memory Address - designates the beginning address of the destination data in the controller. This value must match the type specified in the Function Code as follows:

 

If Function Code 1 or 2 is selected the Do-more Memory Address value can be any writable bit location except for struct fields.

 

If Function Code 3 or 4 is selected the Do-more Memory Address value can be any writable numeric location except for struct fields.

 

If Function Code 7 is selected the Do-more Memory Address value can be any writable numeric location except for struct fields.

 

Note: in most cases, the built-in Modbus memory blocks MC / MI / MIR / MHR should not be used as the location to store data received by the MRX instruction because these memory locations are reserved for use by the Do-more's Modbus Server (Slave) function. Refer to the Modbus Memory Blocks diagram in the Memory Configuration section of the System Configuration for more information.
 


Enable - designates how this instruction is enabled. Select from one of the following:
 

  • Once on Leading Edge - select this option to have this instruction run to completion exactly one time. Typically, this will take more than one controller scan. Configured this way the Modbus Network Read (MRX) instruction is edge triggered.
     

  • Continuous on Power Flow at Interval - select this option to have this instruction run as long as the instruction has power flow. After the Modbus Network Read (MRX) has initially run, if the instruction still has power flow, the instruction will remain enabled and will wait the specified amount of time before running again. The following options selects how much time (in milliseconds) to wait between successive runs.

 

    • Constant - specifies the interval time in Hours / Minutes / Seconds / Milliseconds.
       

    • Variable - This can be any readable numeric location that contains a value between 0 and 2,147,483,647 which represents the number of milliseconds  to wait before running again. A value of 0ms means this instruction will be set to run on the next scan.

 


On Success - designates one of the following two options if the Modbus Network Read operation is successful:

 

  • SET BIT - The specific BIT location will be SET OFF when the Modbus Network Read (MRX) instruction is first enabled, and then SET ON if the operation is successful. This value can be any writable bit location.
     

  • JMP to Stage - JMP to the specified Stage. The target Stage must be in the same Program as the Modbus Network Read (MRX) instruction, you cannot specify a Stage in a different Program. This selection will function like a standalone Jump to Stage instruction. Click here for more information on the Jump To Stage instruction.

 

On Error - designates one of the following two options if the Modbus Network Read operation is unsuccessful:

 

  • SET BIT - The specific BIT location will be SET OFF when the Modbus Network Read (MRX) instruction is first enabled, and then SET ON if the operation is unsuccessful. This value can be any r writable bit location.
     

  • JMP to Stage - JMP to the specified Stage. The target Stage must be in the same Program as the Modbus Network Read (MRX) instruction, you cannot specify a Stage in a different Program. This selection will function the same as a standalone Jump to Stage instruction. Click here for more information on the Jump To Stage instruction.

 


Exception Response - enable this optional selection and enter a memory location to store the Modbus Exception Response for this instruction. This can be any writable numeric location.

 


Status Display:

The red triangle in the upper left corner of the status display indicates this is a Fully Asynchronous instruction.

 

Because Modbus protocol always works with Unsigned Word (16-bit) data, this instruction will display the range specified in the To Do-more Memory Address field in an additional field - called Do-more Range - with the appropriate cast operators any time the entered element is NOT an Unsigned Word type.

For example, If the MRX is reading two Holding Registers and placing them in D0, the Do-more Range field will display D0:UW0 - D0:UW1, which means that the first Holding Register value is located in Word 0 of D0, and the second Holding Register value is located in Word 1 of D0.

 

In the example to the right the specified range is N0 - N19, the N range contains Signed Word values, so the :U cast operator is added to the display in the Do-more Range field as N0:U - N19:U.

 

For more information on casting, refer to the help topic on Casting.

 


See Also:

 


Example 1 of 2:

Description of a Typical Modbus Network Read (MRX) Stage Diagram:

To the right is a stage diagram showing a typical sequence that monitors a 3rd-party Modbus TCP slave and reads up a new recipe whenever it becomes available.

 

Initially the NewRec (New Recipe) stage monitors a bit from the slave. When this bit comes ON, RecipeRdy (Recipe Ready) causes the transition to the RDRec stage.

 

The RDRec (Read Recipe) reads the new recipe from the slave. When this completes, RecipeRD (Recipe Read) causes the transition to the RSTRec stage.

 

The RSTRec (Reset Recipe) writes a bit to the slave to inform it the recipe has been read successfully. When this write is complete, RecipeRST (Recipe Reset) causes a transition to the RSTMon stage.

 

The RSTMon (Reset Monitor) stage monitors a bit from the slave to confirm the slave heard the reset. When this bit comes ON, MonRST (Monitor Reset) causes a transition back to the NewRec stage to repeat the process and wait for the next new recipe.

 

 

Description of a Typical Modbus Network Read (MRX) Stage Ladder:

To the right is a ladder program that is equivalent to the above stage diagram. It utilizes both the Modbus Network Read (MRX) and Modbus Network Write (MWX) instructions to monitor a 3rd-party Modbus TCP slave and read up a new recipe whenever it becomes available.

 

A summary of the master/slave interaction is as follows:

  1. The master monitors Modbus Input bit 1.

  2. The slave formulates a new recipe and stores it in Modbus Input Register range 1-16.

  3. The slave sets Modbus Input bit 1 ON when the recipe is ready and begins to monitor his own Modbus Coil 1 to know when the master has read the new recipe up.

  4. The master sees Modbus Input bit 1 turn ON and reads up the new recipe from Modbus Input Register range 1-16 and stores it in his memory range RecipeVar1-RecipeVar16.

  5. The master then sets the slave's Modbus Coil 1 to inform the slave he has read up the new recipe and begins to monitor Modbus Input bit 2 to know the slave heard him.

  6. The slave sees his Modbus Coil 1 turn ON and turns ON his Modbus Input bit 2 to inform the master he is now formulating the next recipe.

  7. The master sees Modbus Input bit 2 come ON and can now repeat the cycle (return to step 1 above).

 

NewRecipe is the initial stage. The MRX instruction is set to execute "Continuous at Interval 0.100s" to read a particular Modbus Input from the slave. This is the bit the slave will set when a new recipe has been stored and is ready for the Do-more PLC to read it up. The MRX reads this bit and stores it in RecipeReady bit. Upon the successful completion of each read the ReadSuccess1 bit will come ON. When there has been a successful read and the RecipeReady bit is ON, Rung 3 resets the RecipeReady bit back OFF (in preparation for the next cycle) and transitions to the ReadRecipe stage. If any one of these reads fails, the MRX instruction will transition to the ErrorStage where the error can be assessed (e.g. evaluation of the Exception Response, Exception1 value).

 

The ReadRecipe stage executes the MRX instruction once to read up the new recipe from Modbus Input Registers 1-16 and stores them in Do-more memory range RecipeVar1-RecipeVar16. Upon a successful read, the MRX transitions to the ResetRecipe stage. If the read failed, the MRX instruction will transition to the ErrorStage where the error can be assessed (e.g. evaluation of the Exception Response, Exception2 value).

 

The ResetRecipe stage executes the MWX instruction once to write to a single Modbus Coil to inform the slave the new recipe has been read up successfully. Upon a successful read, the MWX transitions to the ResetMonitor stage. If the write failed, the MWX instruction will transition to the ErrorStage where the error can be assessed (e.g. evaluation of the Exception Response, Exception3 value).

 

The ResetMonitor stage monitors a feedback bit from the slave that the slave should use to inform the Do-more PLC that it "heard" the recipe read was successful and that the slave can now make preparations for the next recipe. The MRX reads this bit (Modbus Input 2) and stores it in MonitorReset bit. Upon the successful completion of each read the ReadSuccess2 bit will come ON. When there has been a successful read and the MonitorReset bit is ON, Rung 10 resets the MonitorReset bit back OFF (in preparation for the next cycle) and transitions back to the initial NextRecipe stage. If any one of these reads fails, the MRX instruction will transition to the ErrorStage where the error can be assessed (e.g. evaluation of the Exception Response, Exception4 value).

 

The ErrorStage should have code in it (not shown here) that evaluates the various errors that can occur in this example.


Example 2 of 2: