modbus_MediaPipe/write_sdo_min.py

32 lines
973 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# file: write_sdo_min.py
# pip install python-can canopen
import time
import can
import canopen
# === 請依你的硬體改這裡 ===
IFACE = "pcan" # pcan | kvaser | ixxat | slcan
CHANNEL = "PCAN_USBBUS1" # kvaser/ixxat 用 0slcan 用 "COM5"
BITRATE = 500000
NODE_ID = 1 # 你的 PLC Node-ID
EDS = "PLC.eds" # 你的 EDS/DCF放同資料夾
INDEX = 0x2000 # 你在 PLC 端對外開放的 OD 物件
SUB = 0x00
VALUE = 1234 # 要寫入的整數(型別需與 EDS 一致)
# === 建立 bus & network ===
bus = can.interface.Bus(bustype=IFACE, channel=CHANNEL, bitrate=BITRATE)
net = canopen.Network()
net.connect(bustype="python-can", channel=bus)
node = canopen.RemoteNode(NODE_ID, EDS)
net.add_node(node)
# 建議在 PRE-OP 寫參數
node.nmt.state = "PRE-OPERATIONAL"
node.sdo[INDEX][SUB].raw = VALUE
time.sleep(0.05)
print(f"SDO write OK -> {INDEX:04X}:{SUB:02X} = {VALUE}")
net.disconnect()