Jump to content
OMRON Forums

Global Struct


shansen
 Share

Recommended Posts

Hey guys,

 

I need to declare a global structure so that it is accessible to background CPLCs and User Written phase code (just like how the Motor struct works), but I am not sure how to go about initializing the struct on powerup.

 

I can define the struct type in usrcode.h, but where can I initialize a global struct?

 

Example:

 

In usrcode.h

typedef struct MyStruct
{
int A;
...
};

 

In some other piece of code:

struct MyStruct *mine;

 

In usrcode.c

...
struct MyStruct *m = (*MyStruct)somepointer->mine
m.A = 100;
...

 

Any ideas?

 

-Steven

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I was thinking about something like this a while back. My experiment worked as far as I took it and may help you...

 

I assume the only 'global' memory you have access to as memory is the 'user shared memory' area. So in "C Language/Include/mystruct.h" I define

 

typedef struct
{
int A;
float B;
char C[6];
} MyStruct;

 

Then I can decide to reserve an area of user shared memory for this struct, say from USHM[1000] onwards (the amount depends on the size of your stucture). Then in one C app I have

 


//--------------------------------------------------------------------------------
#include 	// Global Gp Shared memory pointer
//-------------------------------------------------------------
// The following is a projpp created file from the User defines
//-------------------------------------------------------------
#include "../../Include/pp_proj.h"
#include "../../Include/mystruct.h"

int main(void) 
{
struct timespec mSecSleeper = {0};	// Initialize time structure
mSecSleeper.tv_nsec = 1e6;			// 1e6 nanoseconds, which equals 1 msec

InitLibrary();

MyStruct *mine = (MyStruct *)(&((int *)pushm)[1000]);

while (1)
{
	mine->B = 3 * mine->A;

	mine->C[0] = 0x48;
	mine->C[1] = 0x65;
	mine->C[2] = 0x6C;
	mine->C[3] = 0x6C;
	mine->C[4] = 0x6F;
	mine->C[5] = 0x00;

	nanosleep(&mSecSleeper, NULL);
}

CloseLibrary();
return 0;
}

 

which gets the pointer to my reserved area as a pointer to my structure, then manipulates the data every millisecond. In another C app I have

 


//--------------------------------------------------------------------------------
#include 	// Global Gp Shared memory pointer
//-------------------------------------------------------------
// The following is a projpp created file from the User defines
//-------------------------------------------------------------
#include "../../Include/pp_proj.h"
#include "../../Include/mystruct.h"

int main(void) 
{
struct timespec mSecSleeper = {0};	// Initialize time structure
mSecSleeper.tv_nsec = 1e6;			// 1e6 nanoseconds, which equals 1 msec

InitLibrary();

MyStruct *mine = (MyStruct *)(&((int *)pushm)[1000]);

while (1)
{
	// Input
	mine->A = pshm->P[0];
	// Debug output
	pshm->P[1] = mine->B;
	if (pshm->P[1] == 30)
	  Send(2, mine->C);

	nanosleep(&mSecSleeper, NULL);
}

CloseLibrary();
return 0;
}

 

This allows you to put a number in P0 which then goes into the structure. The other app manipulates the number and this app allows us to view the effect in P1 in a watch. I also output a message to the unsolicited view which is stored in the structure, if P1 equals 30 (set P0 to 10). This shows some of the user shared memory being divided into a structure (I have not really examined byte boundary issues or anything more involved than this).

 

I hope this is useful (and correct). Basically the definition goes in a .h file included where you want to reference it, and the instance of the structure has to reside somewhere in user shared memory so everything can get at it.

 

Obviously you have to remember you have reserved some ushm and not use it again somewhere else.

 

Dave

 

Link to comment
Share on other sites

Dave,

 

That is almost exactly what I was looking for. The only thing I don't like about that solution is that I have no control over who writes to that memory space. I'm going to be storing "mission-critical" variables there, and if they get overwritten the best case scenario results in something blowing up.

 

I definitely think you put me on the right track though, thanks!

 

-Steven

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share


×
×
  • Create New...