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 [2023/08/15 14:03] – download scripts 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.10.1 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.
Line 53: Line 53:
         return com_ports         return com_ports
    
-    def connect_to_serial( +    def connect_to_serial(self, rx_size: int = 12800, tx_size: int = 12800, read_timeout: int = 1, write_timeout: int = 1) -> None:
-        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 127: 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 161: 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:     def get_position(self, axis: str) -> int:
         """Return the position of the stage in ASI units (tenths of microns)."""         """Return the position of the stage in ASI units (tenths of microns)."""
-        self.send_command(f"WHERE {axis}\r")+        self.send_command(f"W {axis}\r")
         response = self.read_response()         response = self.read_response()
         return int(response.split(" ")[1])         return int(response.split(" ")[1])
Line 192: Line 186:
     def get_position_um(self, axis: str) -> float:     def get_position_um(self, axis: str) -> float:
         """Return the position of the stage in microns."""         """Return the position of the stage in microns."""
-        self.send_command(f"WHERE {axis}\r")+        self.send_command(f"W {axis}\r")
         response = self.read_response()         response = self.read_response()
         return float(response.split(" ")[1])/10.0         return float(response.split(" ")[1])/10.0
Line 225: 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 259: 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 311: 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.1692122610.txt.gz · Last modified: by brandon