Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
M Variable Access
#1
How do you access an M variable from a C PLC (user_plcc())?
Reply
#2
(11-10-2010, 03:29 PM)Amir Wrote: How do you access an M variable from a C PLC (user_plcc())?

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");
}

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.
Reply
#3
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 <gplib.h>
#include <RtGpShm.h> // 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;
}

(11-10-2010, 05:33 PM)CharlesP Wrote:
(11-10-2010, 03:29 PM)Amir Wrote: How do you access an M variable from a C PLC (user_plcc())?

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");
}

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.

Reply
#4
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
Reply
#5
In the example below, how is MyPtrVar1 defined?

(11-12-2010, 02:21 PM)KEJR Wrote: 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

Reply
#6
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
Reply
#7
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.

(11-29-2010, 08:52 AM)KEJR Wrote: 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

Reply
#8
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
Reply
#9
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;


(11-29-2010, 11:09 AM)KEJR Wrote: 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

Reply
#10
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.
Reply
#11
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)