Join us today!

Notifications
Clear all

interface method with different inputs and quantity according to the FB class where it is implemented, with Abstract FB, or as it would be...??

3 Posts
2 Users
2 Reactions
780 Views
runtimevictor
Posts: 156
Topic starter
(@runtimevictor)
Estimable Member
Joined: 2 years ago

 

TwinCAT Method differents inputs

Hello,

I have a method declared in an interface, it can have different inputs and number and quantity depending on the FB class where the interface is implemented, as it would be with Abstract FB, can someone give me a small example?

Best regards, Víctor.

Reply
2 Replies
2 Replies
runtimevictor
(@runtimevictor)
Joined: 2 years ago

Estimable Member
Posts: 156

Hello, 

This was just answered on the linkedin forum:

"Unfortunately overloading is not allowed in twincat 3. Not for methods, properties nor variables themselves.

it sucks I know, but you gotta play with the cards you got..

So you need to do something around the limitations."

Reply
(@gised_link)
Joined: 2 years ago

New Member
Posts: 1

@runtimevictor

My post on codesys forum about how to overload a sort function may interest you:

https://forge.codesys.com/forge/talk/Runtime/thread/6ae1b1ff98/?page=1&limit=25#f6da

It is not exactly what you are trying to do, but it is an approach.

For an Adition function with undifined parameter inputs, I will do it like this:

  • Create a function per type you want to return (DINT and REAL)
  • Each function will have 3 arguments
    • element_count: the number of element you want to add
    • pData : pointer to array of byte containing the element you want to add
    • element_size : the byte count of each of your element
  • Perform a loop that:
    • cast an element of the pData array to the correct type
    • add the cast element with the result
    • increment the byte number depending on element_size

Unfortunately it is not possible to have an undifined number of parameter in ST. The compiler is not capable to determine it itself like other language (in C we have the '...' argument with VA_List type). So you have to perform it yourself.

 

This is how it is done in C:

https://en.cppreference.com/w/c/variadic/va_arg

Reply
Share: