From f0e11ad0b4ff10758246af7a7c0fb23f6ef4f76a Mon Sep 17 00:00:00 2001 From: Kirill <33geek@gmail.com> Date: Tue, 19 Nov 2024 18:04:24 +0300 Subject: [PATCH 1/4] Nice to have select "J2534-2:" with open J2534 connection --- udsoncan/j2534.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/udsoncan/j2534.py b/udsoncan/j2534.py index 784fa5f..4dd50d0 100755 --- a/udsoncan/j2534.py +++ b/udsoncan/j2534.py @@ -185,7 +185,7 @@ def PassThruOpen(self, pDeviceID=None): if not pDeviceID: pDeviceID = ctypes.c_ulong() - result = dllPassThruOpen(ctypes.POINTER(ctypes.c_int)(), byref(pDeviceID)) + result = dllPassThruOpen(bytes('J2534-2:', 'ascii'), byref(pDeviceID)) return Error_ID(hex(result)), pDeviceID def PassThruConnect(self, deviceID, protocol, baudrate, pChannelID=None): From 62ee6287fd58cbf401c032e41e2d924773d8cce6 Mon Sep 17 00:00:00 2001 From: Kirill <33geek@gmail.com> Date: Tue, 19 Nov 2024 18:05:02 +0300 Subject: [PATCH 2/4] Log operation PassThruWriteMsg with raising if fails --- udsoncan/connections.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/udsoncan/connections.py b/udsoncan/connections.py index 6f8cc7e..138df64 100755 --- a/udsoncan/connections.py +++ b/udsoncan/connections.py @@ -846,7 +846,8 @@ def close(self) -> None: def specific_send(self, payload: bytes, timeout: Optional[float] = None): if timeout is None: timeout = 0 - result = self.interface.PassThruWriteMsgs(self.channelID, payload, self.protocol.value, Timeout=int(timeout * 1000)) + self.result = self.interface.PassThruWriteMsgs(self.channelID, payload, self.protocol.value, Timeout=int(timeout * 1000)) + self.log_last_operation('PassThruWriteMsgs', with_raise=True) def specific_wait_frame(self, timeout: Optional[float] = None) -> Optional[bytes]: if not self.opened: From f37190817850c3c84ce7e18d982162c303f063ca Mon Sep 17 00:00:00 2001 From: Kirill <33geek@gmail.com> Date: Tue, 19 Nov 2024 18:05:55 +0300 Subject: [PATCH 3/4] Temporary fix for ERR_CONCURRENT_API_CALL --- udsoncan/connections.py | 1 + 1 file changed, 1 insertion(+) diff --git a/udsoncan/connections.py b/udsoncan/connections.py index 138df64..a133840 100755 --- a/udsoncan/connections.py +++ b/udsoncan/connections.py @@ -813,6 +813,7 @@ def is_open(self) -> bool: def rxthread_task(self) -> None: while not self.exit_requested: try: + time.sleep(0.02) # FIXME avoid ERR_CONCURRENT_API_CALL result, data, numMessages = self.interface.PassThruReadMsgs(self.channelID, self.protocol.value, 1, 1) if data is not None: self.rxqueue.put(data) From 8fa13a3f6bfe312932d13c11028bae6104b8b68d Mon Sep 17 00:00:00 2001 From: Kirill <33geek@gmail.com> Date: Tue, 19 Nov 2024 18:59:11 +0300 Subject: [PATCH 4/4] Semaphore --- udsoncan/connections.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/udsoncan/connections.py b/udsoncan/connections.py index a133840..a421b8a 100755 --- a/udsoncan/connections.py +++ b/udsoncan/connections.py @@ -795,6 +795,7 @@ def __init__(self, windll: str, rxid: int, txid: int, name: Optional[str] = None def open(self) -> "J2534Connection": self.exit_requested = False + self.sem = threading.Semaphore() self.rxthread = threading.Thread(target=self.rxthread_task, daemon=True) self.rxthread.start() self.opened = True @@ -812,14 +813,16 @@ def is_open(self) -> bool: def rxthread_task(self) -> None: while not self.exit_requested: + self.sem.acquire() try: - time.sleep(0.02) # FIXME avoid ERR_CONCURRENT_API_CALL result, data, numMessages = self.interface.PassThruReadMsgs(self.channelID, self.protocol.value, 1, 1) if data is not None: self.rxqueue.put(data) except Exception: self.logger.critical("Exiting J2534 rx thread") self.exit_requested = True + self.sem.release() + time.sleep(0.001) def log_last_operation(self, exec_method: str, with_raise = False) -> None: if self.result != Error_ID.ERR_SUCCESS: @@ -845,10 +848,13 @@ def close(self) -> None: self.log_last_operation('PassThruClose') def specific_send(self, payload: bytes, timeout: Optional[float] = None): - if timeout is None: - timeout = 0 + timeout = 0 if timeout is None else timeout + + # Fix for avoid ERR_CONCURRENT_API_CALL. Stop reading + self.sem.acquire() self.result = self.interface.PassThruWriteMsgs(self.channelID, payload, self.protocol.value, Timeout=int(timeout * 1000)) self.log_last_operation('PassThruWriteMsgs', with_raise=True) + self.sem.release() def specific_wait_frame(self, timeout: Optional[float] = None) -> Optional[bytes]: if not self.opened: