I have started to setup a continuous integration for our project using the python api and get a login failure if project gets loaded a second time. A made a reproducer:
importscriptengineimportosimporttimeforattempt,_inenumerate(range(2),start=1):  print('*'*100)  print('Load and login attempt: {0}'.format(attempt))  print('*'*100)  ifprojects.primary:    print('Closing previously open project')    projects.primary.close()  path_project=os.path.join(os.getcwd(),r'codesys_dummy.project')  print('Opening project: {0}'.format(path_project))  project=projects.open(path_project)  gw=online.gateways['Gateway-1']  print('Gateway: {0}'.format(gw))  dev=projects.primary.find('Device',False)[0]  print('Device: {0}'.format(dev))  pc=os.environ['COMPUTERNAME']  print('PC: {0}'.format(pc))  dev.set_gateway_and_device_name(gateway=gw,device_name=pc)  proj=projects.primary  app=proj.active_application  onlineapp=online.create_online_application(app)  onlineapp.login(OnlineChangeOption.Never,False)  onlineapp.start()time.sleep(10)
The first time through the loop it finds device and logs in without a problem. The second time it fails. This is 100% reproducible.
I am guessing that there is something that needs to be done to clean up the environment before the second iteration in addition the project close.
We get same issue when using svn to load a project
It seems that some environment information is saved in the project file: if the IDE run up with UI and the project opened, network scan and go online, then the project saved - then the same script run again it logs in Ok both times...
Any help with identifying how to reset/clean the environment between iterations (without exiting the python script ) would be welcome.
Kind Regards
Varley
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You can try to use "system.delay(100)" instead of "time.sleep(100)" because the system.delay() also pumps the message queue. Some stuff in CODESYS are queue and without pumping the message queue they will not be done.
If that does not help you can try to increase the time to 500 or even 1000.
BR
Martin
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for your post. Unfortunately just adding system.delay (tried up to several seconds) in the loop does not fix it.
We did dig into this and eventually managed to get something that would run in a loop but it is not very satisfactory. Also the issue is not just whether it can run multiple times but also in circumstances with a very "clean" setup it would not manage to setup gateway and login the first time.
So to make it work we had to:
Run it in UI mode - behaviour seems to be different to headless
Clean out all old .opt files from the work area
Copy a "golden" .opt file with the name formatted as follows:
    user_name=getpass.getuser()    domain_name=socket.getfqdn().split('.',1)[-1].replace('.group','').upper()    opt_file_name='{0}-{1}-{2}.opt'.format(project_name,user_name,domain_name)    shutil.copy('default.opt',os.path.join(target,opt_file_name))
This opt file is one copied from an environment where the gateway scanning page has been opened.
Checkout project
Then run this bit of code:
      gw=online.gateways['Gateway-1']      devices=self.project.find('Device',False)      dev=devices[0]      dev.set_gateway_and_device_name(gateway=gw,device_name=dev_name)      system.commands["devicecommunication","setactivepath"].execute()      system.commands["file","save"].execute()
The key seems to be the "setactivepath" which is also a dialogue box that you have to OK the very first time the IDE is used.
So all a bit clunky and surly there is an easier way to do this? - or perhaps not ...
Eventually we want to be able to create the codesys installation from scratch as part of build process which is why we make the effort to be able reliably load and run the project in reliable way without having had to interact manually with the IDE.
Another issue we have is there does not seem to be an easy way via python to make an svn update to a particular revision. The default for svn.checkout() is head and there are no arguments to specify revision. Did not find anything like svn.update() available (at least I could not find it anywhere)
All I found was perhaps using:
system.commands["svn","update"].execute()
but I could not work out (or find any documentation) of how to call this method with the svn revision number.
Do you happen to know ?
Thanks in advance and
Kind Regards
Varley
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have started to setup a continuous integration for our project using the python api and get a login failure if project gets loaded a second time. A made a reproducer:
The first time through the loop it finds device and logs in without a problem. The second time it fails. This is 100% reproducible.
I am guessing that there is something that needs to be done to clean up the environment before the second iteration in addition the project close.
Script is run as follows:
Here is the output from running the script:
Load and login attempt: 1
Opening project: C:\svn\CommonTest\Resources\CodesysEnvironment\codesys_dummy.project
Gateway: <_3S.CoDeSys.ScriptDriverOnline.ScriptGateway object at 0x000000000000006C [_3S.CoDeSys.ScriptDriverOnline.ScriptGateway]>
Device: ScriptObject{ScriptDeviceObject, NoExplicitConnectorObject, NoSymbolConfigObject, NoLibManObject, ScriptNoProjectInfoMarker, NoScriptApplicationObject, ScriptNonTextualObject, ScriptExternalFileObjectContainer, NoTaskConfigOb
ject, NoTaskObject, NoImagePoolObject, NoTextListObject, NoScriptApplicationComposerObject, NoScriptApplicationComposerObject, SvnScriptObjectExtender}(Project=0, Name=Device, guid=e996c1ab-272b-4343-8cca-c366568b651a)
PC: ZLUKEPC0JNGFY
Build: Text: C0: ------ Build started: Application: Device.Application -------
Build: Text: C0: typify code ...
Build: Text: C0: Compile complete -- 0 errors, 0 warnings
Build: Text: C0: ------ Build started: Application: Device.Application -------
Build: Text: C0: The application is up to date
Build: Information: Number of published variables...
Build: Information: - total: 9
Build: Information: - with read access: 9
Build: Information: - with write access: 9
Build: Information: - with execute right: 0
Build: Information: Number of published types...
Build: Information: - types: 2
Build: Information: - members: 0
Build: Information: Number of published symbols: 9
Build: Information: Generated XML file: C:\svn\CommonTest\Resources\CodesysEnvironment\codesys_dummy.Device.Application.xml
Build: Text: C0: generate code...
Build: Text: C0: generate global initializations ...
Build: Text: C0: generate code initialization ...
Build: Text: C0: generate relocations ...
Build: Information: C0: Size of generated code: 438916 bytes
Build: Information: C0: Size of global data: 70611 bytes
Build: Information: C0: Total allocated memory size for code and data: 549040 bytes
Build: Information: C0: Memory area 0 contains Data, Input, Output, Memory and Nonsafe Data: size: 1048576 bytes, highest used address: 110120, largest contiguous memory gap: 938456 bytes (89 %)
Build: Information: C0: Memory area 3 contains Code: size: 1048576 bytes, highest used address: 438920, largest contiguous memory gap: 609656 bytes (58 %)
Build: Text: C0: Build complete -- 0 errors, 0 warnings : ready for download!
Load and login attempt: 2
Closing previously open project
Opening project: C:\svn\CommonTest\Resources\CodesysEnvironment\codesys_dummy.project
Gateway: <_3S.CoDeSys.ScriptDriverOnline.ScriptGateway object at 0x000000000000006C [_3S.CoDeSys.ScriptDriverOnline.ScriptGateway]>
Device: ScriptObject{ScriptDeviceObject, NoExplicitConnectorObject, NoSymbolConfigObject, NoLibManObject, ScriptNoProjectInfoMarker, NoScriptApplicationObject, ScriptNonTextualObject, ScriptExternalFileObjectContainer, NoTaskConfigOb
ject, NoTaskObject, NoImagePoolObject, NoTextListObject, NoScriptApplicationComposerObject, NoScriptApplicationComposerObject, SvnScriptObjectExtender}(Project=54, Name=Device, guid=e996c1ab-272b-4343-8cca-c366568b651a)
PC: ZLUKEPC0JNGFY
Build: Text: C0: ------ Build started: Application: Device.Application -------
Build: Text: C0: typify code ...
Build: Text: C0: Compile complete -- 0 errors, 0 warnings
Error: Running script 'C:\svn\CommonTest\Resources\CodesysEnvironment\test_load_project_and_login.py' caused exception System.Exception: Traceback (most recent call last):
File "C:\svn\CommonTest\Resources\CodesysEnvironment\test_load_project_and_login.py", line 32, in <module>
Exception: Login failed...</module>
Some other observations:
Any help with identifying how to reset/clean the environment between iterations (without exiting the python script ) would be welcome.
Kind Regards
Varley
Hi Varley.
You can try to use "system.delay(100)" instead of "time.sleep(100)" because the system.delay() also pumps the message queue. Some stuff in CODESYS are queue and without pumping the message queue they will not be done.
If that does not help you can try to increase the time to 500 or even 1000.
BR
Martin
Hi Martin,
Thanks for your post. Unfortunately just adding system.delay (tried up to several seconds) in the loop does not fix it.
We did dig into this and eventually managed to get something that would run in a loop but it is not very satisfactory. Also the issue is not just whether it can run multiple times but also in circumstances with a very "clean" setup it would not manage to setup gateway and login the first time.
So to make it work we had to:
This opt file is one copied from an environment where the gateway scanning page has been opened.
The key seems to be the "setactivepath" which is also a dialogue box that you have to OK the very first time the IDE is used.
So all a bit clunky and surly there is an easier way to do this? - or perhaps not ...
Eventually we want to be able to create the codesys installation from scratch as part of build process which is why we make the effort to be able reliably load and run the project in reliable way without having had to interact manually with the IDE.
Another issue we have is there does not seem to be an easy way via python to make an svn update to a particular revision. The default for svn.checkout() is head and there are no arguments to specify revision. Did not find anything like svn.update() available (at least I could not find it anywhere)
All I found was perhaps using:
but I could not work out (or find any documentation) of how to call this method with the svn revision number.
Do you happen to know ?
Thanks in advance and
Kind Regards
Varley