PymateIO-HMI
After the Modbus basics let's learn how to use it with an HMI display like the HMI-043U2S.
For this the Pymate must act as a Modbus Slave since the HMI is working as a Modbus Master.
HMI Example Project
The HMI content is designed with the CommandHMI software. The software manages '.skm' project file that can be uploaded to the HMI.
The demonstration script is based on the "HMI_PyMateIO_Modbus_MASTER.skm" project.
It contains various controls starting with two "Numeric Display".
It also contains a "Bit Lamp" displaying the status of a bit stored on the Pymate.
Finally the "Bit Switch" have a LED controled by the HMI Display (when switch is pressed). Later, the LED is updated by the Pymate to confirm the actual on/off state on the Pymate
Pymate Script
The slave act as following:
- change the "Bit Lamp" state every 5 seconds.
- change the "Bit Switch" state (its LED) every 10 seconds.
- capture the "Bit Switch" on/off event and displays a message in REPL.
- display the number of milliseconds in the "Numeric Input" 4x1
- display the number of HMI Request in the "Numeric Input" 4x0
The modbus_slave.py script perform the following tasks:
- Declare the Modbus registers exposed by Pymate
- Implement the callback handlers attached to registers
- Pumps the Modbus messages in the MainLoop
Registers declaration
The slave will declare its registers as follow:
- The both "numeric imput" fields are stored within a single HRegs register with double Integer. The master do reads two consecutive values from the same register.
- The both "Bit Switch" and "bit Lamp" do share the same byte register (8 bits) but at different bit position.
The Modbus Slave uses a specific data structure to declare the registers.
# Registers and their default values
slave_registers = {
"COILS": {
"0x bits": {
"register": 0,
"len": 8,
# WARNING: Bit 7 on left, Bit 0 on right
# bit 1 = LED; bit 0 = Btn LED
"val": [False,False,False,False,False,False,False,False],
"on_get_cb" : get_addr0_state,
"on_set_cb" : set_addr0_state
}
},
"HREGS": {
"4x fields": {
"register": 0,
"len": 2,
# 1st-> 4x0 field = 65280 ; 2nd-> 4x1 field = 1
# Values must be uint16 0-65535
"val": [0xFF00, 1 ],
"on_get_cb" : get_counters
}
},
"ISTS": {
},
"IREGS": {
}
}Note the entries on_get_cb and on_set_cb appearing in the structure. Those entries are used to associate callback functions.
The on_get_cb callback is called when the master request register data from the slave.
The on_set_cb callback is called when the master need to update a register data on the slave.
Script Structure
The slave script is structured as follows.
from pymate.board import Board
import time, struct
SLAVE_ADDR = 0x01 # Slave Addr 1-127 of this instance
print( 'Create Modbus Slave at addr', SLAVE_ADDR )
brd = Board()
modbus_slave = brd.modbus( master=False, addr=SLAVE_ADDR, baudrate=9600 )
# Active Modbus debugging messages
modbus_slave.debug = True
# -----------------------------------------------
# Definition of the callbacks
# -----------------------------------------------
def get_addr0_state( reg_type, address, val ):
pass
def set_addr0_state( reg_type, address, val ):
pass
def get_counters( reg_type, address, val ):
pass
# -----------------------------------------------
# Definition of registers
# -----------------------------------------------
# Registers and their default values
slave_registers = {
...
}
print( 'Setup slave registers...' )
modbus_slave.setup_registers( registers=slave_registers )
# -----------------------------------------------
# Processing incoming request
# -----------------------------------------------
print( 'Start slave processing...' )
while True:
# Process incoming request
result = modbus_slave.process()
# Place your processing code here
time.sleep_ms(500)The points of interests are the following:
- The Modbus slave is created with the following instructions, a variation of a Modbus Master creation.
modbus_slave = brd.modbus( master=False, addr=SLAVE_ADDR, baudrate=9600 ) - The modbus_slave.debug = True will shows incoming Modbus Request on the REPL session. This may represent unvaluable information.
- The slave register definition must be injected within the slave with modbus_slave.setup_registers( registers=slave_registers )
- The main loop do process the request with result = modbus_slave.process() . result will be True when a request was received and processed.
Notice that Exception may be raised in case of invalid message or exception in the callback!
PymateIO all right reserved © 2026 - Written by MCHobby for PymateIO






