Comments (5)
Hi @horus61 I've formatted your code properly. A few comments, don't use list
in your code as list is a python keyword, use something like my_list
or better yet name it accordingly like sensors
or temp_sensors
note the plural. Also don't forget to add a sleep in the while loop or your cpu will go 100% on usage. Also prob stay away from naming variables with one letter.
Here's some history on a similar topic #230
How I would solve it:
prev_ints = run_script()
while True:
y = run_script()
print(y)
# compare lists here
# if different, update prev_ints
# do something
# else if equal, don't do anything
sleep(3)
Luckily for us we have the Zen of Python https://peps.python.org/pep-0020/
There should be one-- and preferably only one --obvious way to do it.
And we have 30,000 ways to do a list comparison lol https://www.digitalocean.com/community/tutorials/how-to-compare-two-lists-in-python
I would probably use list comprehension for your use case since we do care about the order of the items in the list.
l1 = [10, 20, 30, 40, 50]
l2 = [50, 75, 30, 20, 40]
res = [x for x in l1 + l2 if x not in l1 or x not in l2]
print(res)
if not res:
print("Lists l1 and l2 are equal")
else:
print("Lists l1 and l2 are not equal")
If you need to know when an individual index change, then you could use zip:
https://docs.python.org/3/library/functions.html#zip
prev_ints = run_script()
did_change = False
while True:
y = run_script()
print(y)
for val_y, val_p in zip(y, prev_ints):
if val_y != val_p:
print(f"The values {val_y} {val_p} aren't equal")
did_change = True
if did_change:
prev_ints = y
did_change = False
sleep(3)
Here's a few videos I would watch on these topics:
from pylogix.
Thank you Fernando for your help and I bother you again for further advice. I would like to identify the elements of the list that have changed since the previous reading according to their index because I would like to create an alarm report.
Thanks for your cooperation, greetings from Italy. Nino
Sorry for my bad English.
from pylogix.
You're welcome, your English is great! If you take the previous example that compares each value, you just have to create a new list, no need to get the index. Take a look below.
# Initialization vars before loop is started
prev_ints = run_script()
did_change = False
changed_items = [] # <- New list
while True:
y = run_script()
print(y)
# Loop through all values and compare them
for val_y, val_p in zip(y, prev_ints):
# Compare new value with prev value
if val_y != val_p:
changed_items.append(val_y) # <- Add item to list
print(f"The values {val_y} {val_p} aren't equal")
did_change = True
if did_change:
prev_ints = y
did_change = False
# send report logic here # <- New line
sleep(3)
from pylogix.
HI. Thanks for the advice you give me. I wanted to show you the raspberry pi application that I would like to create to monitor the variables on the plc to integrate an alarm message with the sending of sms. This is an example with a variable simulator. I'm a newbie and it certainly doesn't look nice and you would do it more professionally.
But it represents what I would like to do. I thank you and greet you.
from pylogix.
@horus61 I recommend you check out our discord, someone there might be able to further guide you. The best advice I can give you is to start small. Build each step of what you are trying to accomplish one by one on their own and then mash them together in the end. Also if you seek help in the discord your might want to translate function names and variables to English, otherwise is hard to follow the code.
For example:
- Read plc tags print them to the console
- Compare tags in a while loop
- Create fake alarm message to sms
- put all 3 programs together for end result
from pylogix.
Related Issues (20)
- "Forward open failed" when trying to read tags from Berghof PLC HOT 8
- .GetDeviceProperties always returns DeviceType of "None" HOT 5
- Issues connecting to emulate HOT 7
- micro850 connection HOT 5
- Return data gets jumbled up on a multi write when the tags don't exist. HOT 13
- The return value is different when using a multi write compared to a single write for a string. HOT 4
- cannot get a tag list HOT 3
- Is it possible to inhibit a periodic task? HOT 2
- pylogix - returning a tag list as a row of values HOT 7
- PLC REAL values HOT 5
- Discover does not work while running pylogix in docker HOT 10
- Invalid Continuation Byte HOT 5
- Abnormal code termination. HOT 8
- Do I need to use routing? HOT 20
- PLC connection best practice HOT 8
- Forward Open Failed HOT 22
- Connection Issues HOT 9
- PLC read does not run when called from a function HOT 5
- documentation on the CIP protocol HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pylogix.