Oct-05-2016, 02:28 PM
Hi,
I'm trying to use code that I have found online at https://sourceforge.net/projects/pynetvars/
In short: it allows to use CoDeSys PLC network variables in Python.
I'm using their example code to capture network variables sent out by my PLC and to act upon a change of the content.
So far I can capture the data and parse it without any problem.
Now I'm trying to see whether there is a change in the received data compared to the previous received data.
For this I use a global variable netvars_old which will be used to compare against in the callback function.
Now the strange part is that with any run of the callback function, my global variable netvars_old is always the same as the variable netvars which is provided by the callback function as argument.
This doesn't make sense to me as I only assign the current data netvars to netvars_old on the end of the function.
Anybody an idea how this can happen?
(leaving out some predefined values, it is about netvar_old and netvar anyway)
(in this code I only print the value of net_alarm to ease debugging)
I'm trying to use code that I have found online at https://sourceforge.net/projects/pynetvars/
In short: it allows to use CoDeSys PLC network variables in Python.
I'm using their example code to capture network variables sent out by my PLC and to act upon a change of the content.
So far I can capture the data and parse it without any problem.
Now I'm trying to see whether there is a change in the received data compared to the previous received data.
For this I use a global variable netvars_old which will be used to compare against in the callback function.
Now the strange part is that with any run of the callback function, my global variable netvars_old is always the same as the variable netvars which is provided by the callback function as argument.
This doesn't make sense to me as I only assign the current data netvars to netvars_old on the end of the function.
Anybody an idea how this can happen?
(leaving out some predefined values, it is about netvar_old and netvar anyway)
(in this code I only print the value of net_alarm to ease debugging)
def parseReceivedValues1(netvars): global netvars_old print "OLD: net_alarm: ", netvars_old['net_alarm'] # Compare previous with current for netvar_element, netvar_value in netvars.items(): netvar_value_old = netvars_old[netvar_element] if netvar_value <> netvar_value_old: print "Changed: ", netvar_element, " - ", netvar_value print "NEW: net_alarm: ", netvars['net_alarm'] print "OLD: net_alarm: ", netvars_old['net_alarm'] netvars_old = netvars netVars1 = NetVars( 1, 'UDP', mcastGrp, mcastPort, localIf ) netVars1.varsFromExpFile( netvarfile ) # Initialize netvars_old. Will be empty. netvars_old = netVars1.latestValues netVars1.subscribeNewValues(parseReceivedValues1) netVars1.startListenerLoop()Output example:
Output:Initial netvars_old: {}
establish mcast-listening on 225.10.10.10 via
receive loop...
OLD: net_alarm: 0
NEW: net_alarm: 0
OLD: net_alarm: 0
OLD: net_alarm: 1
NEW: net_alarm: 1
OLD: net_alarm: 1
OLD: net_alarm: 1
NEW: net_alarm: 1
OLD: net_alarm: 1
OLD: net_alarm: 0
NEW: net_alarm: 0
OLD: net_alarm: 0
While I expect:Output:Initial netvars_old: {}
establish mcast-listening on 225.10.10.10 via
receive loop...
OLD: net_alarm: 0
NEW: net_alarm: 0
OLD: net_alarm: 0
OLD: net_alarm: 0
NEW: net_alarm: 1
OLD: net_alarm: 0
OLD: net_alarm: 1
NEW: net_alarm: 1
OLD: net_alarm: 1
OLD: net_alarm: 1
NEW: net_alarm: 0
OLD: net_alarm: 1
OLD: net_alarm: 0
NEW: net_alarm: 0
OLD: net_alarm: 0