Clear Filters
Clear Filters

How can I use an event triggered by a COM object to call a method of one of my classes, without having a callback function in the COM object?

3 views (last 30 days)
I use a com server to connect with a measurement device:
ophirApp = actxserver('OphirLMMeasurement.CoLMMeasurement')
Whenever the device is ready for a readout it triggers the event DataReady:
events(ophirApp)
DataReady = void DataReady(int32 hDevice, int32 channel)
When the event is triggered I would like to have a listener which calls a method of a different object since the ophirApp has not built in callback function: Something like:
el = addlistener(ophirApp,'DataReady',@handleEvent);
where @handleEvent is NOT a method of ophirApp
Is there a way to doe so?
  1 Comment
Yuxiao Zhai
Yuxiao Zhai on 15 Mar 2023
Perhaps you could define a new class that has a property to store your phirApp.
The new class also has a method that triggers the DataReady in ophirApp.
You could then create a handle of the method of an instantiation of the new class and pass the handle to addlistener

Sign in to comment.

Accepted Answer

Abhijeet
Abhijeet on 6 Apr 2023
Hi,
To create a listener that calls a method of a different object when the DataReady event is triggered, you can use the "addlistener" function as you mentioned.
Here is an example code snippet to demonstrate this:
% Create an instance of the measurement device
ophirApp = actxserver('OphirLMMeasurement.CoLMMeasurement');
% Define a function handle to the method you want to call when the event is triggered
handleEvent = @myEventHandler;
% Create a listener for the DataReady event
listener = addlistener(ophirApp, 'DataReady', @(src, evt) handleEvent(src, evt, obj));
% obj is the object whose method you want to call when the event is triggered
% Start the measurement
ophirApp.StartAllChannels;
% Wait for the event to be triggered
% When the event is triggered, it will call the handleEvent method of obj
% Clean up by deleting the listener
delete(listener);
Note: The src argument in the handleEvent method will be the ophirApp object that triggered the event, and the evt argument will be an object containing information about the event.
Hope this resolves your query.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!