Access and modify variables with external application or script

desanta
2020-06-20
2020-07-12
  • desanta - 2020-06-20

    Greetings, I'm currently working with CoDeSys V2.3. During simulation I want to be able to read and write variables with an external application or script (e. g. a Python script). Both CoDeSys and the external application should be running on the same local machine. No external hardware should be involved in the scenario.

    To be more specific, I want to simulate a controller. The user should be able to write a program to set some variables (output), the external application would read those variables and set some other variables (input) in return.

    Example:

    The program simply increments a counter variable on each cycle, now the variable named "ACTIVE" should be modified by an external application and the result should be shown in CoDeSys. Therefore the counting should start or stop depending on the external application or script.

    I'm not sure whether there is an API or communication protocol available that could allow this scenario.

    Best regards!

     

    Last edit: desanta 2020-06-20
  • JAPIB

    JAPIB - 2020-06-23

    Hi,

    In simulation mode there is not any communication between your CoDeSys application and outer.
    You must use the soft PLC (PLCWinNT, installed on your PC with CoDeSys) ) to communicate with another application. (OPC server, ...)

    BR

     
  • desanta - 2020-06-26

    Hey, thanks a lot for your answer and interest!

    If I understand correctly, PLCWinNT is like a virtual PLC running on my machine, right?
    When started, a TCP/IP-address and a RS232-address are displayed.
    Which configuration do I need to change in CoDeSys to be able to use PLCWinNT?

    Finally, how would I be able to communicate with PLCWinNT? You mentioned OPC. In this case, would PLCWinNT provide the OPC server and my own application would be the OPC client?

    Best regards

     

    Last edit: desanta 2020-06-26
  • JAPIB

    JAPIB - 2020-06-28

    Hi,

    PLCWinNT is a real soft PLC.
    It is fully operationnal, but without licence it runs during two hours.
    To test a program it is fully sufficiant.

    To use it, you need :

    1Β°) Choose PLCWinNT in Target Setting. (Ressources -> Target Setting)
    2Β°) Uncheck the simulation option (Online Menu)
    3Β°) Define Communication parameters (Online Menu)

    Address IP is Localhost (Internal IP addresse of your PC) or the real IP address of your PC.
    4Β°) Start PLCWinNT
    5Β°) Login (Online Menu), if there is no program on the controller, you can download the new program.
    6Β°) Start the PLC (Online Menu --> Run)
    7)) You can test your program in the same way you used with simulation mode.
    The difference is, when you logout Soft PLC still running, so when you login

    You have to start OPC Configurator to set and start the CODESYS OPC server running on your PC (installed wiht CoDeSys).
    After you need an OPC Client (for example your own application) to test communication.

     
  • desanta - 2020-07-04

    Hey again! Thanks a lot for your help so far! Somehow I had problems with the connection to PLCWinNT so I re-installed everything and now it works! I can load a program into PLCWinNT and start it, furthermore I can watch the variables change in CoDeSys.

    Finally I need to get the OPC connection to work. For now I wanted to use UaExpert to test the connection. Later I would like to use either a Python application with the "opcua" library or some C# application to read and write variables over OPC.

    Do I need to keep the "OPC Configurator" running or is it only needed to set the configuration once?

    I can't seem to find the connection URL for OPC.
    I researched a bit and found some forms of
    "opc.tcp://localhost:4840" or "opcua://localhost:4840"
    but I'm not sure whether the OPC server is even running.

    Kind regards

     
  • JAPIB

    JAPIB - 2020-07-04

    Hi,
    The OPC Configurator is used to configure the OPC server.
    As soon as you save the configuration this starts the OPC server and you can then close the configurator.
    When you want to connect with your OPC client, you should see "CoDeSys OPC02" or "OPC Server for CoDeSys V2.0" which is the server on which to connect.

    Please note, I discovered that the name of your CoDesys project must not contain spaces.
    And when you define this name in the OPC configurator, you only have to put the name, without extension.

    Best regards

     
  • desanta - 2020-07-11

    Hey, at first I got confused because I didn't know the difference between OPC UA and OPC DA, so obviously my OPC UA client could not find anything running.

    It finally worked with the "MatrikonOPC Explorer" client for OPC DA, the OPC server showed up as "CoDeSys.OPC.02". In the next days I will experiment with some libraries to whip up an application to show and edit values.

    Thanks again for your help so far! I wouldn't have made any progress without your help!

     
  • JAPIB

    JAPIB - 2020-07-12

    Sorry, I didn't pay attention to the client you were using.
    The CoDeSys V2 OPC server works in OPC DA and the CODESYS V3 OPC server works in OPC UA.
    I'm glad to have helped you. This is the purpose of the forum.
    BR

     

Log in to post a comment.