Ctrl Extension call startFunc

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
20 posts • Page 1 of 2
Merikarhu
Posts:13
Joined: Wed Jun 07, 2017 3:19 pm

Ctrl Extension call startFunc

Post by Merikarhu »

Hi,

im developing ctrl extension with c++ and currently im registering callback functions from ctrl script via extension and it works fine. Also calling startFunc straight away after function extension event i can call pre-defined function from ctrl script.

But i need to call that callback function outside of that extension event. How to do that ? This is what i have tried so far:

notes: m_param is defined as array which holds param from callback registration event

Code: Select all

BaseExternHdl::ExecuteParamRec *m_param[25000];

case F_Subscribe:
	{
		TextVar datapointName;
		TextVar cbFunc;
		IntegerVar result;

		datapointName = *(param.args->getFirst()->evaluate(param.thread));
		cbFunc = *(param.args->getNext()->evaluate(param.thread));
		result = 1;

		BaseExternHdl::ExecuteParamRec rc = BaseExternHdl::ExecuteParamRec(param);

		m_topicName[TopicCount] = string(datapointName);
		m_param[TopicCount] = &rc;
		m_callbackFunction[TopicCount] = cbFunc;

		TopicCount++;
		return &result;
	}

void ActiveMQExternHdl::funcCall( string functionName)
{

	CtrlScript *script = m_param[0]->thread->getScript();
	Variable *args;

	//convert string to char arr
	char arr[1024];
	strncpy(arr, functionName.c_str(), sizeof(arr));
	arr[sizeof(arr) - 1] = 0;

	// convert char arr to PVSS charstring
	CharString cbfunc = CharString(arr);
	cout

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

Re: Ctrl Extension call startFunc

Post by mkoller »

Basically the concept is correct.
The main question is just: what triggers the callback ?

For your current code:
You can not store the ExecuteParamRec since the thread pointer will at some point be invalid (when the thread stops) and in fact you don't need the thread
pointer as you are going to create a new one. You just need the CtrlScript pointer.
Also, m_param[TopicCount] = &rc; stores a pointer to a local variable, so the pointer is already invalid when the function is left.
Same here: return &result;
You MUST use a static IntegerVar here.
Also, don't use string class. Simply stick to the WinCC_OA CharString class which you already use here (which is also member of TextVar).
And your "args" pointer is still invalid.

Merikarhu
Posts:13
Joined: Wed Jun 07, 2017 3:19 pm

Re: Ctrl Extension call startFunc

Post by Merikarhu »

Thank you for reply, m_param is on top of class file so it should be public ! :) Sorry for that.

I got ActiveMQ topic listener which calls this function when there is new message on topic - that works until its time to call callback function from Ctrl script.

How i introduce public array of ( i assume i need pointer for each callback function.. ) CtrlScript pointers right way ?
And thank you for pointing details for correct coding style.. im not super familiar with c++

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

Re: Ctrl Extension call startFunc

Post by mkoller »

So I assume you're already having a class derived from CtrlModule which implements the listener, right ?
If so, this class has already the CtrlScript pointer, so no need to store this again.
If not, check out this posting: https://portal.etm.at/index.php?option= ... =1040#1042
Then what you need to store is really just a CharString which holds the callback functions name.

vogler
Posts:122
Joined: Thu Oct 28, 2010 8:32 am

Re: Ctrl Extension call startFunc

Post by vogler »

I am curious about the connectivity to ActiveMQ - what is your use case with ActiveMQ?

Merikarhu
Posts:13
Joined: Wed Jun 07, 2017 3:19 pm

Re: Ctrl Extension call startFunc

Post by Merikarhu »

ActiveMQ is message broker, thats all im able to tell you, sorry.

vogler
Posts:122
Joined: Thu Oct 28, 2010 8:32 am

Re: Ctrl Extension call startFunc

Post by vogler »

recently I have implemented a simple driver for Apache Kafka, that's why I am curious in using ActiveMQ... and why you prefer to implement a Ctrl-Extension instead of using an API Manager or Driver...
http://www.rocworks.at/wordpress/?p=764

Merikarhu
Posts:13
Joined: Wed Jun 07, 2017 3:19 pm

Re: Ctrl Extension call startFunc

Post by Merikarhu »

We already have API manager, for specific reasons we want extension too. ActiveMQ works fine so far :)

Merikarhu
Posts:13
Joined: Wed Jun 07, 2017 3:19 pm

Re: Ctrl Extension call startFunc

Post by Merikarhu »

Martin Koller wrote:
So I assume you're already having a class derived from CtrlModule which implements the listener, right ?
If so, this class has already the CtrlScript pointer, so no need to store this again.
If not, check out this posting: https://portal.etm.at/index.php?option= ... =1040#1042
Then what you need to store is really just a CharString which holds the callback functions name.
Currently all code is in template class as this is first version of concept and that default class doesnt have CtrlModule. I checked the TCP example but it doesnt seems to work after i tried to do similiar.

I dont get it why i cannot store CtrlScript pointer and just use it when i want to call user defined function from OA, this is what i would like to do :

Code: Select all

// this is "public" var
CtrlScript *m_script;

// imaginary "main" where this function is called
void main()
{
funcCall("Calculate");
}


const Variable *ExternHdl::execute(ExecuteParamRec &param)
{
	enum
	{
		F_init = 0,
	};

switch (param.funcNum)
	{

	case F_init :
	{
           m_script = param.thread->getScript();

          // if here i call func it works but not after script is stored and used later
          // RecVar vars;
         //  m_script->startFunc("nameofcuntion", &vars);
        }
}
}


void ExternHdl::funcCall(const CharString &functionName)
{
	RecVar vars;

m_script->startFunc(functionName, &vars);
OR
CtrlFunc *func = Controller::thisPtr->findFunc(functionName, m_script);
m_script->startThread(thread, func, &vars);
}

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

Re: Ctrl Extension call startFunc

Post by mkoller »

"does not work" is usually not enough to describe a problem.
Please be more specific. What does not work ? Does it crash ? Does it show an error ?
Is this a script you run in a CTRL manager or the UI manager ?

You can store the CtrlScript pointer if you KNOW that it is still valid when you need it.
But keep in mind that the UI manager starts a lot of scripts and that these scripts are also stopped (deleted) e.g. when the panel is closed.

20 posts • Page 1 of 2