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?

1 visualización (últimos 30 días)
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:
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 comentario
Yuxiao Zhai
Yuxiao Zhai el 15 de Mzo. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Abhijeet el 6 de Abr. de 2023
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
% 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
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.

Más respuestas (0)


Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by