forked from dmroeder/pylogix
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deleted example.py, added examples directory
- Loading branch information
dmroeder
committed
Dec 30, 2018
1 parent
451c411
commit 61f78cb
Showing
21 changed files
with
529 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
''' | ||
the following import is only necessary because eip is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
The simplest example of reading a tag from a PLC | ||
NOTE: You only need to call .Close() after you are done exchanging | ||
data with the PLC. If you were going to read in a loop or read | ||
more tags, you wouldn't want to call .Close() every time. | ||
''' | ||
from eip import PLC | ||
|
||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
value = comm.Read('CurrentScreen') | ||
print(value) | ||
comm.Close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
A simple single read using a with statement. | ||
One advantage of using a with statement is that | ||
you don't have to call .Close() when you are done, | ||
this is handled automatically. | ||
''' | ||
from eip import PLC | ||
|
||
with PLC() as comm: | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
value = comm.Read('CurrentScreen') | ||
print(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
Read 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 eip import PLC | ||
|
||
with PLC() as comm: | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
value = comm.Read('Program:MiscHMI.TimeArray[0]') | ||
print(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
Read an array of values | ||
I have a tag called "LargeArray", which is DINT[10000] | ||
We can read as many of them as we'd like, which makes | ||
reading arrays the most efficient way to read data. | ||
Read will handle multi-packet replies. | ||
We're going to pass Read() the tag and the number | ||
to read. | ||
''' | ||
from eip import PLC | ||
|
||
with PLC() as comm: | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
values = comm.Read('LargeArray[0]', 500) | ||
print(values) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
Read a list of tags at once | ||
Reading lists and arrays is much more efficient than | ||
reading them individually. You can create a list of tags | ||
and pass it to .Read() to read them all in one packet. | ||
The values returned will be in the same order as the tags | ||
you passed to Read() | ||
NOTE: Packets have a ~500 byte limit, so you have to be cautions | ||
about not exceeding that or the read will fail. It's a little | ||
difficult to predict how many bytes your reads will take up becuase | ||
the send packet will depend on the length of the tag name and the | ||
reply will depened on the data type. Strings are a lot longer than | ||
DINT's for example. | ||
I'll usually read no more than 5 strings at once, or 10 DINT's) | ||
''' | ||
from eip import PLC | ||
|
||
tag_list = ['Zone1ASpeed', 'Zone1BSpeed', 'Zone2ASpeed', 'Zone2BSpeed', 'Zone3ASpeed', 'Zone3BSpeed', | ||
'Zone4ASpeed', 'ZOne4BSpeed', 'Zone1Case', 'Zone2Case'] | ||
|
||
with PLC() as comm: | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
value = comm.Read(tag_list) | ||
print(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
''' | ||
the following import is only necessary because eip is not in this directory | ||
''' | ||
import sys | ||
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 eip import PLC | ||
import time | ||
|
||
with PLC() as comm: | ||
comm.IPAddress = '192.168.1.9' | ||
read = True | ||
while read: | ||
try: | ||
value = comm.Read('LargeArray[0]') | ||
print(value) | ||
time.sleep(1) | ||
except KeyboardInterrupt: | ||
print('exiting') | ||
read = False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
''' | ||
the following import is only necessary because eip is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
Monitor a tag, when we see a certain value, | ||
call another function once. | ||
I often finding myself needing to trigger some | ||
other code the first time a BOOL goes true, for | ||
example. I only want the function to fire once | ||
so we'll have to make sure it goes false before | ||
firing again. | ||
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 eip 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: | ||
value = comm.Read('PE040') | ||
time.sleep(1) | ||
if value: | ||
FaultHappend() | ||
while value: | ||
value = comm.Read('PE040') | ||
time.sleep(1) | ||
except KeyboardInterrupt: | ||
print('exiting') | ||
read = False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
''' | ||
the following import is only necessary because eip is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
The simplest example of writing a tag from a PLC | ||
NOTE: You only need to call .Close() after you are done exchanging | ||
data with the PLC. If you were going to read/write in a loop or read/write | ||
more tags, you wouldn't want to call .Close() every time. | ||
''' | ||
from eip import PLC | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
comm.Write('CurrentScreen', 10) | ||
comm.Close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
A simple single write using a with statement. | ||
One advantage of using a with statement is that | ||
you don't have to call .Close() when you are done, | ||
this is handled automatically. | ||
''' | ||
from eip import PLC | ||
|
||
with PLC() as comm: | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
comm.Write('CurrentScreen', 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
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 eip import PLC | ||
|
||
with PLC() as comm: | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
comm.Write('Program:MiscHMI.TimeArray[0]', 2019) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
|
||
''' | ||
Write an array of values | ||
I have a tag called "LargeArray", which is DINT[10000] | ||
We can write a list of values all at once to be more efficient. | ||
You should be careful not to exceed the ~500 byte limit of | ||
the packet. You can pack quite a few values into 500 bytes. | ||
''' | ||
from eip import PLC | ||
|
||
values = [8,6,7,5,3,0,9] | ||
|
||
with PLC() as comm: | ||
comm = PLC() | ||
comm.IPAddress = '192.168.1.9' | ||
comm.Write('LargeArray[10]', values) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
''' | ||
the following import is only necessary because eip.py is not in this directory | ||
''' | ||
import sys | ||
sys.path.append('..') | ||
|
||
''' | ||
Discover Ethernet I/P Devices | ||
This will send a broadcast packet out, all Ethernet I/P | ||
devices will respond with the following information about | ||
themselves: | ||
EncapsulationVersion | ||
IPAddress | ||
VendorID | ||
Vendor | ||
DeviceID | ||
DeviceType | ||
ProductCode | ||
Revision | ||
Status | ||
SerialNumber | ||
ProductNameLength | ||
ProductName | ||
State | ||
''' | ||
from eip import PLC | ||
|
||
with PLC() as comm: | ||
devices = comm.Discover() | ||
for device in devices: | ||
print(device.IPAddress) | ||
print(' Product Code: ' + device.ProductName + ' ' + str(device.ProductCode)) | ||
print(' Vendor/Device ID:' + device.Vendor + ' ' + str(device.DeviceID)) | ||
print(' Revision/Serial:' + device.Revision + ' ' + device.SerialNumber) | ||
print('') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
''' | ||
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 eip import PLC | ||
|
||
with PLC() as comm: | ||
comm.IPAddress = '192.168.1.9' | ||
value = comm.GetPLCTime() | ||
|
||
# print the whole value | ||
print(value) | ||
# print each pice of time | ||
print(value.year, value.month, value.day, value.hour, value.minute, value.second, value.microsecond) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
''' | ||
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 eip import PLC | ||
|
||
with PLC() as comm: | ||
comm.IPAddress = '192.168.1.9' | ||
comm.SetPLCTime() | ||
|
Oops, something went wrong.