CuteHMI - Basic Modbus Example (CuteHMI.Examples.Modbus.Basic.3)
|
Baisc Modbus example. Example demonstrates basic usage of QML components provided by CuteHMI.Modbus.4 extension.
To run the example use cutehmi.view.4 tool.
Let's make a quick tour over View.qml
file.
First thing to notice are CuteHMI imports.
Example uses CuteHMI.Modbus.4 and CuteHMI.Services.3 extensions.
Next thing to notice is a configuration of a Modbus server. The server is given an IP address, a port and a slave address.
Similarly we configure Modbus client.
We want our server to be turned into a service. This is simply done by embedding it inside CuteHMI.Services.Service component.
Similarly we want the client to be turned into a service. Outside a service client would not perform polling, handle connection errors and so on.
Both services are wrapped by the service group. Service group helps manage services. It comes with two default controllers CuteHMI.Services.ServiceAutoStart and CuteHMI.Services.ServiceAutoActivate. They will start the group and activate it and the group will do the same with its services.
The group gives also the ability to configure service dependencies. It can be done with ServiceDependency rule. We let the group know that clientService
requires serverService
. This way the server will be started before the client and stopped after it. This helps keep logs clean as the client won't moan about broken connection.
As the client and the server are configured we can move on to establishing some interaction between them. Let's create a button that will tell the client to send a request to turn on a coil.
Once the button is clicked it will send a request to write coil value at address 10. This may look suspicious on a localhost, but the whole communication is done over TCP/IP protocol. Now let's reset coil value to off on the server side.
Now let's introduce CuteHMI.Modbus.CoilController. Controllers typically are a better mean of control as they allow for property binding.
We assign a device using id of of the client. We also set up the same address as used by buttons. We use onValueChanged
signal handler to update value of a corresponding switch once the value in the controller changes. The switch itself updates the value of a controller once the user changes its value.
Value of the coil can be binded to other properties as it is done with active
property of status indicator.
The same procedure is repeated for a holding register, of course using different set of controls - adequate to holding register 16 bit integer capacity.