diff --git a/udsoncan/connections.py b/udsoncan/connections.py index 2ed3a37..ee91e50 100755 --- a/udsoncan/connections.py +++ b/udsoncan/connections.py @@ -7,7 +7,6 @@ from abc import ABC, abstractmethod import time from typing import Union, Dict -import ctypes import selectors try: @@ -696,7 +695,7 @@ def __init__(self, txid: Optional[int] = None, extid: Optional[int] = None, name: Optional[str] = None, - debug: bool = False, + debug: Optional[bool] = None, protocol = None, baudrate = 500000, ): @@ -706,7 +705,6 @@ def __init__(self, self.result = Error_ID.ERR_SUCCESS self.protocol = protocol or Protocol_ID.ISO15765 self.baudrate = baudrate - self.dll_debug = debug try: self.interface = J2534(windll) @@ -720,6 +718,11 @@ def __init__(self, self.open() self.set_can_id(txid, rxid, extid) + if isinstance(debug, bool): + self.logger.critical("Argument debug deprecated in the constructor. Call conn.set_tatrix_debug() if need.") + if debug: + self.set_tatrix_debug() + def __enter__(self) -> "J2534Connection": self.open() return self @@ -749,13 +752,6 @@ def open(self) -> "J2534Connection": self.log_last_operation("PassThruOpen", with_raise=True) - if self.dll_debug: - self.result = self.interface.PassThruIoctl(0, - Ioctl_Flags.TX_IOCTL_SET_DLL_DEBUG_FLAGS, - SCONFIG_LIST([(0, Ioctl_Flags.TX_IOCTL_DLL_DEBUG_FLAG_J2534_CALLS.value)]) - ) - self.log_last_operation("PassThruIoctl SET_DLL_DEBUG") - # Get the firmeware and DLL version etc, mainly for debugging output self.result, self.firmwareVersion, self.dllVersion, self.apiVersion = self.interface.PassThruReadVersion(self.devID) self.log_last_operation("PassThruReadVersion") @@ -860,15 +856,18 @@ def log_last_operation(self, exec_method: str, with_raise = False) -> None: def read_vbatt(self, digits=1) -> float: self.check_connection_opened() - vbatt = ctypes.POINTER(ctypes.c_int32)() - - self.result = self.interface.PassThruIoctl(self.channelID, Ioctl_ID.READ_VBATT, None, vbatt) + self.result, value = self.interface.PassThruIoctl_READ_VBATT(self.devID) self.log_last_operation("PassThruIoctl READ_VBATT") - value = ctypes.cast(vbatt, ctypes.c_void_p).value - return round(value / 1000, digits) if value else 0 + def set_tatrix_debug(self, enable: bool = True): + from ctypes import c_ulong + flags = Ioctl_Flags.TX_IOCTL_DLL_DEBUG_FLAG_J2534_CALLS.value if enable else 0 + + self.result = self.interface.PassThruIoctl(0, Ioctl_ID.TX_IOCTL_SET_DLL_DEBUG_FLAGS, c_ulong(flags), None) + self.log_last_operation("PassThruIoctl SET_DLL_DEBUG_FLAGS") + class FakeConnection(BaseConnection): """ diff --git a/udsoncan/j2534.py b/udsoncan/j2534.py index 2678373..a196ba2 100755 --- a/udsoncan/j2534.py +++ b/udsoncan/j2534.py @@ -1,5 +1,5 @@ from enum import Enum -from ctypes import Structure, WINFUNCTYPE, POINTER, cast, cdll, c_char, c_long, c_void_p, c_ubyte, c_ulong, byref # type: ignore +from ctypes import Structure, WINFUNCTYPE, POINTER, cdll, c_char, c_long, c_void_p, c_ubyte, c_ulong, byref # type: ignore class Error_ID(Enum): @@ -218,6 +218,12 @@ class Ioctl_ID(Enum): T3_MAX = 0x24 ISO15765_WFT_MAX = 0x25 + # Tatrix specific + TX_IOCTL_APP_SERVICE = 0x70000 + TX_IOCTL_SET_DLL_DEBUG_FLAGS = 0x70001 + TX_IOCTL_SET_DEV_DEBUG_FLAGS = 0x70002 + TX_IOCTL_SET_DLL_STATUS_CALLBACK = 0x70003 + # J2534-2 CAN_MIXED_FORMAT = 0x8000 J1962_PINS = 0x8001 @@ -234,10 +240,11 @@ class Ioctl_ID(Enum): INPUT_RANGE_HIGH = 0x8027 # Upper limit in millivolts of A/D input. Read Only. +# Tatrix specific class Ioctl_Flags(Enum): - TX_IOCTL_BASE = 0x70000 - TX_IOCTL_SET_DLL_DEBUG_FLAGS = 0x70001 - TX_IOCTL_DLL_DEBUG_FLAG_J2534_CALLS = 0x00000001 + TX_IOCTL_DLL_DEBUG_FLAG_J2534_CALLS = 0x00000001 + TX_IOCTL_DLL_DEBUG_FLAG_ALL_DEV_COMMS = 0x00000002 + TX_IOCTL_DEV_DEBUG_FLAG_USB_COMMS = 0x00000001 class PASSTHRU_MSG(Structure): @@ -384,7 +391,7 @@ def __init__(self, windll: str): c_void_p, c_void_p, ) - dllPassThruIoctlParams = (1, "Handle", 0), (1, "IoctlID", 0), (1, "pInput", 0), (1, "pOutput", 0) + dllPassThruIoctlParams = (1, "HandleID", 0), (1, "IoctlID", 0), (1, "pInput", 0), (1, "pOutput", 0) self.dllPassThruIoctl = dllPassThruIoctlProto(("PassThruIoctl", self.hDLL), dllPassThruIoctlParams) def PassThruOpen(self): @@ -465,13 +472,19 @@ def PassThruGetLastError(self): result = self.dllPassThruGetLastError(pErrorDescription) return Error_ID(result), pErrorDescription.value.decode() - def PassThruIoctl(self, Handle, IoctlID, ioctlInput=None, ioctlOutput=None): + def PassThruIoctl(self, HandleID, IoctlID: Ioctl_ID, ioctlInput=None, ioctlOutput=None): pInput = None if ioctlInput is None else byref(ioctlInput) pOutput = None if ioctlOutput is None else byref(ioctlOutput) - result = self.dllPassThruIoctl(Handle, c_ulong(IoctlID.value), pInput, pOutput) + result = self.dllPassThruIoctl(HandleID, c_ulong(IoctlID.value), pInput, pOutput) return Error_ID(result) + def PassThruIoctl_READ_VBATT(self, DeviceID): + vbatt = c_ulong() + + result = self.PassThruIoctl(DeviceID, Ioctl_ID.READ_VBATT, None, vbatt) + return result, vbatt.value + def PassThruStartMsgFilter(self, ChannelID, txid: int, rxid: int, extid = None): self.txid = txid.to_bytes(4, "big") self.rxid = rxid.to_bytes(4, "big")