DeaD_EyE

Beispiel um Daten mittels Put/Get von der S7-1500 abzuholen

May 19th, 2026
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. from tkinter import Tk, Variable, Label, Button
  2.  
  3.  
  4. from snap7 import Client, Area
  5. from snap7.util import get_real, get_int
  6.  
  7.  
  8. class PLC:
  9.     def __init__(self):
  10.         self.plc = Client()
  11.         self.plc.connect("192.168.0.10", 0, 1)
  12.    
  13.     def __enter__(self):
  14.         return self
  15.    
  16.     def __exit__(self, exc_type, exc, tb):
  17.         self.plc.disconnect()
  18.         self.plc.destroy()
  19.    
  20.     def update_variables(self, root: Tk, mm: Variable, raw: Variable):
  21.         val_raw = get_int(self.plc.read_area(Area.PE, 0, 4, 2), 0)
  22.         val_mm = get_real(self.plc.read_area(Area.MK, 0, 50, 4), 0)
  23.         raw.set(f"Rohwert: {val_raw:>5d}")
  24.         mm.set(f"Höhe: {val_mm:>4.1f} mm")
  25.         root.after(10, self.update_variables, root, mm, raw)
  26.  
  27.  
  28. def main(plc: PLC):
  29.     root = Tk()
  30.     root.title("Höhenmesser")
  31.     mm = Variable(root, 0.0)
  32.     raw = Variable(root, 0)
  33.     Label(root, textvariable=mm, font=("JetBrains Mono", 36)).pack()
  34.     Label(root, textvariable=raw, font=("JetBrains Mono", 36)).pack()
  35.     Button(root, text="Verlassen", command=root.destroy, font=("Arial", 24)).pack()
  36.     root.after(500, plc.update_variables, root, mm, raw)
  37.     root.mainloop()
  38.  
  39.  
  40. if __name__ == "__main__":
  41.     with PLC() as plc:
  42.         main(plc)
  43.  
Add Comment
Please, Sign In to add comment