24 lines
760 B
Python
24 lines
760 B
Python
|
|
import can
|
|
import os
|
|
os.add_dll_directory(r"C:\Users\huiting\PCAN-Basic\x64") # 這裡放 64bit DLL 的資料夾
|
|
|
|
# 把 DLL 所在資料夾加進搜尋路徑
|
|
DLL_DIR = r"C:\Users\huiting\PCAN-Basic\x64" # 如果 Python 是 64bit 請換成 \x64
|
|
if os.path.isdir(DLL_DIR):
|
|
os.add_dll_directory(DLL_DIR)
|
|
|
|
bus = can.interface.Bus(interface="pcan", channel="PCAN_USBBUS1", bitrate=500000)
|
|
|
|
NODE_ID = 1
|
|
TPDO1_ID = 0x180 + NODE_ID # PLC 送出的 TPDO1
|
|
|
|
print(f"Listening TPDO1 at 0x{TPDO1_ID:X} …")
|
|
while True:
|
|
msg = bus.recv(timeout=2.0)
|
|
if not msg:
|
|
continue
|
|
if msg.arbitration_id == TPDO1_ID and len(msg.data) >= 2:
|
|
val = int.from_bytes(msg.data[0:2], "little", signed=True)
|
|
print(f"PLC TPDO1 -> 0x2000:00 = {val}")
|