Skip to content

Instantly share code, notes, and snippets.

@TheFern2
Created November 30, 2018 07:36
Show Gist options
  • Save TheFern2/ee45074f2eac20bf340841d8562c832f to your computer and use it in GitHub Desktop.
Save TheFern2/ee45074f2eac20bf340841d8562c832f to your computer and use it in GitHub Desktop.
from pyfirmata import Arduino, util
import time
from eip import PLC
def boolean_read(tag):
'''
simple tag read
'''
return comm.Read(tag)
# define our pylogix communication
# change to your softlogix plc IP!
# change to your softlogix slot!
comm = PLC()
comm.IPAddress = '192.168.0.15'
comm.ProcessorSlot = 2
# Legacy code straight serial requires constant arduino code updates
# not a bad thing, if you aren't using regular IO
# serial example
# s = serial.Serial('COM3', 9600, timeout=1)
# time.sleep(5)
# #s.write(b'2')
# while True:
# if not close_serial:
# if led_status:
# s.write(b'1')
# if not led_status:
# s.write(b'2')
# time.sleep(0.3)
# s.close()
# firmata example
# two controller tags from PLC
close_serial = boolean_read('Close_Serial')
led_status = boolean_read("Led_On")
# define com port and iterator
board = Arduino("COM3")
it = util.Iterator(board)
it.start()
# define hardware input, and output
sw = board.get_pin('d:2:i')
led = board.get_pin('d:13:o')
# run while close_serial is false
while not close_serial:
sw_value = sw.read()
led_status = boolean_read("Led_On")
close_serial = boolean_read('Close_Serial')
# if hardware sw is pushed we
# send bit to softlogix tag program tag Turn_Led
if sw_value == 1:
comm.Write("Program:MainProgram.Turn_Led", 1)
else:
comm.Write("Program:MainProgram.Turn_Led", 0)
# if Controller tag Led_On is True turn LED on pin 13
if led_status:
led.write(1)
else:
led.write(0)
board.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment