Created
December 2, 2015 17:35
-
-
Save sourceperl/0de98d248dd3adadd907 to your computer and use it in GitHub Desktop.
A little industrial HMI with a backgroung img and an overlay of modbus data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
from tkinter import * | |
from pyModbusTCP.client import ModbusClient | |
import time | |
# some const | |
SERVER_HOST = "192.168.0.64" | |
SERVER_PORT = 502 | |
# some global vars | |
# define modbus server host, port | |
c = ModbusClient() | |
c.host(SERVER_HOST) | |
c.port(SERVER_PORT) | |
# Tk | |
root=Tk() | |
# load backgroung image (640x480) | |
img_background=PhotoImage(file="bg_hmi.gif") | |
# build canvas and fill it | |
cnv = Canvas(root, width=640, height=480) | |
item = cnv.create_image(0, 0, anchor=NW, image=img_background) | |
cnv.pack() | |
# add overlay with dynamic data | |
p_amont_vl = cnv.create_text(256, 298, text="##", font=("Helvetica", 8), | |
fill="orange") | |
p_aval_vl = cnv.create_text(256, 312, text="##", font=("Helvetica", 8), | |
fill="orange") | |
p_aval_cp = cnv.create_text(256, 326, text="##", font=("Helvetica", 8), | |
fill="orange") | |
#Button(root, text="more pressure", command=more_p).pack() | |
#cnv.create_oval(100, 200, 120, 220, outline='black', fill='red') | |
# main modbus cycle | |
def mbus_cycle(): | |
if not c.is_open(): | |
c.open() | |
if c.is_open(): | |
reg=c.read_holding_registers(0,20) | |
if reg: | |
cnv.itemconfig(p_amont_vl, text=reg[0], fill="red") | |
cnv.itemconfig(p_aval_vl, text=reg[1], fill="red") | |
cnv.itemconfig(p_aval_cp, text=reg[2], fill="red") | |
else: | |
cnv.itemconfig(p_amont_vl, text="##", fill="orange") | |
cnv.itemconfig(p_aval_vl, text="##", fill="orange") | |
cnv.itemconfig(p_aval_cp, text="##", fill="orange") | |
# redo after 500ms | |
root.after(500, mbus_cycle) | |
# init modbus cycle | |
mbus_cycle() | |
# Tk main loop | |
root.wm_title('Test HMI modbus with Python/Tk') | |
#root.wm_attributes('-fullscreen', True) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment