Run python script after PLC performed an action - python

I try to do an automation using a python script and Snap7 to connect windows pc to PLC Siemens7-1200.
They script itself does a procession of an image and saving that image afterwards and it is working as it should. My next step is to include an automation. I want this script to always run, when PLC performed a specific action. After doing that action the PLC gives out a specific signal '1' for 3 seconds.
My first idea was to connect to PLC in my python script via Snap7 (this already works) and having a While loop that always checks this signal and an IF statement. And IF the signal is '1', the rest of the python script should run, take image, process image etc.
I wondered whether this idea is useful or whether there may be an easier solution? If i have the While loop the script will be active all the time. I wondered whether there is an option to run the script only after this action was performed by PLC to not have the script active all the time. All the time is actually 24/7
In other words: Whats the easiest way to run a python script from a windows PC when a connected PLC performed a specific action?

Related

External input to Python program during runtime

I am creating a test automation which uses an application without any interfaces. However, The application calls a batch script when it changes modes, and I am therefore am able to catch the mode transitions.
What I want to do is to get the batch script to give an input to my python script (I have a state machine running in python) during runtime. Such that I can monitor the state of the application with python instead of the batch file.
I am using a similar state machine to the one of Karn Saheb:
https://dev.to/karn/building-a-simple-state-machine-in-python
However, instead of changing states statically like:
device.on_event('event')
I want the python script to do something similar to:
while(True):
device.on_event(input()) # where the input is passed from the batch script:
REM state.bat
set CurrentState=%1
"magic code to pass CurrentState to python input()" %CurrentState%
I see that a solution would be to start the python script from the batch file every time it is called with the "event" and then save the current event in another file upon termination of the python script... But I want to avoid such handling and rather evaluate this during runtime.
Thank you in advance!
A reasonably portable way of doing this without ugly polling on temporary files is to use a socket: have the main process listen and have the batch file(s) start a small program that connects to the server and writes a message.
There are security considerations here: you can start by listening only to the loopback interface, with further authentication if the local machine should not be trusted.
If you have more than one of these processes, or if you need to handle the child dying before it issues its next report, you’ll have to use threads or something like select to unify the news from different input channels (e.g., waiting on the child to exit vs. waiting on news from the next batch file).

Run a script repeatedly and externally from Flask

Imagine you are measuring the distance of something with a Raspberry Pi. You have a nice python script which measures the distance via a sensor and returns a variable.
Imagine you would like to automate the process and view the results on a web page created with Flask. Basically, be able to turn it on and have it run once a minute regardless of whatever else you do in that web page or anywhere else on the "website".
Graphically:
click a button on a web page -> Script starts -> Script runs once a minute, regardless of whether you close the page, navigate to another page, etc.
How would you go about it?
Consider that the script can run forever if necessary, kind of a fire and forget thing
you could create a second e.g. python script that would wait for a signal from the web server. With your server, after a user clicked a button, you could write to a file, that the python script periodically checks. Then, after writing "1" into the file, the script would do what you want, e.g. read sensor data every minute.
So you would run your web server and the python script alongside it. The script would be waiting for changes in the dedicated "signal" file. Then after the signal the script would do whatever you wanted.

Python: Ways to monitor program is running

Background info:
I have built a program main.py, that is built to perform work at a stateless level (meaning if something fails, it will get requeued/database etc separately)...
Problem:
I am trying to figure out how to monitor this main.py program from my linux instance.
Main.py runs and threads out multiple workers, however I want to monitor and ensure the system is still healthy to 'perform work'...
How can you do this from a single script level and determine that it is no longer working?
My Ideas:
Create a status notification to syslog stating it is healthy... and trigger on no longer receives? (if that is possible)
Linux maybe provides a way to monitor that system is healthy?
last one is creating a TCP/Socket level query mechanism to check for status...
I hope i'm describing the problem right... I'm trying to ensure program is not only running ,but processing and can process[?]

Run python script everyday to open and execute a program

Is it possible for a python script to open and execute a program each day at a specific time? My attention is...poor, but I installed a productivity program (FocalFilter) which blocks unproductive websites. Could a python script be written where it opens this program and runs it so the sites are blocked as soon as I get to my computer in the morning?
Does it matter if the computer is in locked mode (Windows 10)? I don't need a script or anything, it can be a fun little project, but I just don't know if it is even possible.
Yes, indeed you can use python. But the operating system(s) do it for you now.
Scheduled tasks run even if the computer is locked.
Take look at this:
https://ss64.com/nt/schtasks.html
or look att the Scheduler in Administration tools in Control panel. There you have many cool options.
Also, if you permanently want to block certain sites, like facebook.com and stackoverflow.com you can edit your hosts file so that facebook.com goes to 127.0.0.1, i.e. your own computer. Antiviruses do this too. Read here:
https://support.rackspace.com/how-to/modify-your-hosts-file/

How to put a PC into sleep mode using python?

While the copy is in progress, can we put a PC into sleep mode for a specific period of time, then wake up and continue copy using python script? Can you please share the code?
Actually this is possible using shell script.
Most machines manufactured after 2000 support real-time clock wakeup. There are many reasons to do so, one of which would be to record a TV program at a certain time. See ACPI Wakeup.
You'll have to explain what you mean by "While the copy is in progress" - there's not much to go on in the question. While OS drivers have suspend/resume functions, I don't know how to tell the python interpreter to save its state in the middle of running a script and then resume after wakeup. It's possible that the OS suspend/hibernate would fully capture the state of a copy operation and resume without a hiccup, but I wouldn't trust it to do so without substantial testing.
If you have "Wake On Lan" enabled you could potentially run a python script on a different PC and trigger the wake up after your specific period of time.
The scripts would probably need to talk to each other, unless you just do it all at times set in advance.

Categories

Resources