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?

2 次查看(过去 30 天)
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 个评论
Yuxiao Zhai
Yuxiao Zhai 2023-3-15
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

请先登录,再进行评论。

采纳的回答

Abhijeet
Abhijeet 2023-4-6
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by