Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions minimalmodbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
__version__ = "2.1.1"

import sys
import threading

if sys.version_info < (3, 8, 0):
raise ImportError(
Expand Down Expand Up @@ -122,6 +123,7 @@ def __init__(
mode: str = MODE_RTU,
close_port_after_each_call: bool = False,
debug: bool = False,
lock: threading.Lock = threading.Lock()
) -> None:
"""Initialize instrument and open corresponding serial port."""
self.address = slaveaddress
Expand All @@ -135,7 +137,7 @@ def __init__(

New in version 2.0: Support for broadcast
"""

self.lock = lock
self.mode = mode
"""Slave mode (str), can be :data:`minimalmodbus.MODE_RTU` or
:data:`minimalmodbus.MODE_ASCII`. Most often set by the constructor (see the
Expand Down Expand Up @@ -1350,7 +1352,8 @@ def _perform_command(self, functioncode: int, payload_to_slave: bytes) -> bytes:
)

# Communicate
response_bytes = self._communicate(request_bytes, number_of_bytes_to_read)
with self.lock:
response_bytes = self._communicate(request_bytes, number_of_bytes_to_read)

if number_of_bytes_to_read == 0:
return b""
Expand Down