Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
We'll start reading in a loop, in my case, I'm
reading a tag called PE040, which is a BOOL.
Once we see that the value is True, we'll call
FaultHappened(), then we enter another loop that
will keep reading until the value goes False.
This will make sure that we only call the function
once per True result.
'''
from pylogix import PLC
import time
def FaultHappend():
# this should get called once.
print('we had a fault')
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
read = True
while read:
try:
ret = comm.Read('PE040')
time.sleep(1)
if ret.Value:
FaultHappend()
while ret.Value:
ret = comm.Read('PE040')
time.sleep(1)
except KeyboardInterrupt:
print('exiting')
read = False
'''
the following import is only necessary because eip.py is not in this directory
'''
import sys
sys.path.append('..')
'''
We're going to log a tag value 10
times to a text file
'''
import csv
from pylogix import PLC
import time
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
with open('31_log.csv', 'w') as csv_file:
csv_file = csv.writer(csv_file, delimiter=',', quotechar='/', quoting=csv.QUOTE_MINIMAL)
for i in range(10):
ret = comm.Read('LargeArray[5]')
csv_file.writerow([ret.Value])
time.sleep(1)
'''
Write to a custom size string
WHen you create a custom size string, it is essentially
a UDT. We cannot write to them in the same way that we
can write to a standard size string.
In this case, we're going to write some text to the tag
String20, which is a custom string STRING20. We not only
have to write the data, we have to also write the length.
'''
from pylogix import PLC
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
string_size = 20
text = 'This is some text'
values = [ord(c) for c in text] + [0] * (string_size - len(text))
comm.Write('String20.LEN', len(text))
comm.Write('String20.DATA[0]', values)
'''
the following import is only necessary because eip.py is not in this directory
'''
import sys
sys.path.append('..')
'''
Get the PLC time
returns datetime.datetime type
'''
from pylogix import PLC
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
ret = comm.GetPLCTime()
# print the whole value
print(ret.Value)
# print each pice of time
print(ret.year, ret.month, ret.day, ret.hour, ret.minute, ret.second, ret.microsecond)
sys.path.append('..')
'''
Read a tag in a loop
We'll use read loop as long as it's True. When
the user presses CTRL+C on the keyboard, we'll
catch the KeyboardInterrupt, which will stop the
loop. The time sleep interval is 1 second,
so we'll be reading every 1 second.
'''
from pylogix import PLC
import time
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
read = True
while read:
try:
ret = comm.Read('LargeArray[0]')
print(ret.Value)
time.sleep(1)
except KeyboardInterrupt:
print('exiting')
read = False
'''
the following import is only necessary because eip.py is not in this directory
'''
import sys
sys.path.append('..')
'''
We're going to log a tag value 10
times to a text file
'''
from pylogix import PLC
import time
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
with open('30_log.txt', 'w') as txt_file:
for i in range(10):
ret = comm.Read('LargeArray[50]')
txt_file.write(str(ret.Value)+'\n')
time.sleep(1)
'''
the following import is only necessary because eip.py is not in this directory
'''
import sys
sys.path.append('..')
'''
Get the properties of a module in the specified slot
In this example, we're getting the slot 0 module
properties
'''
from pylogix import PLC
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
prop = comm.GetModuleProperties(0)
print(prop.Value.ProductName, prop.Value.Revision)
'''
import sys
sys.path.append('..')
'''
We're going to log a few tag values 10
times to a CSV file
For the first row, we'll write tag names,
then log each set of values with each read
'''
import csv
from pylogix import PLC
import time
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
tags = ['Zone1ASpeed','Zone1BSpeed','Zone2ASpeed','Zone2BSpeed']
with open('32_log.csv', 'w') as csv_file:
csv_file = csv.writer(csv_file, delimiter=',', lineterminator='\n', quotechar='/', quoting=csv.QUOTE_MINIMAL)
csv_file.writerow(tags)
for i in range(10):
ret = comm.Read(tags)
row = [x.Value for x in ret]
csv_file.writerow(row)
time.sleep(1)
'''
Write a program scoped tag
I have a program named "MiscHMI" in my main task.
In MiscHMI, the tag I'm reading will be TimeArray[0]
You have to specify that the tag will be program scoped
by appending the tag name with "Program" and the beginning,
then add the program name, finally the tag name. So our
example will look like this:
Program:MiscHMI.TimeArray[0]
'''
from pylogix import PLC
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
comm.Write('Program:MiscHMI.TimeArray[0]', 2019)
'''
the following import is only necessary because eip.py is not in this directory
'''
import sys
sys.path.append('..')
'''
Set the PLC clock
Sets the PLC clock to the same time as your computer
'''
from pylogix import PLC
with PLC() as comm:
comm.IPAddress = '192.168.1.9'
comm.SetPLCTime()