CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Thu Jan 23, 2020 11:33 am

All times are UTC+01:00

Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Dec 14, 2011 10:21 pm 

Joined: Wed Dec 14, 2011 6:14 am
Posts: 2
I am looking to build a library that is geared toward code reuse, and has a very simple API. Specifically, some basic data structures with methods using the new OOP capabilities (obviously without dynamic memory capabilities). I am using CoDeSys v3.4 SP3 Patch 2, and using the standard text language.

In C, I would cast an object, variable, etc. to a void pointer and manage the void pointer in the backend of the API. Then cast the returned pointer back to an object from there.
   MyObject o;
   DataStruct d;
   //Do stuff with o *)o);
   //Some time later
   o = (MyObject) d.retrieve();

In C++ I would just use the template library to do something similar
  MyObject o;
  DataStruct<MyObject> d;
  //Do stuff with o;
  o = d.retrieve();

Java obviously has something similar, but I would probably store an Object, and then make my specific object inherit from the Object class. Since it is inherited, it can be managed under the parent class.

My question is, in CoDeSys, what can I do to accomplish this behavior? My original thought was to store pointers to bytes, but that unfortunately tries to convert the object to a byte it seems. I haven't tried it yet, but I thought about trying to do the Java pattern of having an object class and using that data type in the library, and then inheriting from that object to store whatever data I want.

Any help or insight would be greatly appreciated.

PostPosted: Thu Dec 15, 2011 7:42 am 

Joined: Thu Aug 04, 2011 11:16 am
Posts: 87
in CoDeSys DataStructs don'have methods. You have to use function block with method. I think you are looking for a generic approach. Try to use interfaces for that.

Define a interface:
INTERFACE IF_GenericInterface

Create a function block that implements that interface:
FUNCTION_BLOCK FB_ImplementsInterface IMPLEMENTS IF_GenericInterface

Create a second function block with the store method. Input parameter is the interface:
ifInterface : IF_GenericInterface;

Create a second methode retrieve with the return value of the interface:
METHOD PUBLIC Retrieve : IF_GenericInterface

Instead of the two methods, you can also use a property. This hsould be more applicable in your case.

Declaration in user code:
fbImplementsInterface : FB_ImplementsInterface;
fbStoreAndReceive : FB_StoreAndReceive;
ifGenericInterface : IF_GenericInterface;

// Store instance

// Receive instance as interface
ifGenericInterface := fbStoreAndReceive.Retrieve();

The usage of interfaces only makes sense if they have additional properties or methods and you have different objects that implement this interface.

Hope this is helpful for you :idea:

PostPosted: Thu Dec 15, 2011 8:09 am 

Joined: Wed Dec 14, 2011 6:14 am
Posts: 2
Thanks for the reply. Next chance I get, I was going to try out a pattern similar to what you describe. I'll post back when I give it a shot.

Also, the interface would make sense here, as the end user would need the ability to define their own data types/methods/etc, so they could implement the interface with to their own liking.

PostPosted: Fri Mar 31, 2017 11:37 am 

Joined: Tue Feb 14, 2017 9:59 am
Posts: 25

Can you give me an example:
so how can i declare a pointer to function in my library.
and how can i pass a pointer to function to my external library?

thank you :)

Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC+01:00

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited