Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

How to write variable by name

zorgoz
2016-11-11
2017-02-27
  • zorgoz - 2016-11-11

    Hello experts,

    I need to be able to apply variable value sets by uploading a file via FTP to a WAGO 750-831 controller.
    You can simpli imagine this file as a name-value pair set like an INI.

    Let's say the file contains these (but might be several hundreds of them, and it will not contain always all variables):

    PLC_PRG.MyVar01=100
    PLC_PRG.MyVar02=200
    

    I have seen that parsing the file won't be a problem in CoDeSys v2.3.

    But supposing that I have the name of the variable to be updated in a string variable (VARNAME) and the new value in an other (NEWVALUE), what is the best (resource and coding friendly) way to set the variable? I can imagine a big IF-THEN, but I hope that is not the best solution. I was thinking about some workarounds but first I want to ask if there is any direct solution.

    Thank you in advance.

    [Edit 11.11 16:44]
    Unfortunately SysLibGetSymbolAddress is not available on the target.

     
  • shooter - 2016-11-12

    you can NOT change variable names, however in an array you can store any names set and the values too.
    or you can just put them in designated places in an array

     
  • zorgoz - 2016-11-13

    shooter hat geschrieben:
    you can NOT change variable names, however in an array you can store any names set and the values too.
    or you can just put them in designated places in an array

    Hello,
    I don't want to change the name of a variable, only the value.
    Ok. This is one option I was thinking about. Although it has some drawbacks. First of all, in my case the high number (200-300) of variables makes this approach error prone.

    I was thinking to get the address of the variable by parsing the symxml.SYM_XML symbol table. It is a promising approach. Still, it's content looks confusing. The SymbolVarList/Var/@Offset looks correct but relative to SysLibGetAddress(DATAID_GLOBVARS), but these are not globals. And the segment base addresses in the symxml look totally unrelated to the base address returned by SysLibGetAddress. See attachment.
    Do you have any idea how to interpret symxml correctly?

    IMG: image4249.png

     
  • Anonymous - 2016-11-14

    Originally created by: scott_cunningham

    I think you will need a big case statement. (Tip - Use MS Excel to build you case statement lines). When you send the file, the variable name would be a string for the case statement.

     
  • zorgoz - 2016-11-14

    scott_cunningham hat geschrieben:
    I think you will need a big case statement. (Tip - Use MS Excel to build you case statement lines). When you send the file, the variable name would be a string for the case statement.

    Thank you. The "" I was hoping not to need But if I have to stick to that, I was also thinking of generating it.

    Anyway, CASE itself looks not to be an option, as when I tried to use it with strings, I got the error message, that .

     
  • Anonymous - 2016-11-14

    Originally created by: scott_cunningham

    Yes, I see now that CASE needs a number in IEC61131.

     
  • shooter - 2016-11-14

    if the names are always same (only the number is different please use an array and get the number from the string.
    if the names are all different and you do not want to use pointers, make an big array with all the names and a for next loop to find every string, and update the value

     
  • zorgoz - 2017-02-14

    What I have done is that I made my own symbol file from symxml. As there is a function that can return the starting address of the memory areas, I am storing in binary format in my file the symbol name, the memory area index, area relative memory offset and a code for the variable type (I am expanding all arrays and structures as needed even if they are not expanded in symxml). At PLC startup I am loading the content from this file into an array in memory. When I need to change a variable by name, I look for it in this array. As I know the type, I can now easily calculate the address, and it is now simple to set it.

     
  • shooter - 2017-02-15

    you can use pointers to find where a var is located.
    please have a look at w www.oscat.de w for ways to do this.

     
  • zorgoz - 2017-02-17

    shooter hat geschrieben:
    you can use pointers to find where a var is located.
    please have a look at http://www.oscat.de for ways to do this.

    I have looked trougd OSCAT allready. Which OSCAT POU can find a variable's address using the variable name?

     
  • hermsen

    hermsen - 2017-02-27

    Bump!

    I am also very interested in this Technique. I see possibilities in combining this with a protocol format like JSON would be very useful.
    E.g. finding a varname out of a file and searching for it in the runtime and read/write it.

     

Log in to post a comment.