PymateIO-HMI: Difference between revisions

From pymate.io wiki
Jump to navigation Jump to search
Line 94: Line 94:


The '''on_set_cb''' callback is called when the master need to update a register data on 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.
<syntaxhighlight lang="python">
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)
</syntaxhighlight>
The points of interests are the following:
* The '''Modbus slave''' is created with the following instructions, a variation of a Modbus Master creation.<br />{{fname|1=modbus_slave = brd.modbus( master=False, addr=SLAVE_ADDR, baudrate=9600 )}}


{{PYMATE-TRAILER}}
{{PYMATE-TRAILER}}

Revision as of 00:26, 14 June 2026


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.

PymateIO-HMI-00.jpg

PymateIO-HMI-DB9-Connector.jpg

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.

PymateIO-HMI-01-01.png

It contains various controls starting with two "Numeric Display".

Note: the second field can be updated on the HMI with an integrated editor. However, this is not the purpose of this tutorial that will ignore the new value.

PymateIO-HMI-01-02.png

PymateIO-HMI-01-03.png

It also contains a "Bit Lamp" displaying the status of a bit stored on the Pymate.

PymateIO-HMI-01-04.png

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

PymateIO-HMI-01-05.png

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

PymateIO-HMI-life.jpg

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 )

PymateIO all right reserved © 2026 - Written by MCHobby for PymateIO