Comm
pychilaslasers.comm
Serial communication interface for Chilas laser systems.
Provides low-level serial communication with laser drivers, including command/response handling, connection management, and port discovery.
Classes:
-
Communication–Main serial communication handler.
Functions:
-
list_comports–Discover available COM ports.
Authors: RLK, AVR, SDU
pychilaslasers.comm.Communication(com_port: str)
Communication class for handling communication with the laser driver over serial.
This class provides methods for sending commands to the laser, receiving responses, and managing the serial connection. It also handles the prefix mode for the laser driver.
This method sets up the serial connection to the laser driver and initializes the communication parameters. It also registers cleanup functions to ensure the serial connection is properly closed on exit or signal termination. And sets the initial baudrate to the default value. When a connection fails, it will attempt to reconnect using the next supported baudrate until a connection is established as this is one of the most common issues when connecting to the laser driver.
Parameters:
-
com_port(str) –The serial port to connect to the laser driver. this can be found by using the
pychilaslasers.comm.list_comports()function.
-
Package Reference
Laser
Laser Attributescomm
Source code in src/pychilaslasers/comm.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
Attributes
prefix_mode: bool
property
writable
Gets prefix mode for the laser driver.
The laser can be operated in two different communication modes:
- Prefix mode on
- Prefix mode off
When prefix mode is on, every message over the serial connection will be
replied to by the driver with a response, and every response will be
prefixed with a return code (rc), either 0 or 1 for an OK or ERROR
respectively.
With prefix mode is off, responses from the laser driver are not prefixed with a return code. This means that in the case for a serial write command without an expected return value, the driver will not send back a reply.
Returns:
-
bool–whether prefix mode is enabled (True) or disabled (False)
baudrate: int
property
writable
Gets the baudrate of the serial connection to the driver.
The baudrate can be changed, but does require a serial reconnect
Currently supported baudrates are:
- 9600
- 14400
- 19200
- 28800
- 38400
- 57600 default
- 115200
- 230400
- 460800
- 912600
Returns:
-
int–baudrate currently in use
port: str
property
Get the serial port currently used for communication.
Returns:
-
str(str) –The name of the serial port in use.
Functions
__del__() -> None
Destructor that ensures the serial connection is closed after deletion.
This method is called when the object is garbage collected, providing an additional safety mechanism to ensure the serial connection is properly closed even if the user forgets to call close explicitly or if the program terminates unexpectedly.
Source code in src/pychilaslasers/comm.py
111 112 113 114 115 116 117 118 119 | |
query(data: str) -> str
Send a command to the laser and return its response.
This method sends a command to the laser over the serial connection and returns the response. It also handles the logging of the command and response. The response code of the reply is checked and an error is raised if the response code is not 0. Commands that are sent multiple times may be replaced with a semicolon to speed up communication.
Parameters:
-
data(str) –The serial command to be sent to the laser.
Returns:
-
str(str) – -
str–The response from the laser. The response is stripped of any leading or trailing whitespace as well as the return code. Response may be empty if the command does not return a value.
Raises:
-
SerialException–If there is an error in the serial communication, such as a decoding error or an empty reply.
-
LaserError–If the response code from the laser is not 0, indicating an error.
Source code in src/pychilaslasers/comm.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
close_connection(signum=None, fname=None) -> None
Close the serial connection to the laser driver safely.
Attempts to reset the prefix mode and baudrate to the initial value before closing the connection.
This method is registered to be called on exit or when a signal is received.
Source code in src/pychilaslasers/comm.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
pychilaslasers.comm.list_comports() -> list[str]
List all available COM ports on the system.
serial.tools.list_ports.comports is used to list all available
ports. In that regard this method is but a wrapper for it.
Returns:
-
list[str]–List of available COM ports as strings sorted
-
list[str]–alphabetically in ascending order.
Source code in src/pychilaslasers/comm.py
386 387 388 389 390 391 392 393 394 395 396 | |