Mal was anderes wie halte ich das "Programm" am laufen weil wenn ich es ausführe wird es danach beendet.
Oder soll ich die Funktion in nen eigenen Thread packen??
Ich steh echt auf'm Schlauch und komm nicht weiter mit der Sache.
Also nur damit man es versteht was ich da mache.
Ich möchte werte aus ner SPS lesen wenn sie sich verändern, dafür gibt es die Funktion AdsSyncAddDeviceNotificationReq(...) (welche in der Bibliothek enthalten ist, die Callback nicht) mit der man sozusagen eine Variable der SPS überwacht und falls sie sich ändert eine Funktion aufgerufen wird. In C++ Funktioniert das ganze ja auch, nur ich soll es halt in Python umsetzen.
Mein Status ist soweit das wenn ich die AdsSyncAddDeviceNotificationReq() aufrufe die Notification regestriert wird und was auch gut ist, nur wie vielleicht schon gemerkt steh ich halt bei der Callback Funktion auf dem schlau.
Falls nützlich hier mein "Test-herumspiel-Code", bei der Callback habe ich mal nen Return mal keinen nur damit ihr nicht drucheinander kommt is halt mein Testcode
Code: Alles auswählen
class Ads:
#ADS Bibliothek Laden
ads_library = ctypes.windll.LoadLibrary(r'C:\TwinCAT\ADS Api\TcAdsDll\TcAdsDll.dll')
padress = 0
def __init__(self, ip=None):
self.connect()
def __del__(self):
self.disconnect()
def connect(self):
port = self.ads_library.AdsPortOpen()
self.padress = ads_struct.PAmsAddr()
self.ads_library.AdsGetLocalAddress(ctypes.pointer(self.padress))
self.padress.port = ads_struct.AMSPORT_R0_PLC_RTS1
return self.padress
def disconnect(self):
error = self.ads_library.AdsPortClose()
def callback(self):
print 'test callback'
def callback2(self, pAddr, pNotification, hUser):
print 'test', pAddr, pNotification, hUser
print pNotification.data
def notification(self, var_name):
var_name = ctypes.create_string_buffer(var_name)
adsNotificationAttrib = ctypes.pointer(ads_struct.AdsNotificationAttrib())
adsNotificationAttrib.cbLength = 4
adsNotificationAttrib.nTransMode = ads_struct.ADSTRANS_SERVERONCHA
adsNotificationAttrib.nMaxDelay = 0
adsNotificationAttrib.nCycleTime = 10000 #1000000
adsNotificationAttrib.dwChangeFilter = 10000 #1000000
handel = ctypes.c_ulong(0)
hNotification = ctypes.c_ulong(0)
cf=ctypes.CFUNCTYPE(None, ctypes.POINTER(ads_struct.PAmsAddr), ctypes.POINTER(ads_struct.AdsNotificationHeader), ctypes.POINTER(ctypes.c_ulong))
tempcall = cf(self.callback2)
error = self.ads_library.AdsSyncReadWriteReq(ctypes.pointer(self.padress),ads_struct.ADSIGRP_SYM_HNDBYNAME,0x0, ctypes.sizeof(handel), ctypes.pointer(handel), ctypes.sizeof(var_name),ctypes.pointer(var_name))
print error, "error 1"
error = self.ads_library.AdsSyncAddDeviceNotificationReq(ctypes.pointer(self.padress),ads_struct.ADSIGRP_SYM_VALBYHND,handel, adsNotificationAttrib, tempcall,handel, ctypes.pointer(hNotification))
print error, "error 2"
print hNotification , 'hNotification'
error = self.ads_library.AdsSyncDelDeviceNotificationReq(ctypes.pointer(self.padress), hNotification)
print error, "error 3"
test = Ads()
test.notification('MAIN.PLCVar0')