使用:pyasds-3.3.8、python-3.9.5
我有一个非常简单的脚本,主要是从 Python Twincat ADS 库 pyads 的文档中复制的。
import sys
import pyads
import socket
def get_local_ip_address(remote_server):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect((remote_server, 80))
return s.getsockname()[0]
def create_route_back(plc_ip):
local_ip = get_local_ip_address(plc_ip)
local_net_id = local_ip + '.1.1'
plc_username = 'Administrator'
plc_password = '1'
hostname = socket.gethostname()
route_name = 'route_to_' + hostname
print (f""""Creating route:
local_net_id: {local_net_id}
local_ip: {local_ip}
plc_ip: {plc_ip}
plc_username: {plc_username}
plc_password: {plc_password}
route_name: {route_name}
""")
pyads.open_port()
pyads.set_local_address(local_net_id)
pyads.add_route_to_plc(
local_net_id,
local_ip,
plc_ip,
plc_username,
plc_password,
route_name)
pyads.close_port()
def plc_connection(plc_ip, plc_net_id):
create_route_back(plc_ip)
return pyads.Connection(plc_net_id, pyads.PORT_TC3PLC1, plc_ip)
def print_version(plc):
(device_name, version) = plc.read_device_info()
version = f'{version.version}.{version.revision}.{version.build}'
print(device_name, version)
def get_symbols(plc):
return [
(symbol.name, symbol.plc_type)
for symbol in plc.get_all_symbols()
]
def main(plc_ip, plc_net_id):
with plc_connection(plc_ip, plc_net_id) as plc:
print_version(plc)
for (symbol_name, plc_type) in get_symbols(plc):
print (symbol_name, plc_type)
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2])
当我使用正在运行的 PLC 的 IP 和 AMS 网络 ID 运行此脚本时,我收到一个奇怪的错误。
2021-10-14T20:23:26+0000 Info: Connected to 10.0.0.10
Plc30 App 3.1.1927
2021-10-14T20:23:26+0000 Info: connection closed by remote
Traceback (most recent call last):
File "/home/mmachenry/src/telemetry/test/get_plc_symbols.py", line 62, in <module>
main(sys.argv[1], sys.argv[2])
File "/home/mmachenry/src/telemetry/test/get_plc_symbols.py", line 58, in main
for (symbol_name, plc_type) in get_symbols(plc):
File "/home/mmachenry/src/telemetry/test/get_plc_symbols.py", line 52, in get_symbols
for symbol in plc.get_all_symbols()
File "/home/mmachenry/src/telemetry/test/venv/lib/python3.9/site-packages/pyads/ads.py", line 773, in get_all_symbols
symbol_size_msg = self.read(
File "/home/mmachenry/src/telemetry/test/venv/lib/python3.9/site-packages/pyads/ads.py", line 699, in read
return adsSyncReadReqEx2(
File "/home/mmachenry/src/telemetry/test/venv/lib/python3.9/site-packages/pyads/pyads_ex.py", line 787, in adsSyncReadReqEx2
raise ADSError(error_code)
pyads.pyads_ex.ADSError: ADSError: symbol not found (1808).
请注意,连接有效,我可以从 PLC 中提取设备名称和版本。但是,当我尝试 get_all_symbols 时,它会崩溃并抱怨找不到某个符号 (1808)。我无法弄清楚它为什么会崩溃。我从来没有要求符号 1808。只是为了所有符号的列表。