MODBUS

UniLogic enables you to easily implement MODBUS communications by configuration rather than programming.

Most application requirements are met by Periodic operations, which run according to the time intervals you set.

You can also run Aperiodic operations. You can trigger a single operation, or a group of operations, via your application.

This means that an application might:

 

A single UniStream can be defined and function simultaneously as a master or slave, and may also contain multiple slave definitions.

Modbus Configuration Diagram

Configuring a Port: Serial/Ethernet

MODBUS may run via ports on the UniStream CPU, HMI panel, or COM modules.

SerialSerial

CPU RS485 Port

On the Solution Explorer, select PLC Communications>Physical> RS485 Serial (COM1) and edit the parameters in the Properties Window as required.
Under Work Mode, select MODBUS (Panel).

Panel USB port

Select Enable USB for Serial Device, click USB Port Settings, then click Initialize Port for: and select the desired communication type.

Note that enabling the port automatically adds the Panel USB Port struct to use in your program.

 

 

EthernetEthernet

On the Solution Explorer, select PLC Communications>physical> Panel Ethernet and edit the parameters in the Properties Window as required.

COM ModulesCOM Modules

To run MODBUS serial communications through a COM module port, select MODBUS Panel in the port settings.

Configuring MODBUS

Master

  1. On the Solution Explorer tree, click PLC Communications to expand it, then Protocols > MODBUS > Master; this opens the MODBUS ports.

  2. Select the desired port, and click Add new Remote Slave.
    You can also add Remote Slaves by right-clicking a MODBUS port in the Explorer tree

    Modbus Main

  3. The remote slave's Properties Window opens on the right of the screen, as does Add New Operation in the center.

    Modbus Master slaves1
     

  4. Set the remote slave definitions in the Properties Windowremote slave definitions in the Properties Window.

    MODBUS Slave Properties Window Parameters

    Active periodic

    This bit controls, for this particular slave, whether Periodic Operations run or not.

    Turn it ON to run the Coil and Register Periodic operations according to the time interval you have set in the individual operation.

    Turn it OFF to suspend all Periodic operations for this slave.
    Note that:
    - You must link a bit to this parameter; failing to do so will result in a compilation error.
    - You can suspend a specific operation from running via the operations' Active parameter.

    Modbus Communications

    Use this to select the port for the MODBUS operations by clicking the drop-down arrow.

    Select COM Port

    Slave ID

    Enter the ID number of the slave to be accessed. The range is 1-255.

    Slave Name

    Assign a name to help you identify the slave

    Response Timeout (ms)

    The default is 500ms, click the field to edit the value.
    Determines how long the controller will wait for a response from the remote slave.

    Advanced

    The Queue options may be used in certain programs that rely on large quantities of data. This is described in the section below, Dropped Operations.

     

  5. For each slave, add new operations as described below.

Setting the Slave ID via Ladder

Note that you can change the Slave ID at runtime via the MODBUS Set Slave ID ladder function. 

 

Parameter Name

Purpose

A

Slave ID

Select the Slave whose ID to want to change

B

ID to set

Link a tag or a constant to set the Slave ID number

C

Status

•    0 = Success
•    1 = RC InProgress
•    2 = AC InProgress
•    -1 = Error

 

Adding Operations

The MODBUS operations you create are arranged in a table. You can create both Period and Aperiodic operations.

To add a single operation:

  1. Select the tab with the desired operation type, and click Add New Operation. UniLogic adds a row to the table.

  2. Click in fields to edit and make selections.

 

The next image shows Periodic operations.


 

Note that while the Active Periodic setting in Properties determines whether the master will run operations periodically, you can prevent an individual operation from running. To do this, assign a bit to the operation's Active parameter. To prevent the operation from running, turn this bit OFF.

 

The next image shows Aperiodic operations.


 

You can convert Periodic operations to Aperiodic, and vice versa. Note that you can also select several operands by holding down CTRL while clicking them and the right-clicking them.

 

 

 

Note that the ID parameters allow you to use Aperiodic Ladder elements to run the operations via your program.
ID allows you to run a single operation. Group ID allows you to run an entire group of operations.

Status

Link a tag to the Status field to contain the MODBUS Transaction Status.

The status codes are in the following table.

Error code

Translation

-11

Timeout

-1

Unknown error

-2

3-second temporary Slave Messaging Suspension, 
after slave communication failure.

Modbus General Errors

1

illegal function

2

illegal data address

3

illegal data value

4

slave or server failure

5

acknowledge

6

slave or server busy

7

negative acknowledge

8

memory parity error

10

gateway path unavailable

11

target device failed to respond

12

invalid crc

13

invalid data

16

too many data

 

As you add/delete each Modbus operation, UniLogic increments/decrements their number.


 

Write/Read Multiple Registers - Periodic and Aperiodic

These operations perform a combination of one Read operation and one Write operation in a single MODBUS transaction.

The Write operation is performed before the Read. Holding registers are addressed starting at zero. (Holding Registers 1-16 are addressed in the PDU as 0-15.)

The request specifies:

 

 

Batch Operations

To add a batch of operations:

  1. Select the tab with the desired operation type, and click the drop-down arrow under Add New Operation.

  2. Select Add New Operation Batch; UniLogic opens the Add window, displaying the appropriate tag type.

  3. View the tags within structs either via the Expand/Collapse icons next to the Search field, or by clicking arrows to expand them.
    In the next image, Coils are selected, therefore only coils are displayed, including coils from within structs.

  4. Check the tags you want, and click Add. In the struct shown below, a bit array is selected, along with two individual bits.

 

 

In the next image, note that the operation Coil1 address starts at zero, and Coil2 at 12. This allots sufficient space for for the bit array, 0-11.

 

In the next image, the Aperiodic tabs were selected when the struct was added.

The bit tags were automatically categorized into Aperiodic  Coils, and the registers to Aperiodic registers.


Operation Parameters

You can select Periodic or Aperiodic operations. The parameters differ as listed in the next table.

Name

Definition

Name

Enter an identifying name for your convenience.

Tag

Link to the tag containing the data for the operation

  • In order to assign a vector, use an Array data tag.
    Note that if the array tag is displayed with a bracket, you need to press backspace to erase it and assign the array.

  • Note that you can also link to the members of a declared struct.

Address

This is the address in the slave device, and maps between it and the Tag.

Action

Click to select the desired action (Read/Write)

Operation

Click to select the MODBUS operation MODBUS operation.
Note that the list of commands filters according to the tag type.

Read Operations

UniStream acts as a master, accessing requested data in a remote device, and writing the data into a tag or buffer within UniStream.

  • Read Coils (1)

  • Read Inputs (2)

  • Read Holding Registers (3)

  • Read Input Registers (4)

Write Operations

UniStream acts as a slave; a MODBUS master can access and write to specified tags within UniStream.

  • Force Single Coil (5)

  • Preset Single Register (6)

  • Force Coils (15)

  • Preset Multiple Registers (16)

Periodic Parameters: relevant for periodic operations

Every Period

Every Time Unit

Determine the frequency of the periodic operation

Active

Link a bit to control whether or not the individual  operation will run.

APeriodic Parameters: relevant for aperiodic operations. Trigger via MODBUS Aperiodic Functions

ID

Use the ID number in your ladder application

Group ID

Use the Group ID number in your ladder application.

Status

Link a register to hold the status codestatus code.

Modbus transaction status errors:

Error code

Translation

-11

Timeout

-1

Unknown error

1

Illegal function

2

Illegal data address

3

Illegal data value

4

Slave or server failure

5

Acknowledge

6

Slave or server busy

7

Negative acknowledge

8

Memory parity error

10

Gateway path unavailable

11

Target device failed to respond

12

Invalid CRC

13

Invalid data

16

Too many data

The actual message that the code indicates, is from the MODBUS protocol itself.

Note that this must be reset by your application.

 

Remote Slave Struct

Defining a slave automatically creates a Remote Slave struct.

Name

Data Type

Definition

Sessions

UINT32

Total number of attempted sessions

These are incremental counters that tracks the total number of events. They are reset at powerup.

Success

UINT32

Total number of successful sessions

Fail

UINT32

Total number of failed sessions

MODBUS Slave Status

UINT8

Status

 

1    -  No errors: the number of attempted sessions = the number of successful sessions

2    -  No connections made (no sessions established)

3    - Connection exists, but there is an error in the MODBUS commands

Remote Slave ID

UINT8

Read-only

Port

UINT16

Read-only

IP

UINT32

Read-only

Dropped

UINT32

Read-only. This is an incremental counter that tracks the total number of dropped sessions. It is reset at powerup.

 

Slaves

As mentioned previously, a single UniStream can function as both Master and as Slave.

  1. On the Solution Explorer tree, click PLC Communications to expand it, then Protocols > MODBUS > Slaves; this opens the MODBUS ports.

  2. Select the desired port, and click Add new Slave.
    You can also add slaves by right-clicking a MODBUS port in the Explorer tree

    Modbus Slave

  3. The slave's Properties Window opens on the right of the screen, as does Add New Operation in the center.

    Set the slave definitions in the Properties Windowslave definitions in the Properties Window.

    MODBUS Slave Properties Window Parameters

    Modbus Communications

    Use this to select the port for the MODBUS operations by clicking the drop-down arrow.

    Select COM Port

    Slave ID

    Enter the ID number of the slave to be accessed. The range is 1-255.

    Slave Name

    Assign a name to help you identify the slave

     

  4. For each slave, add new operations as described below.

Adding Operations

The MODBUS operations you create are arranged in a table.

To add a single operation:

    1. Select the tab with the desired operation type, and click Add New Operation. UniLogic adds a row to the table.

    2. Click in fields to edit and make selections. Note that Permissions determines master access to the slave for the operation.

      MODBus Slave def

 

To add a batch of operations:

  1. Select the tab with the desired operation type, and click the drop-down arrow under Add New Operation.

  2. Select Add New Operation Batch; UniLogic opens the Add window, displaying the appropriate tag type.

  3. View the tags within structs either via the Expand/Collapse icons next to the Search field, or by clicking arrows to expand them.
    In the next image, Coils are selected, therefore only coils are displayed, including coils from within structs.

  4. Check the tags you want, and click Add. In the struct shown below, a bit array is selected, along with two individual bits.

 

To add a struct of operations:

 

  1. Select the tab with the desired operation type, and click the drop-down arrow under Add New Operation.

  2. Select Add New structs; UniLogic opens the Add window, displaying the structs.

 

Operation Parameters

Name

Definition

Name

Enter an identifying name for your convenience.

Tag

Link to the tag containing the data for the operation

  • In order to assign a vector, use an Array data tag.
    Note that if the array tag is displayed with a bracket, you need to press backspace to erase it and assign the array.

  • Note that you can also link to the members of a declared struct.

Address

This is the address in the slave device, and maps between it and the Tag.

Permissions

Click to enable the desired access to the slave (Read/Write)

 

Slave Struct

Defining a slave automatically creates a Slave struct.

Name

Data Type

Definition

Sessions

UINT32

Total number of attempted sessions

These are incremental counters that track the total number of events. They are reset at powerup.

Success

UINT32

Total number of successful sessions

Fail

UINT32

Total number of failed sessions

Num of Clients

UINT8

Read-only. This is the number of clients currently connected to the slave.

Port

UINT16

Read-only

Slave ID

UINT32

Read-only

 

Export/Import Slaves

You can export and import a UniStream MODBUS Master's slave definitions, via a proprietary Unitronics file. To export the definitions of:

Local Slaves

You can export local slave definitions to Excel. To export the definitions of:

 

MODBUS Master Debugging: Dropped Operations

In certain cases, for example in a large project with a large number of sensors, the entire queue of operations may not be able to run during the scan. To see if this is the case, monitor the Drops parameter in the Remote Slave's struct, and see if there are dropped operations during the scan.

The Drops parameter is a continuously incrementing counter that cannot be reset by the user. To track the number of drops, store the value.

If so, first examine your application. Operations that only need to run occasionally during a scan, such as powering on a machine, can be run as Aperiodic operations via ladder.

If this does not remedy the problem, in the Properties window of the Remote Slave, check Add to queue Size, and enter a value under Extended queue.

Note that since this will increase the scan time, it is recommended to add in small increments of 2.

 

 

Remote Slave Address OverLap

You can select the ribbon tab Remote Slave while you are editing, and click it to check for address overlap. This can help you avoid unintentionally overwriting data.

 

If you are connected to the Internet, you can access this UniLogic tutorial:

Using MODBUS with UniStream

 

 

Related Topics

MODBUS: Aperiodic (Ladder Triggered)

Communications: COM Ports and DataCom

PC-PLC COM

Protocols: Third-party

TCP Server