def discoverPLCs(): # Function to discover any PLC on the network ips, slots, progName = [], [], [] try: discovery = CIPDriver.discover() # Return list of all CIP devices on the network for device in discovery: # Go through discovery list and append any PLC#'s to a list if device['product_type'] == "Programmable Logic Controller": ips.append(device['ip_address']) if len(ips) > 0: # Print the discovered PLC's, if there are any ips.sort() # Sort the IP address in ascending order table = Table(box=box.ROUNDED) # Create table table.add_column('#', justify='center') # Add column table.add_column('Device Type', justify='center') # Add column table.add_column('IP Address', justify='center') # Add column table.add_column('Slot #', justify='center') # Add column table.add_column('Program Name', justify='center') # Add column for i, ip in enumerate(ips): # Add row for each PLC discovered slots.append('Unknown') progName.append('Unknown') for slot in range(1, 18): try: plc = LogixDriver(f'{plc}/{str(slot)}', init_tags=False) if plc.open(): slots[i] = slot progName[i] = plc.get_plc_name() plc.close() break except: continue table.add_row(str(i+1), 'Programmable Logic Controller', ip, str(slots[i]), progName[i]) print(table) else: print("No PLC's discovered on the network") except Exception: traceback.print_exc()
def test_discover(): from pycomm3 import CIPDriver devices = CIPDriver.discover() expected = [ {'encap_protocol_version': 1, 'ip_address': '192.168.1.237', 'vendor': 'Rockwell Automation/Allen-Bradley', 'product_type': 'Communications Adapter', 'product_code': 185, 'revision': {'major': 2, 'minor': 7}, 'serial': '73015738', 'product_name': '1763-L16BWA B/7.00', 'state': 0}, {'encap_protocol_version': 1, 'ip_address': '192.168.1.236', 'vendor': 'Rockwell Automation/Allen-Bradley', 'product_type': 'Communications Adapter', 'product_code': 191, 'revision': {'major': 20, 'minor': 19}, 'serial': 'c01ebe90', 'product_name': '1769-L23E-QBFC1 Ethernet Port', 'state': 3}, # {'encap_protocol_version': 1, 'ip_address': '192.168.1.125', 'vendor': 'Rockwell Software, Inc.', # 'product_type': 'Communications Adapter', 'product_code': 115, 'revision': {'major': 12, 'minor': 1}, # 'serial': '21ac1903', 'product_name': 'DESKTOP-907P98D', 'state': 255} ] # status can change based on number of connections or other reasons # just check to make sure it has a value then remove it from the # rest of the device info # for device in devices: # assert 'status' in device # del device['status'] # assert device in expected for device in devices: assert 'ip_address' in device assert 'vendor' in device assert 'product_type' in device assert 'product_code' in device assert 'revision' in device