import pyads def main(): # 开启ADS服务端口 pyads.open_port() sending_net_id = '169.254.111.127.1.1' # PLC的netid adding_host_name = '192.168.100.21' # 填Liunx的IP地址 ip_address = '192.168.100.22' # PLC的IP username = 'Administrator' # PLC用户名 password = '1' # PLC密码 route_name = 'liunxtest' # Linux的名字 added_net_id = '192.168.100.21.1.1' # Linux的netid print('默认配置为:\r\n') print('PLC的IP地址:%s' % ip_address) print('PLC的netid:%s' % sending_net_id) print('PLC的用户名:%s' % username) print('PLC的密码:%s' % password) print('Liunx的IP地址:%s' % adding_host_name) print('Liunx的netid:%s' % added_net_id) num = input('是否使用以上默认配置,是请输入1,不是请输入2:') if ( int(num) != 1 ): sending_net_id = input('请输入PLC的netid:') adding_host_name = input('请输入Liunx的IP地址:') ip_address = input('请输入PLC的IP地址:') username = input('请输入PLC的用户名:') password = input('请输入PLC的密码:') route_name = 'liunxtest' # Linux的名字 added_net_id = input('请设置Liunx的netid:') # 远控PLC加Linux的路由 pyads.add_route_to_plc(sending_net_id,adding_host_name,ip_address,username,password,route_name,added_net_id) # Linux的路由表加plc pyads.add_route(sending_net_id, ip_address) # 设置Linux的netid pyads.set_local_address('192.168.100.21.1.1') # 连接PLC 851端口 plc = pyads.Connection(sending_net_id,851,ip_address) plc.open() # 读写PLC变量 print('正在读取PLC变量……') test = plc.read_by_name('MAIN.test', pyads.PLCTYPE_INT) print('读取到PLC中MAIN.test的值为:%u' % test) print('正在写入PLC变量……') plc.write_by_name('MAIN.writetest', 100, pyads.PLCTYPE_INT) print('写入成功') plc.close() pyads.close_port() if __name__ == '__main__': main()