Skip to content

Commit

Permalink
Deleted example.py, added examples directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dmroeder committed Dec 30, 2018
1 parent 451c411 commit 61f78cb
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/01_read_simple.py
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()
21 changes: 21 additions & 0 deletions examples/02_read_simple.py
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)
26 changes: 26 additions & 0 deletions examples/03_read_program_scope.py
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)
26 changes: 26 additions & 0 deletions examples/04_read_array.py
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)

35 changes: 35 additions & 0 deletions examples/05_read_multiple_tags.py
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)
31 changes: 31 additions & 0 deletions examples/06_read_loop.py
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

49 changes: 49 additions & 0 deletions examples/07_read_first_instance.py
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

19 changes: 19 additions & 0 deletions examples/10_write_simple.py
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()
20 changes: 20 additions & 0 deletions examples/11_write_simple.py
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)
25 changes: 25 additions & 0 deletions examples/12_write_program_scope.py
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)
24 changes: 24 additions & 0 deletions examples/13_write_array.py
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)

37 changes: 37 additions & 0 deletions examples/20_discover_devices.py
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('')
22 changes: 22 additions & 0 deletions examples/21_get_plc_clock.py
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)

17 changes: 17 additions & 0 deletions examples/22_set_plc_clock.py
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()

Loading

0 comments on commit 61f78cb

Please sign in to comment.