def discoverDevices(): try: lbDevices.delete(0, 'end') commDD = PLC() devices = commDD.Discover() if str(devices) == 'None [] Success': lbDevices.insert(1, 'No Devices Discovered') else: i = 0 for device in devices.Value: lbDevices.insert(i * 12 + 1, 'IP Address: ' + device.IPAddress) lbDevices.insert(i * 12 + 2, 'Product Name: ' + device.ProductName) lbDevices.insert(i * 12 + 3, 'Product Code: ' + str(device.ProductCode)) lbDevices.insert(i * 12 + 4, 'Vendor: ' + device.Vendor) lbDevices.insert(i * 12 + 5, 'Vendor ID: ' + str(device.VendorID)) lbDevices.insert(i * 12 + 6, 'Device Type: ' + str(device.DeviceType)) lbDevices.insert(i * 12 + 7, 'Device ID: ' + str(device.DeviceID)) lbDevices.insert(i * 12 + 8, 'Revision: ' + device.Revision) lbDevices.insert(i * 12 + 9, 'Serial: ' + str(int(device.SerialNumber, 0))) lbDevices.insert(i * 12 + 10, 'State: ' + str(device.State)) lbDevices.insert(i * 12 + 11, 'Status: ' + str(device.Status)) lbDevices.insert(i * 12 + 12, '----------------------------------') i += 1 for device in devices.Value: if device.DeviceID == 14: lbDevices.insert(i * 12 + 1, 'Modules at ' + device.IPAddress) ''' Query each slot for a module ''' with PLC() as c: c.IPAddress = device.IPAddress for j in range(17): x = c.GetModuleProperties(j) lbDevices.insert(i * 12 + 2 + j, 'Slot ' + str(j) + ' ' + x.Value.ProductName + ' rev: ' + x.Value.Revision) c.Close() c = None i += 1 commDD.Close() commDD = None except Exception as e: if not commDD is None: commDD.Close() commDD = None if app_closing: pass else: print(str(e))
def main(): ''' Create our window and comm driver ''' global root global comm global ProductionCount # create a comm driver comm = PLC() comm.IPAddress = ipAddress # create a tkinter window root = Tk() root.config(background='black') root.title = 'Production Count' root.geometry('800x600') # bind the "q" key to quit root.bind('q', lambda event: root.destroy()) # create a labe to display our variable ProductionCount = Label(root, text='n', fg='white', bg='black', font='Helvetica 350 bold') ProductionCount.place(anchor=CENTER, relx=0.5, rely=0.5) # call our updater and show our window root.after(1000, UpdateValue) root.mainloop() comm.Close()
def SeqResume(): comm = PLC() comm.IPAddress = '169.254.215.82' comm.Write('SEQResume', False) time.sleep(.3) comm.Write('SEQResume', True) comm.Close() return
def PauseSEQ(): comm = PLC() comm.IPAddress = '169.254.215.82' comm.Write('SEQPause', True) time.sleep(.25) comm.Write('SEQPause', False) comm.Close() return
def StartSEQ(): comm = PLC() comm.IPAddress = '169.254.215.82' comm.Write('StartSeq', True) time.sleep(.3) comm.Write('StartSeq', False) comm.Close() return
def MasterOn(): comm = PLC() comm.IPAddress = '169.254.215.82' comm.Write('MasterStart', True) time.sleep(.25) comm.Write('MasterStart', False) comm.Close() return
def Emergency(): comm = PLC() comm.IPAddress = '169.254.215.82' comm.Write('EmergencyPB', False) time.sleep(.250) comm.Write('EmergencyPB', True) comm.Close() return
def getTags(): try: lbTags.delete(0, 'end') commGT = PLC() commGT.IPAddress = selectedIPAddress.get() if checkVarMicro800.get() == 0: commGT.ProcessorSlot = int(selectedProcessorSlot.get()) tags = commGT.GetTagList() if not tags is None: if not tags.Value is None: # save tags to a file if checkVarSaveTags.get() == 1: with open('tags_list.txt', 'w') as f: for t in tags.Value: if t.DataType == '': f.write(t.TagName + '\n') else: f.write(t.TagName + ' (DataType - ' + t.DataType + ')\n') for t in tags.Value: j = 1 if t.DataType == '': lbTags.insert(j, t.TagName) else: lbTags.insert(j, t.TagName + ' (DataType - ' + t.DataType + ')') j = j + 1 else: lbTags.insert(1, 'No Tags Retrieved') else: lbTags.insert(1, 'No Tags Retrieved') commGT.Close() commGT = None except Exception as e: if not commGT is None: commGT.Close() commGT = None if app_closing: pass else: print(str(e))
def WriteSEQCount(): comm = PLC() comm.IPAddress = '169.254.215.82' seqcount = entry1.get() seqcount = int(seqcount) comm.Write('SEQCount', seqcount) time.sleep(.22) comm.Close() return
def get_all_tags(ipAddress, slot): tags = [] comm = PLC(ipAddress, slot) ret = comm.GetTagList() comm.Close() if ret.Value is None: return jsonify(ret.Status) for tag in ret.Value: tags.append({"tagName": tag.TagName, "dataType": tag.DataType}) return jsonify({'tags': tags})
def get_devices(ipAddress, slot): devices = [] comm = PLC(ipAddress, slot) ret = comm.Discover() comm.Close() if ret.Value == []: return jsonify('No Devices Discovered') for device in ret.Value: devices.append({ "productName": device.ProductName, "revision": device.Revision }) return jsonify({'devices': devices})
from pylogix import PLC # Setup the PLC object with initial parameters # Change to your plc ip address, and slot, default is 0, shown for clarity comm = PLC('192.168.1.207', 0) # Read returns Response class (.TagName, .Value, .Status) ret = comm.Read('bool_01') print(ret.TagName, ret.Value, ret.Status) # Close Open Connection to the PLC comm.Close()
def main(): ''' Create our window and comm driver ''' global root global comm global tagValue global selectedProcessorSlot global selectedIPAddress global selectedTag global updateRunning global btnStart global btnStop global lbDevices global lbTags global tbIPAddress global sbProcessorSlot global tbTag global popup_menu_tbTag global popup_menu_tbIPAddress root = Tk() root.config(background='black') root.title('Pylogix GUI Test') root.geometry('800x600') updateRunning = True # bind the "q" keyboard key to quit root.bind('q', lambda event: root.destroy()) # add Exit button btnExit = Button(root, text='Exit', fg='red', height=1, width=10, command=root.destroy) btnExit.pack(side=BOTTOM, pady=5) # add button to start updating tag value btnStart = Button(root, text='Start Update', state='normal', fg='blue', height=1, width=10, command=startUpdateValue) btnStart.place(anchor=CENTER, relx=0.44, rely=0.6) # add button to stop updating tag value btnStop = Button(root, text='Stop Update', state='disabled', fg='blue', height=1, width=10, command=stopUpdateValue) btnStop.place(anchor=CENTER, relx=0.56, rely=0.6) # add list boxes for Devices and Tags lbDevices = Listbox(root, height=11, width=45, bg='lightblue') lbTags = Listbox(root, height=11, width=45, bg='lightgreen') lbDevices.pack(anchor=N, side=LEFT, padx=3, pady=3) # add scrollbar for the Devices list box scrollbarDevices = Scrollbar(root, orient="vertical", command=lbDevices.yview) scrollbarDevices.pack(anchor=N, side=LEFT, pady=3, ipady=65) lbDevices.config(yscrollcommand=scrollbarDevices.set) # copy selected IP Address to the clipboard on the mouse double-click # this is currently set to work for IP Address only lbDevices.bind('<Double-Button-1>', lambda event: ip_copy()) # add Discover Devices button btnDiscoverDevices = Button(root, text='Discover Devices', fg='brown', height=1, width=14, command=discoverDevices) btnDiscoverDevices.pack(anchor=N, side=LEFT, padx=3, pady=3) # add scrollbar for the Tags list box scrollbarTags = Scrollbar(root, orient="vertical", command=lbTags.yview) scrollbarTags.pack(anchor=N, side=RIGHT, padx=3, pady=3, ipady=65) lbTags.config(yscrollcommand=scrollbarTags.set) # copy selected tag to the clipboard on the mouse double-click lbTags.bind('<Double-Button-1>', lambda event: tag_copy()) lbTags.pack(anchor=N, side=RIGHT, pady=3) # add Get Tags button btnGetTags = Button(root, text='Get Tags', fg='brown', height=1, width=14, command=getTags) btnGetTags.pack(anchor=N, side=RIGHT, padx=3, pady=3) # create a label to display our variable tagValue = Label(root, text='Tag Value', fg='yellow', bg='navy', font='Helvetica 24', width=24) tagValue.place(anchor=CENTER, relx=0.5, rely=0.5) # create a label and a text box for the IPAddress entry lblIPAddress = Label(root, text='IP Address', fg='white', bg='black', font='Helvetica 8') lblIPAddress.place(anchor=CENTER, relx=0.5, rely=0.12) selectedIPAddress = StringVar() tbIPAddress = Entry(root, justify=CENTER, textvariable=selectedIPAddress) selectedIPAddress.set(ipAddress) # add the "Paste" menu on the mouse right-click popup_menu_tbIPAddress = Menu(tbIPAddress, tearoff=0) popup_menu_tbIPAddress.add_command(label='Paste', command=ip_paste) tbIPAddress.bind('<Button-3>', lambda event: ip_menu(event, tbIPAddress)) tbIPAddress.place(anchor=CENTER, relx=0.5, rely=0.15) # create a label and a spinbox for the ProcessorSlot entry lblProcessorSlot = Label(root, text='Processor Slot', fg='white', bg='black', font='Helvetica 8') lblProcessorSlot.place(anchor=CENTER, relx=0.5, rely=0.22) selectedProcessorSlot = StringVar() sbProcessorSlot = Spinbox(root, width=10, justify=CENTER, from_=0, to=20, increment=1, textvariable=selectedProcessorSlot, state='readonly') selectedProcessorSlot.set(processorSlot) sbProcessorSlot.place(anchor=CENTER, relx=0.5, rely=0.25) # create a label and a text box for the Tag entry lblTag = Label(root, text='Tag To Poll', fg='white', bg='black', font='Helvetica 8') lblTag.place(anchor=CENTER, relx=0.5, rely=0.38) selectedTag = StringVar() tbTag = Entry(root, justify=CENTER, textvariable=selectedTag, font='Helvetica 10', width=90) selectedTag.set(myTag) # add the "Paste" menu on the mouse right-click popup_menu_tbTag = Menu(tbTag, tearoff=0) popup_menu_tbTag.add_command(label='Paste', command=tag_paste) tbTag.bind('<Button-3>', lambda event: tag_menu(event, tbTag)) tbTag.place(anchor=CENTER, relx=0.5, rely=0.42) comm = PLC() discoverDevices() getTags() root.mainloop() comm.Close()
def get_tag(tag, ipAddress, slot): comm = PLC(ipAddress, slot) regularTags = [] arrayTags = dict() results = [] readArray = False arrayElementCount = 0 showBoolAsOneZero = False if tag.startswith('[') and tag.endswith(']'): # array of mixed tags tags = (tag[1:-1].replace(' ', '')).split(';') for t in tags: if not t == '': if t.endswith( '}') and '{' in t: # 1 or 2 or 3 dimensional array tag try: arrayElementCount = int(t[t.index('{') + 1:t.index('}')]) readArray = True t = t[:t.index('{')] arrayTags.update({t: arrayElementCount}) except: pass else: regularTags.append(t) if len(regularTags) > 0: ret = comm.Read(regularTags) if ret[0].Value is None: comm.Close() return jsonify(ret[0].Status) for i in range(0, len(ret)): if showBoolAsOneZero and (str(ret[i].Value) == 'True' or str(ret[i].Value) == 'False'): results.append({ "tagName": str(ret[i].TagName), "value": 1 if str(ret[i].Value) == 'True' else 0, "status": str(ret[i].Status) }) else: results.append({ "tagName": str(ret[i].TagName), "value": str(ret[i].Value), "status": str(ret[i].Status) }) if len(arrayTags) > 0: for tag in arrayTags: ret = comm.Read(tag, arrayTags[tag]) if ret.Value is None: comm.Close() return jsonify(ret.Status) if showBoolAsOneZero and (str(ret.Value[0]) == 'True' or str(ret.Value[0]) == 'False'): newBoolArray = [] for val in range(0, len(ret.Value)): newBoolArray.append(1 if str(ret.Value[val]) == 'True' else 0) results.append({ "tagName": ret.TagName, "value": newBoolArray, "status": ret.Status }) else: results.append({ "tagName": ret.TagName, "value": ret.Value, "status": ret.Status }) comm.Close() if len(regularTags) > 0 or len(arrayTags) > 0: return jsonify({'tags': results}) else: return jsonify('Not a valid tag!') else: if tag.endswith('}') and '{' in tag: # 1 or 2 or 3 dimensional array try: arrayElementCount = int(tag[tag.index('{') + 1:tag.index('}')]) readArray = True tag = tag[:tag.index('{')] except: pass else: pass if readArray and arrayElementCount > 0: ret = comm.Read(tag, arrayElementCount) if ret.Value is None: comm.Close() return jsonify(ret.Status) if showBoolAsOneZero and (str(ret.Value[0]) == 'True' or str(ret.Value[0]) == 'False'): newBoolArray = [] for val in range(0, len(ret.Value)): newBoolArray.append(1 if str(ret.Value[val]) == 'True' else 0) results.append({ "tagName": ret.TagName, "value": newBoolArray, "status": ret.Status }) else: results.append({ "tagName": ret.TagName, "value": ret.Value, "status": ret.Status }) else: ret = comm.Read(tag) if ret.Value is None: comm.Close() return jsonify(ret.Status) if showBoolAsOneZero and (str(ret.Value) == 'True' or str(ret.Value) == 'False'): results.append({ "tagName": ret.TagName, "value": 1 if str(ret.Value) == 'True' else 0, "status": ret.Status }) else: results.append({ "tagName": ret.TagName, "value": ret.Value, "status": ret.Status }) comm.Close() return jsonify({'tag': results})
class CondorSkill(MycroftSkill): # The constructor of the skill, which calls Mycroft Skill's constructor def __init__(self): super(CondorSkill, self).__init__(name="CondorSkill") self.myKeywords = [] # self.client = '' # mqtt.Client() # Initialize settings values self.client = mqtt.Client(self.id_generator()) #self.MQTT_Enabled = False #self.settings["MQTT_Enabled"] = False #self.broker_address = "192.168.0.43" #self.settings["broker_address"] = "" #self.broker_address #self.broker_port = 1883 #self.settings["broker_port"] = "" # self.broker_port #self.settings["plc_address"] = "" # '142.156.204.41' #self.plcOutTagName = "StartRobot" #self.settings["plc_out_tag_name"] = "" # self.plcOutTagName #self.plcInTagName = "RobotStarted" #self.settings["plc_in_tag_name"] = "" # self.plcInTagName #self.cardRequestFreq = 2 #self.settings["card_request_interval"] = 2 self.comm = PLC() self._is_setup = False self.io_pins = [] self.notifier_bool = True # This method loads the files needed for the skill's functioning, and # creates and registers each intent that the skill uses def initialize(self): self.io_pins = [ 3, 5, 7, 29, 31, 26, 24, 21, 19, 23, 32, 33, 8, 10, 36, 11, 12, 35, 38, 40, 15, 16, 18, 22, 37, 13 ] GPIO.setmode(GPIO.BOARD) self.load_data_files(dirname(__file__)) # Check and then monitor for credential changes self.settings.set_changed_callback(self.on_websettings_changed) self.on_websettings_changed() self.add_event('recognizer_loop:wakeword', self.handle_listen) # should be "utterance" self.add_event('recognizer_loop:utterance', self.handle_utterances) # should be "utterances" self.add_event('speak', self.handle_speak) # should be "utterance" def on_websettings_changed(self): # called when updating mycroft home page #if not self._is_setup: self.MQTT_Enabled = self.settings.get( "MQTT_Enabled", False) # used to enable / disable mqtt self.broker_address = self.settings.get("broker_address", "192.168.0.43") self.broker_port = self.settings.get("broker_port", 1883) self.comm.IPAddress = self.settings.get( "plc_address", '142.156.204.41') # PLC Address self.plcOutTagName = self.settings.get("plc_out_tag_name", "StartRobot") self.plcInTagName = self.settings.get("plc_in_tag_name", "RobotStarted") self.cardRequestFreq = self.settings.get("card_request_interval", 2) self._is_setup = True LOG.info("Websettings Changed! " + str(self.MQTT_Enabled) + ", " + self.broker_address + ", " + str(self.broker_port)) def id_generator(self, size=6, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) @intent_handler( IntentBuilder("GPIOIntent").require("GpioKeyword").one_of( "OnKeyword", "OffKeyword").build()) def handle_gpio_intent(self, message): str_limits = [] str_remainder = str(message.utterance_remainder()) if (str_remainder.find('for') != -1) or (str_remainder.find('four') != -1): str_limits = [4] else: str_limits = re.findall('\d+', str_remainder) if str_limits: gpio_request = int(str_limits[0]) if (gpio_request > 1) and (gpio_request < 28): pin_index = gpio_request - 2 board_pin = self.io_pins[pin_index] LOG.info('The pin number requested was: ' + str(board_pin)) if "OnKeyword" in message.data: self.gpio_on(board_pin, gpio_request) if "OffKeyword" in message.data: self.gpio_off(board_pin, gpio_request) else: self.speak_dialog("error", data={"result": str(gpio_request)}, wait=True) else: self.speak('No GPIO Pin was specified') @intent_handler( IntentBuilder("WikiIntent").require("TellKeyword").require( "AboutKeyword").require("ConestogaKeyword").build()) def handle_wiki_intent(self, message): LOG.info('Condor.ai was asked: ' + message.data.get('utterance')) str_remainder = str(message.utterance_remainder()) self.speak_dialog("about", wait=True) self.card_conversation() @intent_handler( IntentBuilder("AcademicIntent").require("WhatKeyword").require( "AcademicKeyword").optionally("ConestogaKeyword").build()) def handle_academic_intent(self, message): LOG.info('Condor.ai was asked: ' + message.data.get('utterance')) str_remainder = str(message.utterance_remainder()) self.speak_dialog("academic1", wait=True) self.speak_dialog("academic2", wait=True) self.card_conversation() # @intent_handler(IntentBuilder("CampusIntent").require("WhereKeyword"). # require("CampusKeyword").optionally("ConestogaKeyword").build()) @intent_handler( IntentBuilder("CampusIntent").require("WhereKeyword").optionally( "CampusKeyword").require("ConestogaKeyword").build()) def handle_campus_intent(self, message): LOG.info('Condor.ai was asked: ' + message.data.get('utterance')) str_remainder = str(message.utterance_remainder()) self.speak_dialog("campus_intro", wait=True) self.speak_dialog("campus", wait=True) self.card_conversation() @intent_handler( IntentBuilder("SetStackLightIntent").require("SetKeyword").require( "StackLightKeyword").require("ColorKeyword").build()) def handle_set_stack_light_intent(self, message): LOG.info('Condor.ai was asked: ' + message.data.get('utterance')) color_kw = message.data.get("ColorKeyword") self.speak_dialog("set_stacklight", data={"result": str(color_kw)}, wait=True) @intent_handler( IntentBuilder("RobotStartIntent").require("BusinessKeyword").require( "CardKeyword").optionally("ConestogaKeyword").build()) def handle_robot_start_intent(self, message): LOG.info('Condor.ai was asked: ' + message.data.get('utterance')) str_remainder = str(message.utterance_remainder()) self.start_robot() @intent_handler( IntentBuilder("CardConversationIntent").require( "BusinessCardContextKeyword").one_of('YesKeyword', 'NoKeyword').build()) def handle_card_conversation_intent(self, message): LOG.info('Condor.ai was asked: ' + message.data.get('utterance')) str_remainder = str(message.utterance_remainder()) self.set_context('BusinessCardContextKeyword', '') if "YesKeyword" in message.data: self.start_robot() else: self.speak_dialog("no_card", wait=False) def card_conversation(self): low_number = 1 high_number = self.cardRequestFreq my_number = random.randint(low_number, high_number) LOG.info('Card Request Context ID: ' + str(my_number) + '/' + str(high_number)) if my_number == high_number: self.set_context('BusinessCardContextKeyword', 'SetBusinessCardContext') self.speak_dialog("ask_card", wait=True, expect_response=True) def gpio_on(self, board_number, gpio_request_number): GPIO.setup(board_number, GPIO.OUT, initial=0) GPIO.output(board_number, True) LOG.info('Turning On GPIO Number: ' + str(gpio_request_number)) self.speak_dialog("on", data={"result": str(gpio_request_number)}, wait=True) def gpio_off(self, board_number, gpio_request_number): GPIO.setup(board_number, GPIO.OUT, initial=0) GPIO.output(board_number, False) LOG.info('Turning Off GPIO Number: ' + str(gpio_request_number)) self.speak_dialog("off", data={"result": str(gpio_request_number)}, wait=True) def get_card(self, program_select): GPIO.setup(program_select, GPIO.OUT, initial=0) GPIO.output(program_select, True) def start_robot(self): LOG.info(self.comm.IPAddress) self.write_plc("StartRobot", 1) LOG.info('PLC Output Should be On') self.speak_dialog("retrieve_card", wait=False) sleep(1) self.write_plc(self.plcOutTagName, 0) LOG.info('PLC Output Should be Off') inTag = self.comm.Read(self.plcInTagName) while inTag.value == 0: inTag = self.comm.Read(self.plcInTagName) LOG.info('Checking Robot Complete Status: ' + str(inTag.value)) if inTag.value == 1: self.speak_dialog("card_delivered", wait=False) else: sleep(1) def write_plc(self, myTagName, myTagValue): LOG.info('Writing: ' + myTagName + ' A value of: ' + str(myTagValue)) self.comm.Write(myTagName, myTagValue) self.comm.Close() # listening event used for kodi notifications def handle_listen(self, message): voice_payload = message.data.get('utterance') if self.notifier_bool: try: LOG.info(voice_payload) except Exception as e: LOG.error(e) self.on_websettings_changed() # utterance event used for notifications ***This is what the user says*** def handle_utterances(self, message): voice_payload = str(message.data.get('utterances')[0]) if self.notifier_bool: try: LOG.info(voice_payload) self.send_MQTT("Mycroft/Student", voice_payload) except Exception as e: LOG.error(e) self.on_websettings_changed() # mycroft speaking event used for notificatons ***This is what mycroft says*** def handle_speak(self, message): voice_payload = message.data.get('utterance') if self.notifier_bool: try: LOG.info(voice_payload) self.send_MQTT("Mycroft/AI", voice_payload) #self.card_conversation() except Exception as e: LOG.error(e) self.on_websettings_changed() def send_MQTT(self, myTopic, myMessage): if self.MQTT_Enabled: LOG.info("MQTT: " + myTopic + ", " + myMessage) myID = self.id_generator() #LOG.info("MyID: " + str(myID)) #self.client = mqtt.Client(myID) #self.client.connect(self.broker_address, self.broker_port) # connect to broker #self.client.publish(myTopic, myMessage) # publish #self.client.disconnect() LOG.info("address: " + self.broker_address + ", Port: " + str(self.broker_port)) publish.single(myTopic, myMessage, hostname=self.broker_address) else: LOG.info( "MQTT has been disabled in the websettings at https://home.mycroft.ai" ) def stop(self): pass