Python threading for hardware control (COM interface)

Discuss macros to control the hardware functions of your ZEISS microscope
Post Reply
Jacob O'Brien
Posts: 1
Joined: Wed Aug 03, 2022 4:03 pm

Python threading for hardware control (COM interface)

Post by Jacob O'Brien »

Hello,

I'm interfacing with ZEN through the Python COM interface to handle hardware control for a GUI I'm developing. Has anyone successfully enabled multithreading for commands to ZEN through the COM interface?

For instance, my GUI becomes unresponsive while the it waits for focusing to finish (Zen.Devices.Focus.MoveTo(position)). I tried running that command in a thread and I get an error:
"pywintypes.com_error (-2147221008, 'CoInitialize has not been called.', None, None)"

Pleas let me know if you have any suggestions or alternative ways to maintain GUI responsiveness while ZEN commands are executing.
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Python threading for hardware control (COM interface)

Post by CarlZeissMicroscopy3 »

Hello Jacob O'Brien,
multithreading for commands to ZEN through the COM interface
Sorry, but there seems to be some basic misunderstanding.

The 'SimpleInterface' of OAD, which is the basis for e.g. the macro commands and the COM interface, is by design not meant to be used in an multithreading environment.

One of the main features of the SimpleInterface is that commands work sequentially, which makes it easy for 'normal programmers' to set up a macro etc. for a certain workflow.
In other words: Zen.Devices.Focus.MoveTo(position) is waiting for the MoveTo to finish before the next command is executed ‘by philosophy’.

I hope this helps.
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Python threading for hardware control (COM interface)

Post by CarlZeissMicroscopy3 »

Additional note for advanced programmers.

For various reasons the 'Simple Interface' has to run in the 'GUI-Thread of ZEN' and it is not possible to 'get hold of' or 'interact with' the 'GUI-Thread' via COM or macro.
User avatar
Bob F12
Posts: 1
Joined: Tue Jul 11, 2023 7:21 pm

Re: Python threading for hardware control (COM interface)

Post by Bob F12 »

Wow, looks really interesting!
Post Reply