User Tools

Site Tools


python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python [2021/05/14 19:26] – card address note for tiger brandonpython [2026/06/16 15:15] (current) – add section for pymmcore-plus brandon
Line 20: Line 20:
 --> Here is an example script:# --> Here is an example script:#
  
-**Last tested on Windows 10 64-Bit, Python 3.9.4 64-Bit, and pyserial 3.5.**+**Last tested on Windows 11 64-Bit, Python 3.14.6 64-Bit, and pyserial 3.5.**
  
 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 39: Line 38:
     """     """
  
-    def __init__(self, com_port: str, baud_rate: int, report: str=True):+    def __init__(self, com_port: str, baud_rate: int, report: bool = True):
         self.serial_port = Serial()         self.serial_port = Serial()
         self.com_port = com_port         self.com_port = com_port
Line 54: Line 53:
         return com_ports         return com_ports
    
-    def connect_to_serial(self, rx_size: int=12800, tx_size: int=12800, read_timeout: int=1, write_timeout: int=1) -> None:+    def connect_to_serial(self, rx_size: int = 12800, tx_size: int = 12800, read_timeout: int = 1, write_timeout: int = 1) -> None:
         """Connect to the serial port."""         """Connect to the serial port."""
         # serial port settings         # serial port settings
Line 94: Line 93:
         """Returns True if the serial port exists and is open."""         """Returns True if the serial port exists and is open."""
         # short circuits if serial port is None         # short circuits if serial port is None
-        if self.serial_port and self.serial_port.is_open: +        return self.serial_port and self.serial_port.is_open 
-            return True +    
-        else: +
-            return False +
     def report_to_console(self, message: str) -> None:     def report_to_console(self, message: str) -> None:
         """Print message to the output device, usually the console."""         """Print message to the output device, usually the console."""
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 160: Line 155:
     def moverel(self, x: int=0, y: int=0, z: int=0) -> None:     def moverel(self, x: int=0, y: int=0, z: int=0) -> None:
         """Move the stage with a relative move."""         """Move the stage with a relative move."""
-        self.send_command(f"MOVREL X={x} Y={y} Z={z}\r")+        self.send_command(f"R X={x} Y={y} Z={z}\r")
         self.read_response()         self.read_response()
    
     def moverel_axis(self, axis: str, distance: int) -> None:     def moverel_axis(self, axis: str, distance: int) -> None:
         """Move the stage with a relative move."""         """Move the stage with a relative move."""
-        self.send_command(f"MOVREL {axis}={distance}\r")+        self.send_command(f"R {axis}={distance}\r")
         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:
         """Move the stage with an absolute move."""         """Move the stage with an absolute move."""
-        self.send_command(f"MOVE X={x} Y={y} Z={z}\r")+        self.send_command(f"M X={x} Y={y} Z={z}\r")
         self.read_response()         self.read_response()
    
     def move_axis(self, axis: str, distance: int) -> None:     def move_axis(self, axis: str, distance: int) -> None:
         """Move the stage with an absolute move."""         """Move the stage with an absolute move."""
-        self.send_command(f"MOVE {axis}={distance}\r")+        self.send_command(f"M {axis}={distance}\r")
         self.read_response()         self.read_response()
    
     def set_max_speed(self, axis: str, speed:int) -> None:     def set_max_speed(self, axis: str, speed:int) -> None:
         """Set the speed on a specific axis. Speed is in mm/s."""         """Set the speed on a specific axis. Speed is in mm/s."""
-        self.send_command(f"SPEED {axis}={speed}\r")+        self.send_command(f"S {axis}={speed}\r")
         self.read_response()         self.read_response()
    
 +    def get_position(self, axis: str) -> int:
 +        """Return the position of the stage in ASI units (tenths of microns)."""
 +        self.send_command(f"W {axis}\r")
 +        response = self.read_response()
 +        return int(response.split(" ")[1])
 +
 +    def get_position_um(self, axis: str) -> float:
 +        """Return the position of the stage in microns."""
 +        self.send_command(f"W {axis}\r")
 +        response = self.read_response()
 +        return float(response.split(" ")[1])/10.0
 +    
     # ------------------------------ #     # ------------------------------ #
     #    MS2000 Utility Functions    #     #    MS2000 Utility Functions    #
Line 190: Line 197:
         """Returns True if the axis is busy."""         """Returns True if the axis is busy."""
         self.send_command(f"RS {axis}?\r")         self.send_command(f"RS {axis}?\r")
-        response = self.read_response() +        return "B" in self.read_response() 
-        if "B" in response: + 
-            return True +    def is_device_busy(self) -> bool: 
-        else: +        """Returns True if any axis is busy.""" 
-            return False+        self.send_command("/") 
 +        return "B" in self.read_response()
  
-    def wait_for_device(self, report: bool=False) -> None: +    def wait_for_device(self, report: bool = False) -> None: 
-        """Waits for the stage device to complete move commmands on the X and Y axes.""" +        """Waits for the all motors to stop moving."""
-        # does not report serial communications by default+
         if not report:         if not report:
             print("Waiting for device...")             print("Waiting for device...")
         temp = self.report         temp = self.report
         self.report = report         self.report = report
-        x_busy, y_busy = True, True +        busy = True 
-        while x_busy or y_busy: +        while busy: 
-            x_busy = self.is_axis_busy("X") +            busy = self.is_device_busy() 
-            y_busy = self.is_axis_busy("Y") +        self.report = temp
-        self.report = temp +
 </code> </code>
 <-- <--
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 265: Line 270:
 def main(): def main():
     hardware_cfg = "pymmcore_test.cfg"     hardware_cfg = "pymmcore_test.cfg"
-    mm_directory = "C:/Program Files/Micro-Manager-2.0gamma"+    mm_directory = "C:/Program Files/Micro-Manager-2.0"
  
     version_info = pymmcore.CMMCore().getAPIVersionInfo()     version_info = pymmcore.CMMCore().getAPIVersionInfo()
Line 299: Line 304:
 <-- <--
  
-\\ +==== Via pymmcore-plus ==== 
-==== Via Pycro-Manager ====+ 
 +See the [[https://github.com/pymmcore-plus/pymmcore-plus|pymmcore-plus GitHub page]] for information. 
 + 
 +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 ====
  
-See [[https://pycro-manager.readthedocs.io/en/latest/|Pycro-Manager]] documentation.  +See the [[https://github.com/micro-manager/pycro-manager|pycro-manager GitHub page]] for information.
  
-Micro-Manager which has excellent support for ASI devices built in.  Using Pycro-Manager is especially valuable if you also have non-ASI devices to control that are supported in Micro-Manager.  Instead of sending serial commands directly you use Micro-Manager's hardware control APIs (e.g. to move stages) and also Micro-Manager's device properties.  These properties include a mechanism for sending arbitrary serial commands.+Micro-Manager which has excellent support for ASI devices built in.  Using pycro-manager is especially valuable if you also have non-ASI devices to control that are supported in Micro-Manager.  Instead of sending serial commands directly you use Micro-Manager's hardware control APIs (e.g. to move stages) and also Micro-Manager's device properties.  These properties include a mechanism for sending arbitrary serial commands.
  
 +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.1621034772.txt.gz · Last modified: (external edit)