This Python package provides control of the Nikon Ti2-E microscope with support for all major platforms. It does not rely on the Nikon SDK but instead uses the reverse-engineered USB protocol of the microscope directly through PyUSB.
$ pip install nikon
- On Windows, the driver for the Nikon Ti2-E must be set to WinUSB or another driver supported by libusb, if you are using libusb as a backend for PyUSB. After changing the driver, NIS Elements won't be able to connect to the microscope until the driver is uninstalled and the default driver is re-installed. The driver installation procedure can be automated with libwdi.
- On macOS (and Linux?), a firmware bug causes
get_event()
and other status-related functions to return with a minimal delay of 4.096 seconds between calls. Furthermore, the returned data may not be up-to-date as events are queued by the firmware.
import asyncio
from nikon import MicroscopeDevice
async def main():
devices = MicroscopeDevice.list()
device = next(devices)
await device.set_x(-31790)
await device.set_y(28120)
await device.set_condenser(3)
await device.set_light(True)
async for event in device:
match event:
case StatusEvent():
event.condenser
event.dia
event.filter
event.light
event.objective
event.optical_path
event.x
event.y
event.z
asyncio.run(main())
Every command first tries to acquire a lock common to the device, therefore starting two commands at the same time will still cause them to run sequentially.
If you do not wish to use functions asynchronously, you can wrap all calls with asyncio.run()
.
asyncio.run(device.set_x(-31790))
asyncio.run(device.set_light(True))