Frequently Asked Question
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.
Hello everyone. In this lesson we will look at how Modbus Client can be used in HIL SCADA.
As we saw in previous lessons, both Modbus TCP client and server are implemented at the
model level, with corresponding components in Schematic Editor. In order to simulate a
Modbus client during simulation runtime, you can use the dedicated Modbus API. This API
makes it possible to implement a Modbus Client instance in your PC in order to communicate with
Servers implemented on other devices. This is a Python-based API, which can run on your PC
even without a HIL device. It can be used inside of HIL SCADA, Standalone HIL SCADA,
or even in standalone scripts.Now let's see how we can perform
full Modbus communication using only a HIL device and a Computer.
Let's start by searching for and opening the "Modbus TCP client implementation in HIL SCADA"
example in Example Explorer, shown here.This model consists of two main parts:
an electrical part and a communication part. The electrical part models a standard grid-connected
Battery inverter from the Microgrid Toolbox, with the ability to toggle between grid forming
and grid following mode. In grid forming mode, the battery inverter sources power
a Constant impedance load, which is modeled here as resistors in series with inductances.
The communication part is located inside the Modbus subsystem,
and is directly connected to the Battery inverter, as shown here. By entering the Modbus subsystem,
you can see that communication is handled by a Modbus Device component. It is possible to
toggle between SCADA and Modbus control mode here. By changing the value of the Modbus SCADA Input,
you can control which control inputs will pass through the Signal switch to the Inverter.
The Modbus Device is configured using a Python dictionary named config_Batt.
The dictionary is defined inside the Model Initialization script shown here.
The first keys of the dictionary define the network-oriented parameters:
port, IP address, netmask, and slave ID. Before compiling the model make sure that the Modbus
device belongs to the same subnetwork as your PC. We should first check the
current IP address of the PC and change the value of the 'ip_addr' field accordingly.
In order to check the current IP address of your PC, let's open the console by typing
CMD in the Windows search tab. Open the application and type the command ipconfig.
In the IPv4 address field you should get the current IP of your PC,
highlighted here. Your Modbus Server IP should have the same first three numbers as here,
but the fourth number should be different from any of the others on the network.
Let's pick a number that's different from the current
IP address and check if it is free in our network.
Once we have our number, type the command ping followed by the IP you
have chosen in the Command Prompt. If we see the message unreachable,
that means that this is free and we can use it for our Modbus Server.
Let's put this new IP address in the 'ip_addr' field in the Model Initialization script.
The remaining keys of the configuration dictionary are used to define the Modbus registers. Since
there aren't any coils or discrete inputs in this particular model, those fields are left empty. For
holding registers, the dictionary defines 6 output registers: 2 integers and 4 floats. The next key
allows for defining initial values for the holding registers, which will be overridden as soon as a
Modbus client sends a write command to the server. The last key provides definitions for 7 input
registers: 1 integer and 6 floats. Those registers are interfaces for sending measurements from the
simulation to the Modbus Client, and for receiving commands from the Modbus Client to the simulation.
Now let's compile and open the model in HIL SCADA.
As you can see, this model comes with a pre-built SCADA panel.
The Panel file consists of 3 widget groups: SCADA Command Panel,
SCADA Modbus Client, and Switch Control Mode.In the Panel initialization dialog,
we can see Python code defining how the Modbus Client initialization is done.
After importing the basic Python API packages, the basic network Server parameters are defined.
Then, the Modbus Client object (battery_client) is initialized using these network parameters.
The remaining code defines the input and output registers.
The input registers are specified via the battery_input_register_address String,
and the output registers are defined by the battery_output_register_address dictionary.
Widgets in the SCADA Command Panel group access the HIL simulation directly using
HIL API through USB or Ethernet connection. From this group, you can control the Battery
inverter the same way as you would any other microgrid example available in the Typhoon
HIL Control Center Example Library Explorer.The widgets inside the SCADA Modbus Client group
are used for Modbus communication. By default, the "Control with Modbus" checkbox is unchecked,
which means that the Battery inverter is controlled directly by the SCADA Command
Panel. By checking the box, we can switch the Inverter control mode to Modbus.
In the SCADA Modbus Client group, there are 6 widgets for sending commands to the Modbus server.
Those commands use the write_registers_adv function from the Modbus API.
The remaining 7 widgets display readings from the Modbus server.
Those widgets utilize another function, the read_input_registers_adv.
For demonstration purposes let's open the checkbox widget 'On_Off' that allows us to
turn the converter ON and OFF. Here we use the write_single_register command
to set the coil corresponding to change the Inverter status in the Modbus Server.
Similarly, we can open the LED widget which shows the Inverter status, called 'Inverter On'. Here
we use the read_input_registers_adv function and point to the first address that corresponds to
reading the first coil address from Modbus Server.As we covered in the HIL Fundamentals Course,
there are two ways to connect to a HIL device - via USB connection and Ethernet
connection. As you can see here, an Ethernet connection is used for both communications.
For more information about setting up a direct Ethernet communication with a HIL device,
feel free to revisit the course or check the corresponding link in the Materials Tab.
Now let's hit the Play button in SCADA to start the simulation.
By default, control is done via SCADA inputs, so it is possible to control the model with
the left panel: the SCADA command Panel.Now let's check the Control with Modbus
checkbox to enable control via Modbus requests. The right panel, the SCADA Modbus client,
is where all Modbus Client requests are performed.As we can see, when we try to turn the converter
ON, we receive a message in the Message log saying "Modbus Server not connected!".
This is a common error that can occur when trying to configure a Modbus Client and Server
connection. It happens because the Modbus Client tried to reach the Server and write to the address
scripted in the HIL SCADA panel initialization file under the BATTERY_SERVER_ADDRESS field
shown here. When a connection couldn't be made, an exception was raised,
which was then treated by a log message shown here in the Macro Code section of the ON_OFF widget.
In this case, we caused this issue when updated our IP address in the Schematic
Editor Model Initialization, but left the HIL SCADA Panel Configuration at
its default value, causing a mismatch.Now let's return to the Panel Initialization,
code the right Modbus Server IP address that we setup previously in Schematic Editor,
and perform the same action. Then, we need to stop the model so the changes can be applied.
Now let's start the model again and re-activate Modbus control.
You can see now that by turning the converter ON and OFF, we affect the model,
as seen in the gauges on the left panel. Moreover,
you can change the operation mode by selecting V or I mode here, and change the Voltage, frequency,
and Power references by changing the values inside their respective text box, as shown here.
With this, we've now simulated a remote device controlling and monitoring a battery storage
system over a Modbus network using a Typhoon HIL real-time simulator.