Preparatory work:
In Codesys
Use a preferably current Codesys IDE version
From Codesys version SP18 on forward, install the Codesys Scripting add-on via the Codesys installer
- Create a standard project with a Codesys WinV3 PLC
in Python
- Create a Python Script with the name, for example: scan_and_download.py.
- Include the following code into the script:
GatewayName = '<Name Gateway>' TargetName = '<Name in Scan>' DeviceName = '<Name in Device Tree>' #find DeviceName in the Device Tree; has to be unique found = projects.primary.find(DeviceName, False) print('found "{}" in device tree'.format (DeviceName)) assert(found and len(found) == 1, 'No or more than one device found') dev = found[0] # set the selected Gateway for scan gw = online.gateways[GatewayName] # execute scan print ('start scan for devices') targets = gw.perform_network_scan() # check if TargetName is in Scan result, only 1 is expected matchingTarget = None for target in targets: if target.device_name == TargetName: print("Name: {} Addr: {}".format(target.device_name, target.address)) matchingTarget = target; break # set the commuication path with scanned device if matchingTarget: print('Set communication path of device ') dev.set_gateway_and_device_name(gw, target.device_name) # or # dev.set_gateway_and_address(gw, target.address) else: assert('No matching target found') # or set the communication path with IP #IPAddress = '<IP Address PLC xxx.xxx.xxx.xxx<' #dev.set_gateway_and_ip_address(gw, IPAddress) # create online application onlineApp = online.create_online_application() print ('login with download application') onlineApp.login(OnlineChangeOption.Never, True) print ('create boot application ') onlineApp.create_boot_application() print ('logout') onlineApp.logout()
Use / execute
- Open the preliminarily created Codesys Project.
- Execute the script scan_and_download.py via the Command: Scripting – Execute Script File.
The execution will be reported in the Script Messages.
For communication path and login, no user / password is set.
Please note that the given code examples are only intended to illustrate possible structures.
The examples do not claim to be complete or error-free.