project pyads

Project description

pyads - Python package

 

 

 

This is a python wrapper for TwinCATs ADS library. It provides python functions for communicating with TwinCAT devices. pyads uses the C API provided by TcAdsDll.dll on Windows adslib.so on Linux. The documentation for the ADS API is available on infosys.beckhoff.com.

Documentation: http://pyads.readthedocs.io/en/latest/index.html

Installation

From PyPi:

$ pip install pyads

From Github:

$ git clone https://github.com/MrLeeh/pyads.git --recursive$ cd pyads$ python setup.py install

Quickstart

Creating routes

ADS uses its own address system named AmsNetId to identify devices. The assignment of a devices to an AmsNetId happens via routing. Routing is handled differently on Windows and Linux.

Creating routes on Linux

Open a port and create a AmsAddr object for the remote machine.

>>> import pyads>>> pyads.open_port()32828

Add a route to the remote machine (Linux only - Windows routes must be added in the TwinCat Router UI).

>>> remote_ip = '192.168.0.100'>>> adr = pyads.AmsAddr('127.0.0.1.1.1', pyads.PORT_SPS1)>>> pyads.add_route(adr, remote_ip)

Get the AMS address of the local machine. This may need to be added to the routing table of the remote machine. NOTE: On Linux machines at least one route must be added before the call to get_local_address() will function properly.

Creating routes on Windows

On Windows you don't need to manually add the routes with pyads but instead you use the TwinCAT Router UI (TcSystemManager) which comes with the TwinCAT installation. Have a look at the TwinCAT documentation infosys.beckhoff.com TcSystemManager for further details.

Testserver

For first tests you can use the simple testserver that is provided with the pyads package. To start it up simply run the following command from a separate console window.

$ python -m pyads.testserver

This will create a new device on 127.0.0.1 port 48898. In the next step the route to the testserver needs to be added from another python console.

>>> import pyads>>> pyads.open_port()>>> adr = pyads.AmsAddr('127.0.0.1.1.1', pyads.PORT_SPS1)>>> pyads.add_route(adr, '127.0.0.1')

Usage

Connect to a remote device

>>> import pyads>>> plc = pyads.Connection('127.0.0.1.1.1', pyads.PORT_SPS1)>>> plc.open()>>> plc.close()

Read and write values by name

>>> import pyads>>> plc = pyads.Connection('127.0.0.1.1.1', pyads.PORT_SPS1)>>> plc.open()>>> plc.read_by_name('global.bool_value', pyads.PLCTYPE_BOOL)True>>> plc.write_by_name('global.bool_value', False, pyads.PLCTYPE_BOOL)>>> plc.read_by_name('global.bool_value', pyads.PLCTYPE_BOOL)False>>> plc.close()

If the name could not be found an Exception containing the error message and ADS Error number is raised.

>>> plc.read_by_name('global.wrong_name', pyads.PLCTYPE_BOOL)ADSError: ADSError: symbol not found (1808)

For reading strings the maximum buffer length is 1024.

>>> plc.read_by_name('global.sample_string', pyads.PLCTYPE_STRING)'Hello World'>>> plc.write_by_name('global.sample_string', 'abc', pyads.PLCTYPE_STRING)>>> plc.read_by_name(adr, 'global.sample_string', pyads.PLCTYPE_STRING)'abc'

Read and write values by address

Read and write UDINT variables by address.

>>> import pyads>>> plc = pyads.Connection('127.0.0.1.1.1', pyads.PORT_SPS1)>>> plc.open()>>> # write 65536 to memory byte MDW0>>> plc.write(pyads.INDEXGROUP_MEMORYBYTE, 0, 65536, pyads.PLCTYPE_UDINT)>>> # write memory byte MDW0>>> plc.read(pyads.INDEXGROUP_MEMORYBYTE, 0, pyads.PLCTYPE_UDINT)65536>>> plc.close()

Toggle bitsize variables by address.

>>> # read memory bit MX100.0>>> data = plc.read(pyads.INDEXGROUP_MEMORYBIT, 100*8 + 0, pyads.PLCTYPE_BOOL)>>> # write inverted value to memory bit MX100.0>>> plc.write(pyads.INDEXGROUP_MEMORYBIT, 100*8 + 0, not data)

Simple handling of notification callbacks

To make the handling of notifications more Pythonic a notification decorator has been introduced in version 2.2.4. This decorator takes care of converting the ctype values transfered via ADS to python datatypes.

>>> import pyads>>> plc = pyads.Connection('127.0.0.1.1.1', 48898)>>> plc.open()>>>>>> @plc.notification(pyads.PLCTYPE_INT)>>> def callback(handle, name, timestamp, value):>>>     print(>>>         '{0}: received new notitifiction for variable "{1}", value: {2}'>>>         .format(name, timestamp, value)>>>     )>>>>>> handles = plc.add_device_notification('GVL.intvar',                                          pyads.NotificationAttrib(2), callback)>>> # Write to the variable to trigger a notification>>> plc.write_by_name('GVL.intvar', 123, pyads.PLCTYPE_INT)2017-10-01 10:41:23.640000: received new notitifiction for variable "GVL.intvar", value: abc>>> # remove notification>>> plc.del_device_notification(handles)

The notification callback works for all basic plc datatypes but not for arrays or structures.

(0)

相关推荐

  • 学Python用哪个操作系统好?推荐Linux!

    学习Python之前,很多人都纠结系统的选择,到底选择哪个系统好呢?Linux.Windows还是MAC呢?接下来通过这篇文章我们来看看吧. 学习Python用Linux.MAC还是Windows呢? ...

  • 学Python选择什么系统?Linux学习

    随着编程语言的爆火,越来越多的人都想要转行学Python,而对于初入门Python的人来讲,刚开始学习会产生各种各样的疑问,比如学习Python一般使用什么操作系统?其实初入门Python对于系统的选 ...

  • Python环境

    windows下安装Python(手动添加环境变量)以2.7版本举例:windows:12345671 .下载安装包https: / / www.python.org / downloads /2 . ...

  • Windows 10将通过Project Latte提供对Android应用的运行支持

    cnBeta发布时间: 20-11-2808:59Windows Central 报道称,微软将通过所谓的"拿铁项目"(Project Latte),为 Windows 10 操作 ...

  • 画师笔下的LO装版东方project看着太漂亮了,感觉现实里都能穿啊

    不知道诸位朋友们第一次接触<东方project>的时候是因为啥? 我小时候,咱们国内看日本的动漫.玩日本的游戏.接触日本的同人文化啥的还不是很方便,那时候网络还不发达,我记得家家户户网络带 ...

  • 最强丰田GR超跑提上日程,千匹马力剑指AMG Project One | 酷乐汽车

    # CLAUTO 酷乐汽车 # # 丰田 # # GR Super Sport # 丰田一直想通过GR Yaris和GR Supra来提高品牌调性并吸引更多性能车车友,而在发展GR的路上,丰田的想法显 ...

  • Project单行显示多个工序

    前言 在我们的印象中Project的每条任务都是各占一行,竖向排列,如下图所示, 其实Project中也可以按照横向显示,多条任务在一行显示.这样可以更清楚地了解一件事情的前后工序关系.如下图所示 操 ...

  • 从PROJECT导出到EXCEL中的任务如何快速分级?

    作为计划工程师或计划管理人员,经常把任务从Project导出到Excel中,一是因为Excel是通用软件,计划人人都可打开编辑,二是因为Excel的强大数据处理能力,可以解决Project中无法处理的 ...

  • 万科内部资料:详细讲解如何用Project编制项目计划

    阅读引语 1编制项目计划的基本步骤 1.1设置项目开始时间 打开工具栏中的"项目信息",如下图一: 设置项目开始时间,如下图二: 1.2设定工作时间: Project默认的工作时间 ...

  • 创新突破,Yatri摩托车推出Project One概念车

    Yatri Motorcycles 是一家位于尼泊尔的电动摩托车创业公司,一直在努力开发实用,时尚和功能强大的电动摩托车.几周前,他们以零计划的形式共享了他们的第一款电动摩托车的量产版.这次,该公司透 ...

  • Project公式入门

    前言 看到题目,你也许认为老崔把EXCEL写成PROJECT了吧,难道PROJECT也可以写公式吗?当然可以,只是99%的人不知道而已! 就我的观察:大家对PROJECT的应用仅限于任务列表,搭建逻辑 ...

  • LAC的书屋 | 库哈斯名作&新陈代谢派访谈录:Project Japan 日本计划

    日本计划 Project Japan  「 作 者 介 绍 」 雷姆·库哈斯 Rem Koolhaas 1944年出生于荷兰鹿特丹,荷兰建筑师,OMA的首席设计师,哈佛大学设计研究所的建筑与城市规划学 ...