python
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python [2021/03/26 08:55] – [Via pymmcore] jon | python [2026/06/16 15:15] (current) – add section for pymmcore-plus brandon | ||
|---|---|---|---|
| Line 13: | Line 13: | ||
| We use the pyserial library and Python 3 in-house. | We use the pyserial library and Python 3 in-house. | ||
| + | |||
| + | You can adapt this script to work with Tiger devices, you will need to send the card address before some serial commands. For example, with a MS2000 you would send '' | ||
| This script works best when the baud rate is set to 115200. | This script works best when the baud rate is set to 115200. | ||
| Line 18: | Line 20: | ||
| --> Here is an example script:# | --> Here is an example script:# | ||
| - | **Last tested on Windows | + | **Last tested on Windows |
| This class manages the serial connection. | This class manages the serial connection. | ||
| - | <code python> | + | <code python serialport.py> |
| - | # serialport.py | + | |
| from serial import Serial | from serial import Serial | ||
| from serial import SerialException | from serial import SerialException | ||
| Line 30: | Line 31: | ||
| from serial import STOPBITS_ONE | from serial import STOPBITS_ONE | ||
| from serial.tools import list_ports | from serial.tools import list_ports | ||
| - | |||
| - | from typing import List | ||
| class SerialPort: | class SerialPort: | ||
| Line 39: | Line 38: | ||
| """ | """ | ||
| - | def __init__(self, | + | def __init__(self, |
| self.serial_port = Serial() | self.serial_port = Serial() | ||
| self.com_port = com_port | self.com_port = com_port | ||
| Line 48: | Line 47: | ||
| @staticmethod | @staticmethod | ||
| - | def scan_ports() -> List[str]: | + | def scan_ports() -> list[str]: |
| """ | """ | ||
| com_ports = [port.device for port in list_ports.comports()] | com_ports = [port.device for port in list_ports.comports()] | ||
| Line 54: | Line 53: | ||
| return com_ports | return com_ports | ||
| - | def connect_to_serial(self, | + | def connect_to_serial(self, |
| """ | """ | ||
| # serial port settings | # serial port settings | ||
| Line 94: | Line 93: | ||
| """ | """ | ||
| # short circuits if serial port is None | # short circuits if serial port is None | ||
| - | | + | |
| - | return True | + | |
| - | else: | + | def report_to_console(self, |
| - | return False | + | |
| - | + | ||
| - | def report_to_console(self, | + | |
| """ | """ | ||
| # useful if we want to output data to something other than the console (ui element etc) | # useful if we want to output data to something other than the console (ui element etc) | ||
| Line 105: | Line 101: | ||
| print(message) | print(message) | ||
| - | def send_command(self, | + | def send_command(self, |
| """ | """ | ||
| # always reset the buffers before a new command is sent | # always reset the buffers before a new command is sent | ||
| Line 126: | Line 122: | ||
| --> The MS2000 class is a subclass of SerialPort and adds input validation to the constructor.# | --> The MS2000 class is a subclass of SerialPort and adds input validation to the constructor.# | ||
| - | <code python> | + | <code python ms2k.py> |
| - | # ms2k.py | + | |
| from serialport import SerialPort | from serialport import SerialPort | ||
| Line 146: | Line 141: | ||
| BAUD_RATES = [9600, 19200, 28800, 115200] | BAUD_RATES = [9600, 19200, 28800, 115200] | ||
| - | def __init__(self, | + | def __init__(self, |
| super().__init__(com_port, | super().__init__(com_port, | ||
| # validate baud_rate input | # validate baud_rate input | ||
| Line 158: | Line 153: | ||
| # ------------------------------ # | # ------------------------------ # | ||
| - | def moverel(self, | + | def moverel(self, |
| """ | """ | ||
| - | self.send_command(f" | + | self.send_command(f" |
| self.read_response() | self.read_response() | ||
| - | def moverel_axis(self, | + | def moverel_axis(self, |
| """ | """ | ||
| - | self.send_command(f" | + | self.send_command(f" |
| self.read_response() | self.read_response() | ||
| - | def move(self, x:int=0, y:int=0, z:int=0) -> None: | + | def move(self, x: int=0, y: int=0, z: int=0) -> None: |
| """ | """ | ||
| - | self.send_command(f" | + | self.send_command(f" |
| self.read_response() | self.read_response() | ||
| - | def move_axis(self, | + | def move_axis(self, |
| """ | """ | ||
| - | self.send_command(f" | + | self.send_command(f" |
| self.read_response() | self.read_response() | ||
| - | def set_max_speed(self, | + | def set_max_speed(self, |
| """ | """ | ||
| - | self.send_command(f" | + | self.send_command(f" |
| self.read_response() | self.read_response() | ||
| + | def get_position(self, | ||
| + | """ | ||
| + | self.send_command(f" | ||
| + | response = self.read_response() | ||
| + | return int(response.split(" | ||
| + | |||
| + | def get_position_um(self, | ||
| + | """ | ||
| + | self.send_command(f" | ||
| + | response = self.read_response() | ||
| + | return float(response.split(" | ||
| + | | ||
| # ------------------------------ # | # ------------------------------ # | ||
| # MS2000 Utility Functions | # MS2000 Utility Functions | ||
| # ------------------------------ # | # ------------------------------ # | ||
| - | def is_axis_busy(self, | + | def is_axis_busy(self, |
| """ | """ | ||
| self.send_command(f" | self.send_command(f" | ||
| - | | + | |
| - | if "B" | + | |
| - | return True | + | def is_device_busy(self) -> bool: |
| - | | + | |
| - | return | + | |
| + | return | ||
| - | def wait_for_device(self, | + | def wait_for_device(self, |
| - | """ | + | """ |
| - | # does not report serial communications by default | + | |
| if not report: | if not report: | ||
| print(" | print(" | ||
| temp = self.report | temp = self.report | ||
| self.report = report | self.report = report | ||
| - | | + | |
| - | while x_busy or y_busy: | + | while busy: |
| - | | + | |
| - | y_busy | + | self.report = temp |
| - | self.report = temp | + | |
| </ | </ | ||
| <-- | <-- | ||
| Line 213: | Line 219: | ||
| -->And using the class in a script:# | -->And using the class in a script:# | ||
| - | <code python> | + | <code python main.py> |
| - | # main.py | + | |
| from ms2k import MS2000 | from ms2k import MS2000 | ||
| Line 247: | Line 252: | ||
| - | Note: Make sure you enter the correct COM port and baud rate in the constructor for the MS2000 class. | + | **Note:** Make sure you enter the correct COM port and baud rate in the constructor for the MS2000 class. |
| Line 256: | Line 261: | ||
| Micro-Manager has excellent support for ASI devices built via " | Micro-Manager has excellent support for ASI devices built via " | ||
| + | To run the example below, you will need to make a configuration file using the '' | ||
| + | |||
| + | -->An example script to control an XYStage | ||
| + | <code python> | ||
| + | import pymmcore | ||
| + | import os | ||
| + | |||
| + | def main(): | ||
| + | hardware_cfg = " | ||
| + | mm_directory = " | ||
| + | |||
| + | version_info = pymmcore.CMMCore().getAPIVersionInfo() | ||
| + | print(version_info) | ||
| + | |||
| + | mmc = pymmcore.CMMCore() | ||
| + | print(mmc.getVersionInfo()) | ||
| + | |||
| + | # load the device adapters | ||
| + | mmc.setDeviceAdapterSearchPaths([mm_directory]) | ||
| + | mmc.loadSystemConfiguration(os.path.join(mm_directory, | ||
| + | |||
| + | # get the stage device name | ||
| + | xy_stage = mmc.getXYStageDevice() | ||
| + | print(f" | ||
| + | |||
| + | # move the xy stage | ||
| + | mmc.setRelativeXYPosition(10000, | ||
| + | mmc.waitForDevice(xy_stage) | ||
| + | |||
| + | mmc.setRelativeXYPosition(0, | ||
| + | mmc.waitForDevice(xy_stage) | ||
| + | |||
| + | mmc.setRelativeXYPosition(-10000, | ||
| + | mmc.waitForDevice(xy_stage) | ||
| + | |||
| + | mmc.setRelativeXYPosition(0, | ||
| + | mmc.waitForDevice(xy_stage) | ||
| + | |||
| + | if __name__ == " | ||
| + | main() | ||
| + | </ | ||
| + | <-- | ||
| + | |||
| + | ==== Via pymmcore-plus ==== | ||
| + | |||
| + | See the [[https:// | ||
| + | This library wraps pymmcore and adds additional features like an acquisition engine. This gives you access to the Micro-Manager ecosystem while remaining in a pure Python environment. | ||
| - | ==== Via Pycro-Manager | + | ==== Via pycro-manager |
| - | See [[https://pycro-manager.readthedocs.io/ | + | See the [[https://github.com/ |
| - | Micro-Manager which has excellent support for ASI devices built in. Using Pycro-Manager | + | Micro-Manager which has excellent support for ASI devices built in. Using pycro-manager |
| + | This requires you to run a Micro-Manager instance in the background and connect to it through a socket. There is also a headless mode where you don't have to open the ui. | ||
| {{tag> python serial}} | {{tag> python serial}} | ||
Address: 29391 W Enid Rd. Eugene, OR 97402, USA | Phone: +1 (541) 461-8181
python.1616763326.txt.gz · Last modified: (external edit)
