Defining c style type using typedef?

Discussion about recent product features & solutions!
13 posts • Page 1 of 2
Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Defining c style type using typedef?

Post by Smiffy »

In 'c' you can define your own data types using the "typedef" keyword. Is there an equivalent in OA scripting?
Or are the types defined in the para module? If so, how do you use these types with the scrpting to make and auto variable of that type.
E.g. MyDataType : MDT;

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Defining c style type using typedef?

Post by mkoller »

In CTRL scripting you can define your own class/struct/enum datatypes, very similar to C++
A typedef as in C is not available.
A Datapoint Type you define with the para module has nothing to do with a datatype in the scripting language - although
you can create your own class in CTRL which is designed to work with a define Datapoint Type.

Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Re: Defining c style type using typedef?

Post by Smiffy »

Ok so what is the syntax for defining types for use in scripting language then?
Could you give an example please?

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Defining c style type using typedef?

Post by mkoller »

Check the help pages of 3.15 "Object Oriented Scripting (CTRL++)"
Or in the help reader the url qthelp://wincc_oa/doc/Control_Grundlagen/Control_Grundlagen-47.htm

Other short example here:

Note: We suggest that classes be placed in separate files per class and stored under scripts/libs/classes
e.g. here as ValveDPE.ctl
To use such a class definition in another script, use e.g.

#uses "classes/ValveDPE"

the class itself could be:

Code: Select all

class ValveDPE
{
  public ValveDPE(string dpe = "unknown")    // constructor
  {
    writeDPE = dpe + ":_original.._value";
    readDPE = dpe + ":_online.._value";
  }
  public open()
  {
    dpSet(writeDPE, TRUE);
  }
  public close()
  {
    dpSet(writeDPE, FALSE);
  }
  public bool getState()
  {
    bool state;

    dpGet(readDPE, state);
    return(state);
  }

  protected string writeDPE;
  protected string readDPE;
};

Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Re: Defining c style type using typedef?

Post by Smiffy »

Turns out that you can define structure types.
You just do not use the typdef keyword.
You define the srtructure type outside any functions (usually at the top of the script) thus:-
struct myStructDef
{
int i;
float f;
}

And is used thus:-
main()
{
MyStructDef MyStruct;
MyStruct.i = 123;
MyStruct.f = 123.45;
}

So the struct definition is actually defining a type as per typedef in c, but just without the need for the typedef keyword.

It would have been nice is this was explained in the help files. There is no mention of structure types or how to define them.
The next hurdle is to try and find out how to define an array of structures.
Another frustrating exercide awaits I suspect.

ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

Re: Defining c style type using typedef?

Post by ozangor »

Structs are almost identical with classes. The only difference is that the default access modifier for structs is "public" whereas for classes it is "private". Both can have props and methods.

So:

Code: Select all

class MyClass
{
  public int i; //public
  int j; //private
  
  string toString(){} //private
};

struct MyStruct
{
  int i; //public
  private int j; //private

  string toString(){} //public
};
And for your next question of defining arrays of anything you can just use "dyn_anytype". Following should work fine:

Code: Select all

dyn_anytype MyClassList;
MyClass mc;
dynAppend(MyClassList, mc);

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Defining c style type using typedef?

Post by mkoller »

See my previous post. I mentioned the help page qthelp://wincc_oa/doc/Control_Grundlagen/Control_Grundlagen-47.htm
and on that page you find an explanation including example code about class and struct

Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Re: Defining c style type using typedef?

Post by Smiffy »

Thanks for the replies guys. I eventually settled for this:-

struct Message
{
int MessNum;
int MessPri;
bit32 MessCfg;
time EntryTime;
bool EntryValid;
time ExitTime;
bool ExitValid;
time AckTime;
bool AckValid;
bool AwaitingAck;
bit32 HMIAct;
int NextItem;
bool DelQueueItem;
};

dyn_anytype MsgList;

main()
{
Message MsgData;
MsgList = makeDynAnytype(MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData, MsgData, MsgData, MsgData, MsgData,
MsgData);
}

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Defining c style type using typedef?

Post by mkoller »

Or you'd do it like this:

for (int i = 1; i

Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Re: Defining c style type using typedef?

Post by Smiffy »

I initially tried it the way you suggested. At first glance it works and the pointers contain the correct values and can be dereferenced correctly.
However, when you use the dynlen() function on the dynamic type, it returns the incorrect value (0 or 1, I can recall but not the actual size of the definition).
That is why I used the makeDynAny() as this defines the type fully and allows dynlen() to return the correct value.

13 posts • Page 1 of 2