用python pyads模块读写PLC (beckhoff CX9020)中的变量



我想实现一个通信网关,在lunix (Debian9)下的虚拟机(VM)和PLC (Beckhoff CX9020)之间使用ADS协议,使用python pyads模块。我通过下面的链接创建了这条路线https://pyads.readthedocs.io/en/latest/quickstart.html下面是我的代码:

import pyads
print ("=== set_local_address");
pyads.open_port ()
pyads.set_local_address ('1.2.3.4.1.1')
print (pyads.get_local_address ())
pyads.close_port ()
print ("=== add route");
pyads.open_port ()
pyads.add_route ('5.82.237.216.1.1', '192.168.0.20')
pyads.close_port ()
print ("=== add route to plc");
SENDER_AMS = '1.2.3.4.1.1'
PLC_IP = '192.168.0.20'
PLC_USERNAME = 'Administrator'
PLC_PASSWORD = 'x'
ROUTE_NAME = 'RouteToMyPC'
HOSTNAME = '192.168.0.15' # or IP
PLC_AMS_ID = '5.82.237.216.1.1'
pyads.add_route_to_plc (SENDER_AMS, HOSTNAME, PLC_IP, PLC_USERNAME, PLC_PASSWORD, route_name = ROUTE_NAME)
print ("=== init connection to plc");
plc = pyads.Connection (PLC_AMS_ID, 851, PLC_IP)
print ("=== open connection")
plc.open ()
print ('Connected')
v1 = plc.read_by_name ("MAIN.v1", pyads.PLCTYPE_INT)
print (v1)

我可以访问PLC,但我不能读或写,我仍然得到错误信息


Connecting... 
2021-01-21117:17:59+0100 Info: Connected to 192.168.0.20 
Connected 
Traceback (most recent call last): 
File "rw_pyads.py", tine 34, in <module> 
value = plc.read by_name('MAIN.vr, pyads.PLCTYPE INT) 
File "/usr/local/lib/python3.8/site-packages/pyads/ads.py", line 761, in read_by_name 
return adsSyncReadByNameEx( 
File "/usr/local/lib/python3.8/site-packages/pyads/pyads_ex.py", line 977, in adsSyncReadByNameEx
handle = adsGetHandle(port, address, data name) 
File "/usr/local/lib/python3.8/site-packages/pyads/pyads_ex.py", line 770, in adsGetHandle
handle = adsSyncReadWriteReqEx2(
File "/usr/local/lib/python3.8/site-packages/pyads/pyads_ex.py", line 638, in adsSyncReadWriteReqEx2
raise ADSError(err code)
pyads.pyads ex.ADSError: ADSError: timeout elapsed (1861). 
2021-01-21117:18:04+0100 Info: connection closed by remote 

有人遇到过这个问题吗?有人能告诉我到底是什么问题吗?我花了几天时间,但还是没能解决问题谢谢!

我有同样的问题,有一个python脚本与Twincat3共享变量,但在20分钟后连接下降到关闭连接。

设法"解决";通过重新启动连接的批处理文件,错误后。因此,现在每次出现错误时,连接都会恢复。

祝你好运!Goncalo

相关内容

  • 没有找到相关文章

最新更新