python和倍福的通讯类库

  • C8_788541
    了解作者
  • 144.6KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-07 09:26
    上传日期
基于以太网,使用上位机PC与下位机PLC(倍福)通讯的Python类库,下载完成之后直接放在Python安装目录的Python\Python38-32\Lib\site-packages文件夹下,import即可使用,采用ADS协议,内部使用标签进行通讯。
pyads.rar
  • pyads
  • testserver_ex
  • __pycache__
  • structs.cpython-38.pyc
    5.9KB
  • handler.cpython-38.pyc
    6.8KB
  • server.cpython-38.pyc
    3.1KB
  • client.cpython-38.pyc
    2.9KB
  • __init__.cpython-38.pyc
    162B
  • handler.py
    8.4KB
  • __init__.py
    0B
  • server.py
    2.8KB
  • structs.py
    4.1KB
  • client.py
    3.4KB
  • __pycache__
  • testserver.cpython-38.pyc
    15.7KB
  • structs.cpython-38.pyc
    9.3KB
  • utils.cpython-38.pyc
    877B
  • filetimes.cpython-38.pyc
    2.4KB
  • constants.cpython-38.pyc
    4.5KB
  • ads.cpython-38.pyc
    30.1KB
  • __init__.cpython-38.pyc
    2.8KB
  • errorcodes.cpython-38.pyc
    4.5KB
  • pyads_ex.cpython-38.pyc
    18.9KB
  • filetimes.py
    3.7KB
  • ads.py
    35.5KB
  • pyads_ex.py
    27.3KB
  • testserver.py
    24KB
  • utils.py
    714B
  • __init__.py
    2KB
  • errorcodes.py
    4.8KB
  • structs.py
    8.8KB
  • adslib.so
    274KB
  • constants.py
    5.3KB
内容介绍
"""Pythonic ADS functions. :author: Stefan Lehmann <stlm@posteo.de> :license: MIT, see license file or https://opensource.org/licenses/MIT :created on: 2018-06-11 18:15:53 """ from typing import Optional, Union, Tuple, Any, Type, Callable, Dict from datetime import datetime import struct from ctypes import memmove, addressof, c_ubyte, Structure, sizeof from collections import OrderedDict from .utils import platform_is_linux from .filetimes import filetime_to_dt from .pyads_ex import ( adsAddRoute, adsAddRouteToPLC, adsDelRoute, adsPortOpenEx, adsPortCloseEx, adsGetLocalAddressEx, adsSyncReadStateReqEx, adsSyncReadDeviceInfoReqEx, adsSyncWriteControlReqEx, adsSyncWriteReqEx, adsSyncReadWriteReqEx2, adsSyncReadReqEx2, adsGetHandle, adsReleaseHandle, adsSyncReadByNameEx, adsSyncWriteByNameEx, adsSyncAddDeviceNotificationReqEx, adsSyncDelDeviceNotificationReqEx, adsSyncSetTimeoutEx, adsSetLocalAddress, ADSError, ) from .constants import ( PLCTYPE_BOOL, PLCTYPE_BYTE, PLCTYPE_DATE, PLCTYPE_DINT, PLCTYPE_DT, PLCTYPE_DWORD, PLCTYPE_INT, PLCTYPE_LREAL, PLCTYPE_REAL, PLCTYPE_SINT, PLCTYPE_STRING, PLCTYPE_TIME, PLCTYPE_TOD, PLCTYPE_UDINT, PLCTYPE_UINT, PLCTYPE_USINT, PLCTYPE_WORD, PLC_DEFAULT_STRING_SIZE, DATATYPE_MAP, ) from .structs import ( AmsAddr, SAmsNetId, AdsVersion, NotificationAttrib, SAdsNotificationHeader, ) linux = platform_is_linux() port = None # type: int def _parse_ams_netid(ams_netid): # type: (str) -> SAmsNetId """Parse an AmsNetId from *str* to *SAmsNetId*. :param str ams_netid: NetId as a string :rtype: SAmsNetId :return: NetId as a struct """ try: id_numbers = list(map(int, ams_netid.split("."))) except ValueError: raise ValueError("no valid netid") if len(id_numbers) != 6: raise ValueError("no valid netid") # Fill the netId struct with data ams_netid_st = SAmsNetId() ams_netid_st.b = (c_ubyte * 6)(*id_numbers) return ams_netid_st def open_port(): # type: () -> int """Connect to the TwinCAT message router. :rtype: int :return: port number """ global port port = port or adsPortOpenEx() return port def close_port(): # type: () -> None """Close the connection to the TwinCAT message router.""" global port if port is not None: adsPortCloseEx(port) port = None def get_local_address(): # type: () -> Optional[AmsAddr] """Return the local AMS-address and the port number. :rtype: AmsAddr """ if port is not None: return adsGetLocalAddressEx(port) return None def set_local_address(ams_netid): # type: (Union[str, SAmsNetId]) -> None """Set the local NetID (**Linux only**). :param str: new AmsNetID :rtype: None **Usage:** >>> import pyads >>> pyads.open_port() >>> pyads.set_local_address('0.0.0.0.1.1') """ if isinstance(ams_netid, str): ams_netid_st = _parse_ams_netid(ams_netid) else: ams_netid_st = ams_netid assert isinstance(ams_netid_st, SAmsNetId) if linux: return adsSetLocalAddress(ams_netid_st) else: raise ADSError( text="SetLocalAddress is not supported for Windows clients." ) # pragma: no cover def read_state(adr): # type: (AmsAddr) -> Optional[Tuple[int, int]] """Read the current ADS-state and the machine-state. Read the current ADS-state and the machine-state from the ADS-server. :param AmsAddr adr: local or remote AmsAddr :rtype: (int, int) :return: adsState, deviceState """ if port is not None: return adsSyncReadStateReqEx(port, adr) return None def write_control(adr, ads_state, device_state, data, plc_datatype): # type: (AmsAddr, int, int, Any, Type) -> None """Change the ADS state and the machine-state of the ADS-server. :param AmsAddr adr: local or remote AmsAddr :param int ads_state: new ADS-state, according to ADSTATE constants :param int device_state: new machine-state :param data: additional data :param int plc_datatype: datatype, according to PLCTYPE constants :note: Despite changing the ADS-state and the machine-state it is possible to send additional data to the ADS-server. For current ADS-devices additional data is not progressed. Every ADS-device is able to communicate its current state to other devices. There is a difference between the device-state and the state of the ADS-interface (AdsState). The possible states of an ADS-interface are defined in the ADS-specification. """ if port is not None: return adsSyncWriteControlReqEx( port, adr, ads_state, device_state, data, plc_datatype ) def read_device_info(adr): # type: (AmsAddr) -> Optional[Tuple[str, AdsVersion]] """Read the name and the version number of the ADS-server. :param AmsAddr adr: local or remote AmsAddr :rtype: string, AdsVersion :return: device name, version """ if port is not None: return adsSyncReadDeviceInfoReqEx(port, adr) return None def write(adr, index_group, index_offset, value, plc_datatype): # type: (AmsAddr, int, int, Any, Type) -> None """Send data synchronous to an ADS-device. :param AmsAddr adr: local or remote AmsAddr :param int index_group: PLC storage area, according to the INDEXGROUP constants :param int index_offset: PLC storage address :param value: value to write to the storage address of the PLC :param Type plc_datatype: type of the data given to the PLC, according to PLCTYPE constants """ if port is not None: return adsSyncWriteReqEx( port, adr, index_group, index_offset, value, plc_datatype ) def read_write( adr, index_group, index_offset, plc_read_datatype, value, plc_write_datatype, return_ctypes=False, ): # type: (AmsAddr, int, int, Type, Any, Type, bool) -> Any """Read and write data synchronous from/to an ADS-device. :param AmsAddr adr: local or remote AmsAddr :param int index_group: PLC storage area, according to the INDEXGROUP constants :param int index_offset: PLC storage address :param Type plc_read_datatype: type of the data given to the PLC to respond to, according to PLCTYPE constants :param value: value to write to the storage address of the PLC :param Type plc_write_datatype: type of the data given to the PLC, according to PLCTYPE constants :param bool return_ctypes: return ctypes instead of python types if True (default: False) :rtype: PLCTYPE :return: value: **value** """ if port is not None: return adsSyncReadWriteReqEx2( port, adr, index_group, index_offset, plc_read_datatype, value, plc_write_datatype, return_ctypes, ) return None def read(adr, index_group, index_offset, plc_datatype, return_ctypes=False): # type: (AmsAddr, int, int, Type, bool) -> Any """Read data synchronous from an ADS-device. :param AmsAddr adr: local or remote AmsAddr :param int index_group: PLC storage area, according to the INDEXGROUP constants :param int index_offset: PLC storage address :param int plc_datatype: type of the data given to the PLC, according to
评论
    相关推荐
    • Python
      Python Boto3代码
    • Python
      python程序
    • Python
      Python
    • python
      Python,就在这里。
    • Python
      Python-
    • Python
      完整的python 其中包含用于学习python的代码,注释和微型项目。
    • Python
      项目Python
    • Python
      Pythonpython3中使用乌龟库的简单pong游戏 贡献 拉请求是欢迎的! :) 执照
    • Python
      Python Dit是Mijn储存库van Python
    • python
      Python 基本的python编程