Frequently Asked Question

Video: 7.2.3.1. Modbus Client in HIL SCADA
Last Updated 2 years ago


Press "Ctrl + F" to find the keyword of your interest.

If you wish to have a direct link access to the video timestamps, please follow these instructions.

Found this video helpful? Why not take the whole HIL Specialist course? A Certificate is waiting for you for free at HIL Academy.

Would you or your organization benefit from having these videos narrated in your native language? Contact us and let us know if you wish to contribute.

TRANSCRIPT

00:00:03

Hello everyone. In this lesson we will look at how Modbus Client can be used in HIL SCADA. 

00:00:08

As we saw in previous lessons, both Modbus TCP client and server are implemented at the  

00:00:14

model level, with corresponding components in Schematic Editor. In order to simulate a  

00:00:18

Modbus client during simulation runtime, you can use the dedicated Modbus API. This API  

00:00:25

makes it possible to implement a Modbus Client instance in your PC in order to communicate with  

00:00:29

Servers implemented on other devices. This is a Python-based API, which can run on your PC  

00:00:36

even without a HIL device. It can be used inside of HIL SCADA, Standalone HIL SCADA,  

00:00:42

or even in standalone scripts.Now let's see how we can perform  

00:00:46

full Modbus communication using only a HIL device and a Computer. 

00:00:52

Let's start by searching for and opening the "Modbus TCP client implementation in HIL SCADA"  

00:00:57

example in Example Explorer, shown here.This model consists of two main parts:  

00:01:04

an electrical part and a communication part. The electrical part models a standard grid-connected  

00:01:09

Battery inverter from the Microgrid Toolbox, with the ability to toggle between grid forming  

00:01:14

and grid following mode. In grid forming mode, the battery inverter sources power  

00:01:19

a Constant impedance load, which is modeled here as resistors in series with inductances. 

00:01:24

The communication part is located inside the Modbus subsystem,  

00:01:29

and is directly connected to the Battery inverter, as shown here. By entering the Modbus subsystem,  

00:01:34

you can see that communication is handled by a Modbus Device component. It is possible to  

00:01:40

toggle between SCADA and Modbus control mode here. By changing the value of the Modbus SCADA Input,  

00:01:45

you can control which control inputs will pass through the Signal switch to the Inverter. 

00:01:53

The Modbus Device is configured using a Python dictionary named config_Batt.  

00:01:58

The dictionary is defined inside the Model Initialization script shown here. 

00:02:04

The first keys of the dictionary define the network-oriented parameters:  

00:02:08

port, IP address, netmask, and slave ID. Before compiling the model make sure that the Modbus  

00:02:14

device belongs to the same subnetwork as your PC. We should first check the  

00:02:18

current IP address of the PC and change the value of the 'ip_addr' field accordingly. 

00:02:25

In order to check the current IP address of your PC, let's open the console by typing  

00:02:30

CMD in the Windows search tab. Open the application and type the command ipconfig. 

00:02:38

In the IPv4 address field you should get the current IP of your PC,  

00:02:42

highlighted here. Your Modbus Server IP should have the same first three numbers as here,  

00:02:47

but the fourth number should be different from any of the others on the network. 

00:02:57

Let's pick a number that's different from the current  

00:02:59

IP address and check if it is free in our network.  

00:03:12

Once we have our number, type the command ping followed by the IP you  

00:03:16

have chosen in the Command Prompt. If we see the message unreachable,  

00:03:20

that means that this is free and we can use it for our Modbus Server.  

00:03:23

Let's put this new IP address in the 'ip_addr' field in the Model Initialization script. 

00:03:45

The remaining keys of the configuration dictionary are used to define the Modbus registers. Since  

00:03:50

there aren't any coils or discrete inputs in this particular model, those fields are left empty. For  

00:03:56

holding registers, the dictionary defines 6 output registers: 2 integers and 4 floats. The next key  

00:04:03

allows for defining initial values for the holding registers, which will be overridden as soon as a  

00:04:08

Modbus client sends a write command to the server. The last key provides definitions for 7 input  

00:04:13

registers: 1 integer and 6 floats. Those registers are interfaces for sending measurements from the  

00:04:19

simulation to the Modbus Client, and for receiving commands from the Modbus Client to the simulation. 

00:04:26

Now let's compile and open the model in HIL SCADA. 

00:04:33

As you can see, this model comes with a pre-built SCADA panel. 

00:04:37

The Panel file consists of 3 widget groups: SCADA Command Panel,  

00:04:41

SCADA Modbus Client, and Switch Control Mode.In the Panel initialization dialog,  

00:04:47

we can see Python code defining how the Modbus Client initialization is done.  

00:04:52

After importing the basic Python API packages, the basic network Server parameters are defined.  

00:04:58

Then, the Modbus Client object (battery_client) is initialized using these network parameters. 

00:05:05

The remaining code defines the input and output registers.  

00:05:08

The input registers are specified via the battery_input_register_address String,  

00:05:13

and the output registers are defined by the battery_output_register_address dictionary. 

00:05:22

Widgets in the SCADA Command Panel group access the HIL simulation directly using  

00:05:27

HIL API through USB or Ethernet connection. From this group, you can control the Battery  

00:05:32

inverter the same way as you would any other microgrid example available in the Typhoon  

00:05:36

HIL Control Center Example Library Explorer.The widgets inside the SCADA Modbus Client group  

00:05:42

are used for Modbus communication. By default, the "Control with Modbus" checkbox is unchecked,  

00:05:48

which means that the Battery inverter is controlled directly by the SCADA Command  

00:05:52

Panel. By checking the box, we can switch the Inverter control mode to Modbus. 

00:05:57

In the SCADA Modbus Client group, there are 6 widgets for sending commands to the Modbus server.  

00:06:02

Those commands use the write_registers_adv function from the Modbus API.  

00:06:07

The remaining 7 widgets display readings from the Modbus server.  

00:06:11

Those widgets utilize another function, the read_input_registers_adv. 

00:06:18

For demonstration purposes let's open the checkbox widget 'On_Off' that allows us to  

00:06:23

turn the converter ON and OFF. Here we use the write_single_register command  

00:06:27

to set the coil corresponding to change the Inverter status in the Modbus Server.  

00:06:31

Similarly, we can open the LED widget which shows the Inverter status, called 'Inverter On'. Here  

00:06:37

we use the read_input_registers_adv function and point to the first address that corresponds to  

00:06:42

reading the first coil address from Modbus Server.As we covered in the HIL Fundamentals Course,  

00:06:49

there are two ways to connect to a HIL device - via USB connection and Ethernet  

00:06:54

connection. As you can see here, an Ethernet connection is used for both communications. 

00:06:59

For more information about setting up a direct Ethernet communication with a HIL device,  

00:07:04

feel free to revisit the course or check the corresponding link in the Materials Tab. 

00:07:10

Now let's hit the Play button in SCADA to start the simulation. 

00:07:14

By default, control is done via SCADA inputs, so it is possible to control the model with  

00:07:20

the left panel: the SCADA command Panel.Now let's check the Control with Modbus  

00:07:25

checkbox to enable control via Modbus requests. The right panel, the SCADA Modbus client,  

00:07:31

is where all Modbus Client requests are performed.As we can see, when we try to turn the converter  

00:07:37

ON, we receive a message in the Message log saying "Modbus Server not connected!". 

00:07:42

This is a common error that can occur when trying to configure a Modbus Client and Server  

00:07:46

connection. It happens because the Modbus Client tried to reach the Server and write to the address  

00:07:51

scripted in the HIL SCADA panel initialization file under the BATTERY_SERVER_ADDRESS field  

00:07:56

shown here. When a connection couldn't be made, an exception was raised,  

00:08:00

which was then treated by a log message shown here in the Macro Code section of the ON_OFF widget. 

00:08:05

In this case, we caused this issue when updated our IP address in the Schematic  

00:08:09

Editor Model Initialization, but left the HIL SCADA Panel Configuration at  

00:08:13

its default value, causing a mismatch.Now let's return to the Panel Initialization,  

00:08:21

code the right Modbus Server IP address that we setup previously in Schematic Editor,  

00:08:26

and perform the same action. Then, we need to stop the model so the changes can be applied. 

00:08:33

Now let's start the model again and re-activate Modbus control. 

00:08:39

You can see now that by turning the converter ON and OFF, we affect the model,  

00:08:43

as seen in the gauges on the left panel. Moreover,  

00:08:46

you can change the operation mode by selecting V or I mode here, and change the Voltage, frequency,  

00:08:53

and Power references by changing the values inside their respective text box, as shown here. 

00:09:08

With this, we've now simulated a remote device controlling and monitoring a battery storage  

00:09:13

system over a Modbus network using a Typhoon HIL real-time simulator.

Please Wait!

Please wait... it will take a second!