Skip to content

Instantly share code, notes, and snippets.

@ASolchen
Created January 9, 2019 17:45
Show Gist options
  • Save ASolchen/d05b001cd7432b9124488c6c8b47c6ef to your computer and use it in GitHub Desktop.
Save ASolchen/d05b001cd7432b9124488c6c8b47c6ef to your computer and use it in GitHub Desktop.
Basic window to talk to a Beckhoff controller using PyGObject and pyads
#local ip is 10.10.10.11
#PLC ip is 10.10.10.10
#route setup on PLC's ADS router AmsNetID 10.10.10.11.1.1 and IP 10.10.10.11
import gi, shutil, os
import pyads
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, Gdk
def setup_ads():
local_port = pyads.open_port()
pyads.set_local_address('10.10.10.11.1.1')
print(pyads.get_local_address())
remote_ams = '10.10.10.10.1.1'
plc_port = 851
plc = pyads.Connection(remote_ams, plc_port)
plc.open()
return plc
def get_val(plc, label):
label.set_text(str(plc.read_by_name('Main.value', pyads.PLCTYPE_DINT)))
return True
def close(plc):
plc.close()
Gtk.main_quit()
settings = Gtk.Settings.get_default()
settings.set_property("gtk-application-prefer-dark-theme", True)
w = Gtk.Window()
w.set_title('Test Window')
l = Gtk.Label('Test Window')
w.add(l)
w.set_opacity(0.2)
w.show_all()
w.connect("delete-event", Gtk.main_quit)
w.show_all()
plc = setup_ads()
GObject.timeout_add(100, get_val, plc, l)
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment