exposing C++ variables as global variables
Hi Martin,
I want to create a function block with a variable from my program connected to it.
I want to be able to pass in an address-string (with letters, numbers and symbols) using the code interface in PLCNe/PCWE
First question:
I am looking on how to expose my variables globally to the PLC and i keep reading about declaring a variable using an 'external' option. I don't know exactly how this is done. Is it in the port definition, like below ? or did you mean the 'extern' C keyword. I tried some things but they never show up.
//#port
//#external (??)
//#attributes(Input|Retain)
//#name(address)
uint8 address[9];
Second question:
im using a uint8 [] instead of a string since that isnt yet supported but i wonder if it's possible to send a string to this variable from PLCnext for example from the port list because the value only shows [...], meaning its an array. I know i can update the individual chars from the watch window but that's not the preferred way.. In this screen I can perfectly toggle booleans between true and false or enter a value for an int so why not push a string to a uint8 []
Thanks in advance!!
Comments
Hi Thomas,
Answer to First Question
The “external” variables you saw mentioned were probably defined in an IEC program in PLCnext Engineer (ie a program written in Ladder, for example). This is done by defining the variable as “External”, rather than as a Port variable. Then, that variable is available as a “global” variable in the PLC.
There is currently no equivalent to this type of variable in C++ programs for PLCnext. As you have found, C++ programs can only expose “Port” variable to other programs, and these cannot be made global.
If you want multiple programs to read the value from a single Out port on your program, then this is possible by simply connecting multiple In port variables to the C++ Out port in the Port List table.
if you want multiple programs to write to an In port on your program ... then this is not possible directly, and probably just as well. Debugging a project like that could get very confusing very quickly.
The solution is to write to the In port on your C++ program from a single Out port on an IEC program. Then, that IEC program can define one or more External variables (if you want), and these will be global variables in the PLC. Then, in that IEC program, you can transfer the value from the global variable(s) to the Out port variable, and thereby to the In port variable on your C++ program.
Note that the the use of External (ie global) variables is generally discouraged, since this can also make debugging quite difficult if global variables are being written from multiple places in the PLC project.
Answer to second question
To copy a string to a BYTE array, you will need to write some IEC code that includes the STRING_TO_BUF function. Then, you can assign a value to the STRING variable in PLCnext Engineer, and this will be copied to the BYTE array.
The documentation for that function should help, but please let us know if there are any questions on this.
- Martin