106 lines
2.6 KiB
Python
106 lines
2.6 KiB
Python
import ctypes
|
|
import os
|
|
import time
|
|
|
|
dll_path = os.path.abspath("ControlCANFD.dll")
|
|
canfd = ctypes.cdll.LoadLibrary(dll_path)
|
|
|
|
DEVICE_TYPE = 41
|
|
DEVICE_INDEX = 0
|
|
CAN_CHANNEL_INDEX = 0
|
|
BAUD_RATE = 500000
|
|
TARGET_ID = 0x11
|
|
|
|
class ZCAN_CHANNEL_INIT_CONFIG(ctypes.Structure):
|
|
_fields_ = [
|
|
("acc_code", ctypes.c_uint),
|
|
("acc_mask", ctypes.c_uint),
|
|
("reserved", ctypes.c_uint),
|
|
("filter", ctypes.c_ubyte),
|
|
("mode", ctypes.c_ubyte),
|
|
("padding1", ctypes.c_ubyte),
|
|
("padding2", ctypes.c_ubyte),
|
|
("baud_rate", ctypes.c_uint),
|
|
]
|
|
|
|
class ZCAN_CAN_FRAME(ctypes.Structure):
|
|
_fields_ = [
|
|
("can_id", ctypes.c_uint),
|
|
("data_len", ctypes.c_ubyte),
|
|
("flags", ctypes.c_ubyte), # Deve ser zero para CAN clássico
|
|
("__res0", ctypes.c_ubyte),
|
|
("__res1", ctypes.c_ubyte),
|
|
("data", ctypes.c_ubyte * 8),
|
|
]
|
|
|
|
class ZCAN_Transmit_Data(ctypes.Structure):
|
|
_fields_ = [
|
|
("frame", ZCAN_CAN_FRAME),
|
|
("transmit_type", ctypes.c_uint),
|
|
]
|
|
|
|
ZCAN_OpenDevice = canfd.ZCAN_OpenDevice
|
|
ZCAN_OpenDevice.argtypes = [ctypes.c_uint, ctypes.c_uint, ctypes.c_uint]
|
|
ZCAN_OpenDevice.restype = ctypes.c_void_p
|
|
|
|
ZCAN_InitCAN = canfd.ZCAN_InitCAN
|
|
ZCAN_InitCAN.argtypes = [ctypes.c_void_p, ctypes.c_uint, ctypes.POINTER(ZCAN_CHANNEL_INIT_CONFIG)]
|
|
ZCAN_InitCAN.restype = ctypes.c_void_p
|
|
|
|
ZCAN_StartCAN = canfd.ZCAN_StartCAN
|
|
ZCAN_StartCAN.argtypes = [ctypes.c_void_p]
|
|
ZCAN_StartCAN.restype = ctypes.c_uint
|
|
|
|
ZCAN_Transmit = canfd.ZCAN_Transmit
|
|
ZCAN_Transmit.argtypes = [ctypes.c_void_p, ctypes.POINTER(ZCAN_Transmit_Data), ctypes.c_uint]
|
|
ZCAN_Transmit.restype = ctypes.c_uint
|
|
|
|
dev = ZCAN_OpenDevice(DEVICE_TYPE, DEVICE_INDEX, 0)
|
|
|
|
print(f"Device Handle: {dev}, type: {type(dev)}")
|
|
|
|
if not dev:
|
|
print("❌ Erro ao abrir dispositivo")
|
|
exit()
|
|
|
|
config = ZCAN_CHANNEL_INIT_CONFIG(
|
|
acc_code=0,
|
|
acc_mask=0xFFFFFFFF,
|
|
reserved=0,
|
|
filter=0,
|
|
mode=0,
|
|
padding1=0,
|
|
padding2=0,
|
|
baud_rate=BAUD_RATE
|
|
)
|
|
|
|
chn = ZCAN_InitCAN(dev, CAN_CHANNEL_INDEX, ctypes.byref(config))
|
|
if not chn:
|
|
print("❌ Erro ao iniciar canal")
|
|
exit()
|
|
|
|
if ZCAN_StartCAN(chn) != 1:
|
|
print("❌ Falha ao iniciar CAN")
|
|
exit()
|
|
|
|
frame = ZCAN_CAN_FRAME()
|
|
frame.can_id = TARGET_ID & 0x7FF
|
|
frame.data_len = 2
|
|
frame.flags = 1 # ⚠️ CAN clássico!
|
|
frame.__res0 = 0
|
|
frame.__res1 = 0
|
|
frame.data = (ctypes.c_ubyte * 8)(0x00, 0x00, 0, 0, 0, 0, 0, 0)
|
|
|
|
tx = ZCAN_Transmit_Data()
|
|
tx.frame = frame
|
|
tx.transmit_type = 0
|
|
|
|
print(f"🟢 Enviando para ID 0x{TARGET_ID:X}...")
|
|
|
|
result = ZCAN_Transmit(chn, ctypes.byref(tx), 1)
|
|
|
|
if result > 0:
|
|
print("✅ Enviado com sucesso.")
|
|
else:
|
|
print("❌ Falha no envio.")
|