diff --git a/j1939/Dm14Query.py b/j1939/Dm14Query.py index a27fcf8..8c79d9a 100644 --- a/j1939/Dm14Query.py +++ b/j1939/Dm14Query.py @@ -213,6 +213,7 @@ def read( object_byte_size: int = 1, signed: bool = False, return_raw_bytes: bool = False, + max_timeout: int = 1, ) -> list: """ Send a read query to dest_address, requesting data at address @@ -223,6 +224,7 @@ def read( :param int object_byte_size: size of each object in bytes :param bool signed: whether the data is signed :param bool return_raw_bytes: whether to return raw bytes or values + :param int max_timeout: max timeout for transaction """ assert object_count > 0 self._dest_address = dest_address @@ -255,6 +257,7 @@ def write( address: int, values: list, object_byte_size: int = 1, + max_timeout: int = 1, ) -> None: """ Send a write query to dest_address, requesting to write values at address @@ -263,6 +266,7 @@ def write( :param int address: address of the message :param list values: values to be written :param int object_byte_size: size of each object in bytes + :param int max_timeout: max timeout for transaction """ self._dest_address = dest_address self.direct = direct @@ -276,7 +280,7 @@ def write( self.state = QueryState.WAIT_FOR_SEED # wait for operation completed DM15 message try: - self.data_queue.get(block=True, timeout=1) + self.data_queue.get(block=True, timeout=max_timeout) for _ in range(self.exception_queue.qsize()): raise self.exception_queue.get(block=False, timeout=1) except queue.Empty: diff --git a/j1939/Dm14Server.py b/j1939/Dm14Server.py index de57a4a..c039a1b 100644 --- a/j1939/Dm14Server.py +++ b/j1939/Dm14Server.py @@ -340,6 +340,7 @@ def respond( data=None, error: int = 0xFFFFFF, edcp: int = 0xFF, + max_timeout: int = 3, ) -> list: """ Respond to DM14 query with the requested data or confimation of operation is good to proceed @@ -347,6 +348,7 @@ def respond( :param list data: data to be sent to device :param int error: error code to be sent to device :param int edcp: value for edcp extension + :param int max_timeout: max time for transaction """ if data is None: data = [] @@ -366,5 +368,5 @@ def respond( self._wait_for_data() mem_data = None if self.state == ResponseState.WAIT_FOR_DM16: - mem_data = self.data_queue.get(block=True, timeout=3) + mem_data = self.data_queue.get(block=True, timeout=max_timeout) return mem_data diff --git a/j1939/memory_access.py b/j1939/memory_access.py index 816c070..434f798 100644 --- a/j1939/memory_access.py +++ b/j1939/memory_access.py @@ -128,7 +128,12 @@ def _listen_for_dm14( pass def respond( - self, proceed: bool, data: list = None, error: int = 0xFFFFFF, edcp: int = 0xFF + self, + proceed: bool, + data: list = None, + error: int = 0xFFFFFF, + edcp: int = 0xFF, + max_timeout: int = 3, ) -> list: """ Responds with requested data and error code, if applicable, to a read request @@ -136,13 +141,14 @@ def respond( :param list data: data to be sent to device :param int error: error code to be sent to device :param int edcp: value for edcp extension + :param int max_timeout: max timeout for transaction """ if data is None: data = [] if self.state is DMState.WAIT_RESPONSE: self._ca.unsubscribe(self._listen_for_dm14) self.state = DMState.IDLE - return_data = self.server.respond(proceed, data, error, edcp) + return_data = self.server.respond(proceed, data, error, edcp, max_timeout) self._ca.subscribe(self._listen_for_dm14) return return_data else: @@ -157,6 +163,7 @@ def read( object_byte_size: int = 1, signed: bool = False, return_raw_bytes: bool = False, + max_timeout: int = 1, ) -> list: """ Make a dm14 read Query @@ -167,6 +174,7 @@ def read( :param int object_byte_size: size of each object in bytes :param bool signed: whether the data is signed :param bool return_raw_bytes: whether to return raw bytes or values + :param int max_timeout: max timeout for transaction """ if self.state == DMState.IDLE: self.state = DMState.WAIT_QUERY @@ -179,6 +187,7 @@ def read( object_byte_size, signed, return_raw_bytes, + max_timeout, ) self.state = DMState.IDLE return data @@ -192,6 +201,7 @@ def write( address: int, values: list, object_byte_size: int = 1, + max_timeout: int = 1, ) -> None: """ Send a write query to dest_address, requesting to write values at address @@ -200,11 +210,14 @@ def write( :param int address: address of the message :param list values: values to be written :param int object_byte_size: size of each object in bytes + :param int max_timeout: max timeout for transaction """ if self.state == DMState.IDLE: self.state = DMState.WAIT_QUERY self.address = dest_address - self.query.write(dest_address, direct, address, values, object_byte_size) + self.query.write( + dest_address, direct, address, values, object_byte_size, max_timeout + ) self.state = DMState.IDLE def set_seed_generator(self, seed_generator: callable) -> None: