Jump to content
OMRON Forums

M Variable Access


Amir

Recommended Posts

  • Replies 10
  • Created
  • Last Reply

Top Posters In This Topic

[quote='Amir' pid='824' dateline='1289428140'] How do you access an M variable from a C PLC (user_plcc())? [/quote] One way is to use the function GetPmacVar, whose prototype is as such: int GetPmacVar (char * pinstr, double * pdata) For example, to read M400, one could use this code snippet inside the CPLC: [code] int ErrorCode; double M_Var_Result; ErrorCode = GetPmacVar("M400",&M_Var_Result); if(ErrorCode != 0) { printf("\nThere was an error in reading the M-Variable.\n"); } [/code] After running that, the value in M400 should be stored in your C variable "M_Var_Result". Note that in this example, I used a string literal to input "M400" into the GetPmacVar function. One could also use the sprintf function to dynamically create the string to pass into GetPmacVar if needed.
Link to comment
Share on other sites

Another way: In your "global definitions.pmh" you have your M-ptr definition ptr MyPtrVar1->*; In your "C" program you would have something like #include #include // Global Rt/Gp Shared memory pointers //------------------------------------------------------------- // The following is a projpp created file from the User defines //------------------------------------------------------------- #include "../../Include/pp_proj.h" int main(void) { double MyMvar; InitLibrary(); //Put your code here MyMvar = GetPtrVar(MyPtrVar1); CloseLibrary(); return 0; } [quote='CharlesP' pid='826' dateline='1289435593'] [quote='Amir' pid='824' dateline='1289428140'] How do you access an M variable from a C PLC (user_plcc())? [/quote] One way is to use the function GetPmacVar, whose prototype is as such: int GetPmacVar (char * pinstr, double * pdata) For example, to read M400, one could use this code snippet inside the CPLC: [code] int ErrorCode; double M_Var_Result; ErrorCode = GetPmacVar("M400",&M_Var_Result); if(ErrorCode != 0) { printf("\nThere was an error in reading the M-Variable.\n"); } [/code] After running that, the value in M400 should be stored in your C variable "M_Var_Result". Note that in this example, I used a string literal to input "M400" into the GetPmacVar function. One could also use the sprintf function to dynamically create the string to pass into GetPmacVar if needed. [/quote]
Link to comment
Share on other sites

The global vars are nice, you don't have to type in hard M or P vars. I just want to point out that you don't have to declare another double in your code. You can do this kind of thing: MyScaledResult = (200* GetPtrVar(MyPtrVar1)) / 3; it makes a difference if you are using alot of Ptr vars or global vars. I personally don't like the GetPMACVar() function because of its possibility of throwing a run time error if you misspell the string. It is considerably slower too. Something to note if you are doing alot of global var manipulation. KEJR
Link to comment
Share on other sites

  • 3 weeks later...
In the example below, how is MyPtrVar1 defined? [quote='KEJR' pid='835' dateline='1289596869'] The global vars are nice, you don't have to type in hard M or P vars. I just want to point out that you don't have to declare another double in your code. You can do this kind of thing: MyScaledResult = (200* GetPtrVar(MyPtrVar1)) / 3; it makes a difference if you are using alot of Ptr vars or global vars. I personally don't like the GetPMACVar() function because of its possibility of throwing a run time error if you misspell the string. It is considerably slower too. Something to note if you are doing alot of global var manipulation. KEJR [/quote]
Link to comment
Share on other sites

In my example the MyPtrVar1 is defined in the delta tau IDE under global definitions. (PMAC Script Language / Global Includes / global definitions.pmh) What happens is that the IDE auto assigns unallocated P var indexes to your text name. The cool thing is that it is useable in the script programs, terminal, and C programs. The way they get brought into your C program is with the following include: #include "../../Include/pp_proj.h" There are other posts on here regarding global variables I believe, and there are some example C app projects installed with the IDE I think. KEJR
Link to comment
Share on other sites

In my case, the IDE has not auto assigned anything in the pp_proj.h file. Can you please provide one example of how MyPtrVar1 would be defined. [quote='KEJR' pid='862' dateline='1291045941'] In my example the MyPtrVar1 is defined in the delta tau IDE under global definitions. (PMAC Script Language / Global Includes / global definitions.pmh) What happens is that the IDE auto assigns unallocated P var indexes to your text name. The cool thing is that it is useable in the script programs, terminal, and C programs. The way they get brought into your C program is with the following include: #include "../../Include/pp_proj.h" There are other posts on here regarding global variables I believe, and there are some example C app projects installed with the IDE I think. KEJR [/quote]
Link to comment
Share on other sites

See edlay's response in a previous thread: "In your "global definitions.pmh" you have your M-ptr definition ptr MyPtrVar1->*; " Of course to make it do anything of much use you would want to map the M variable to something useful. KEJR
Link to comment
Share on other sites

Okay, the part that was missing, and not so obvious is that in the PMAC script language portion in the file plc1.plc you will have the following statement: #define MyPtrVar1 M1; #define MyPtrVar2 M2; and after a build, the file pp_proj.h is automatically generated and will produce something like: #define MyPtrVar1 1 #define MyPtrVar2 2 and then you can use MyPtrVar1 or MyPtrVar2 as an argument in GetPtrVar function from a C program with the added requirement that you include pp_proj.h. The bad part is that the compiler does not issue a warning if you accidentally reuse a definition like this: #define MyPtrVar1 M1; #define MyPtrVar1 M2; [quote='KEJR' pid='866' dateline='1291054158'] See edlay's response in a previous thread: "In your "global definitions.pmh" you have your M-ptr definition ptr MyPtrVar1->*; " Of course to make it do anything of much use you would want to map the M variable to something useful. KEJR [/quote]
Link to comment
Share on other sites

The key point is that there are two ways of assigning user names to variables. One is the variable declaration, which auto-assigns an underlying variable to your name. For an M-variable, this would look like: ptr MyPtrVar->{definition}; For a P-variable, this would look like: global MyGlobalVar1; You can do "manual assignment" with a #define directive, which is really just a text substitution: e.g. #define MyPtrVar1 M1 The auto-assignment method is strongly recommended in virtually all cases. It is easier and more robust. Power PMAC and the IDE keep track of the underlying variables used so you don't have to. The project manager's downloader will automatically catch any duplicate definitions. We recommend that your auto-assigning variable declarations go in the file "global definitions.pmh" in the "Global Includes" folder of the Project Manager.
Link to comment
Share on other sites

I agree with what Curt said. Just to elaborate a little on my past posts, If you are trying to figure out what is actually going on in your C code add a test global to your "global definitions.pmh" file: global footestglobal; global foo2; Now hit build, and then open "../../Include/pp_proj.h" in a text editor. you will see something like this: #define footestglobal 8192 #define foo2 8193 This is because the IDE by default starts allocating globals at 8192. This is what is automatically generated by the IDE and it happens at compile time. Now in your C code you are including "../../Include/pp_proj.h" so now your function call to get global variable will evaluate to something like this after the C preprocessor : MyScaledResult = (200* GetGlobalVar(8192)) / 3; Make no mistake, however, don't refer to your variable in any shape by P8192, or 8192 directly. Refer to it always by "footestglobal", or whatever name you give it. The 8192 is auto generated, so forget its there, it just is a way to give the C compiler the name in a C friendly way (#define directive in an include file). KEJR
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

×
×
  • Create New...