PymateIO-HMI: Difference between revisions

From pymate.io wiki
Jump to navigation Jump to search
No edit summary
Line 33: Line 33:
[[File:PymateIO-HMI-01-05.png]]
[[File:PymateIO-HMI-01-05.png]]


== Pymate Script ==


The slave act as following:


{{PYMATE-FOOTER}}
* 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
 
[[File:PymateIO-HMI-life.jpg|640px]]
 
The {{fname|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
 
=== Register 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.
 
<syntaxhighlight lang="python" line start="1">
# 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": {
    }
}
</syntaxhighlight>
 
Note the entries '''on_get_cb''' and '''on_set_cb'''
 
{{PYMATE-TRAILER}}

Revision as of 23:51, 13 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

Register 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.
# 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


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