Serial¤
Prerequisites¤
If you are communicating with a Serial device on Windows, you already have permission (from the operating system) to access the serial port (or USB-to-Serial adaptor). If you receive an Access is denied error message when connecting to a serial port, that indicates the serial port is already open in another application.
If you are using Linux or macOS and receive a Device or resource busy error message when connecting to a serial port, that indicates the serial port is already open in another application. A Permission denied error can be solved by either running your application as the root user or by adding the current user to the same group that the serial port belongs to.
To determine the group that a serial port (e.g., /dev/ttyUSB0) belongs to, run the following.
ls -l /dev/ttyUSB0
The output will be similar to
crw-rw---- 1 root dialout 4, 64 Feb 20 17:23 /dev/ttyUSB0
where it is shown that this serial port belongs to the dialout group.
Add the current user to the appropriate group (e.g., dialout for the above case)
sudo usermod -a -G dialout $USER
and then log out and in again (or restart the computer if you still encounter permission issues after re-logging in).
Serial
(MessageBased)
¤
Serial(equipment: Equipment)
Base class for equipment that is connected through a Serial port (or a USB-to-Serial adaptor).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
equipment
|
Equipment
|
An Equipment instance. |
required |
A Connection instance supports the following properties for the serial communication protocol, as well as the properties defined in MessageBased. The DataBits, Parity and StopBits enumeration names and values may also be used. For properties that specify an alias, you may also use the alternative name as the property name. See serial.Serial for more details.
Connection Properties:
| Name | Type | Description |
|---|---|---|
baud_rate |
int
|
The baud rate (alias: baudrate). Default: |
buffer_size |
int
|
The maximum number of bytes to read at a time. Default: |
data_bits |
DataBits | str | int
|
The number of data bits: 5, 6, 7 or 8 (alias: bytesize). Default: |
dsr_dtr |
bool
|
Whether to enable hardware (DSR/DTR) flow control (alias: dsrdtr). Default: |
inter_byte_timeout |
float | None
|
The maximum duration, in seconds, that is allowed between two
consecutive bytes in a read operation. A value of zero (or |
parity |
Parity | str
|
Parity checking: NONE, ODD, EVEN, MARK or SPACE. Default: |
rts_cts |
bool
|
Whether to enable hardware (RTS/CTS) flow control (alias: rtscts). Default: |
stop_bits |
StopBits | str | float
|
The number of stop bits: 1, 1.5 or 2 (alias: stopbits). Default: |
xon_xoff |
bool
|
Whether to enable software flow control (alias: xonxoff). Default: |
Source code in src/msl/equipment/interfaces/serial.py
34 35 36 37 38 39 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 | |
disconnect
¤
disconnect() -> None
Close the serial port.
Source code in src/msl/equipment/interfaces/serial.py
137 138 139 140 141 | |
query
¤
query(
message: bytes | str,
*,
delay: float = 0.0,
decode: Literal[True] = True,
dtype: None = None,
fmt: MessageFormat = ...,
size: int | None = ...
) -> str
query(
message: bytes | str,
*,
delay: float = 0.0,
decode: Literal[False] = False,
dtype: None = None,
fmt: MessageFormat = ...,
size: int | None = ...
) -> bytes
query(
message: bytes | str,
*,
delay: float = 0.0,
decode: bool = ...,
dtype: MessageDataType = ...,
fmt: MessageFormat = ...,
size: int | None = ...
) -> NumpyArray1D
query(
message: bytes | str,
*,
delay: float = 0.0,
decode: bool = True,
dtype: MessageDataType | None = None,
fmt: MessageFormat = None,
size: int | None = None
) -> bytes | str | NumpyArray1D
Convenience method for performing a write followed by a read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
bytes | str
|
The message to write to the equipment. |
required |
delay
|
float
|
Time delay, in seconds, to wait between the write and read operations. |
0.0
|
decode
|
bool
|
True
|
|
dtype
|
MessageDataType | None
|
The data type of the elements in the returned message. Can be any object that numpy
dtype supports. For messages that are of scalar type (i.e., a single number)
it is more efficient to not specify |
None
|
fmt
|
MessageFormat
|
The format that the returned message data is in. Ignored if |
None
|
size
|
int | None
|
The number of bytes to read. Ignored if the value is |
None
|
Returns:
| Type | Description |
|---|---|
bytes | str | NumpyArray1D
|
Source code in src/msl/equipment/interfaces/message_based.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
read
¤
read(
*,
decode: Literal[True] = True,
dtype: None = None,
fmt: MessageFormat = ...,
size: int | None = ...
) -> str
read(
*,
decode: Literal[False] = False,
dtype: None = None,
fmt: MessageFormat = ...,
size: int | None = ...
) -> bytes
read(
*,
decode: bool = ...,
dtype: MessageDataType = ...,
fmt: MessageFormat = ...,
size: int | None = ...
) -> NumpyArray1D
read(
*,
decode: bool = True,
dtype: MessageDataType | None = None,
fmt: MessageFormat = None,
size: int | None = None
) -> bytes | str | NumpyArray1D
Read a message from the equipment.
This method will block until one of the following conditions is fulfilled:
sizebytes have been received — only ifsizeis notNone.- the read_termination
byte(s) is(are) received — only if
read_termination
is not
None. - a timeout occurs — only if timeout
is not
None. If a timeout occurs, an MSLTimeoutError is raised. - max_read_size bytes have been received. If the maximum number of bytes have been read, an MSLConnectionError is raised.
Tip
You may also want to set the rstrip value for the class instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decode
|
bool
|
True
|
|
dtype
|
MessageDataType | None
|
The data type of the elements in the returned message. Can be any object that numpy
dtype supports. For messages that are of scalar type (i.e., a single number)
it is more efficient to not specify |
None
|
fmt
|
MessageFormat
|
The format that the returned message data is in. Ignored if |
None
|
size
|
int | None
|
The number of bytes to read. Ignored if the value is |
None
|
Returns:
| Type | Description |
|---|---|
bytes | str | NumpyArray1D
|
Source code in src/msl/equipment/interfaces/message_based.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
write
¤
write(
message: bytes | str,
*,
data: Sequence1D | None = None,
dtype: MessageDataType = "<f",
fmt: MessageFormat = "ieee"
) -> int
Write a message to the equipment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
bytes | str
|
The message to write to the equipment. |
required |
data
|
Sequence1D | None
|
The data to append to |
None
|
dtype
|
MessageDataType
|
The data type to use to convert each element in |
'<f'
|
fmt
|
MessageFormat
|
The format to use to convert |
'ieee'
|
Returns:
| Type | Description |
|---|---|
int
|
The number of bytes written. |
Source code in src/msl/equipment/interfaces/message_based.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |