Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global variable funkiness
#1
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)
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
Reply
#2
>>> True != False
True
>>> True <> False
  File "<stdin>", line 1
    True <> False
          ^
SyntaxError: invalid syntax
How does it run at all?
Reply
#3
(Oct-05-2016, 05:14 PM)nilamo Wrote: How does it run at all?
It's an old version of != that doesn't work in more modern Python versions.
Reply
#4
still works on the latest 2.x
Output:
metulburr@ubuntu:~$ python Python 2.7.12 (default, Jul  1 2016, 15:12:24)  [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1 <> 1 False
Recommended Tutorials:
Reply
#5
Hi tonivr,
your problem is simply a misunderstanding of dictionary references vs. dictionary copies.
If you assign a dictionary to another one it only duplicates the reference, not the whole dictionary. So modifying one will also modify the other one because it's the same memory. You only created a new name for the same storage.
You have to do a "netvars_old = netvars.copy()". That's all.

cu
Frank
Reply
#6
(Oct-05-2016, 06:24 PM)metulburr Wrote: still works on the latest 2.x
Output:
metulburr@ubuntu:~$ python Python 2.7.12 (default, Jul  1 2016, 15:12:24)  [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1 <> 1 False

Gross.  It's a good thing 3.x got rid of it, similarities to VisualBasic send chills down my spine.
Reply
#7
Hi Frank,

This is my first code in Python, so I'm afraid I will have more misunderstandings of Python as my main scripting language is Perl.

Anyway, thanks for your clear and fast explanation.
It works with the .copy()!

And I also replaced <> with !=.

Thanks all for the help!

Regards,
Toni
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,172 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Variable scope - "global x" didn't work... ptrivino 5 3,044 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,226 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  NameError for global variable leviporton 3 2,550 Jul-10-2020, 06:51 PM
Last Post: bowlofred
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,249 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Global Variable Not Updating joew 2 7,836 Jan-26-2020, 04:15 PM
Last Post: joew
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,512 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Declaring a Global Variable in a Function Bob1948 4 3,041 Sep-14-2019, 11:16 PM
Last Post: ichabod801
  Global variable does not seem to be global. Columbo 6 3,700 Jul-15-2019, 11:00 PM
Last Post: Columbo
  change value of a global variable across all modules aster 5 5,021 Jan-01-2019, 06:42 PM
Last Post: aster

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020