Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/MrLeeh/pyads
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLeeh authored and MrLeeh committed Jul 23, 2015
2 parents b3c94f4 + c2b09f3 commit 5435576
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
31 changes: 31 additions & 0 deletions pyads/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@
#PLCADS_IGR_RDSIZE
INDEXGROUP_DATASIZE = 0x4045 #:size of the data area in bytes


ADSIGRP_SYMTAB = 0xF000
ADSIGRP_SYMNAME = 0xF001
ADSIGRP_SYMVAL = 0xF002

ADSIGRP_SYM_HNDBYNAME = 0xF003
ADSIGRP_SYM_VALBYNAME = 0xF004
ADSIGRP_SYM_VALBYHND = 0xF005
ADSIGRP_SYM_RELEASEHND = 0xF006
ADSIGRP_SYM_INFOBYNAME = 0xF007
ADSIGRP_SYM_VERSION = 0xF008
ADSIGRP_SYM_INFOBYNAMEEX = 0xF009

ADSIGRP_SYM_DOWNLOAD = 0xF00A
ADSIGRP_SYM_UPLOAD = 0xF00B
ADSIGRP_SYM_UPLOADINFO = 0xF00C

ADSIGRP_SYMNOTE = 0xF010 #notification of named handle

ADSIGRP_IOIMAGE_RWIB = 0xF020 #read/write input byte(s)
ADSIGRP_IOIMAGE_RWIX = 0xF021 #read/write input bit
ADSIGRP_IOIMAGE_RWOB = 0xF030 #read/write output byte(s)
ADSIGRP_IOIMAGE_RWOX = 0xF031 #read/write output bit
ADSIGRP_IOIMAGE_CLEARI = 0xF040 #write inputs to null
ADSIGRP_IOIMAGE_CLEARO = 0xF050 #write outputs to null

ADSIGRP_DEVICE_DATA = 0xF100 #state, name, etc...
ADSIOFFS_DEVDATA_ADSSTATE = 0x0000 #ads state of device
ADSIOFFS_DEVDATA_DEVSTATE = 0x0002 #device state


#PORTS
PORT_LOGGER = 100
PORT_EVENTLOGGER = 110
Expand Down
50 changes: 49 additions & 1 deletion pyads/pyads.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


# load dynamic ADS library
_adsDLL = CDLL("TcAdsDll.dll") #: ADS-DLL (Beckhoff TwinCAT)
_adsDLL = windll.TcAdsDll #: ADS-DLL (Beckhoff TwinCAT)


def adsGetDllVersion():
Expand Down Expand Up @@ -268,3 +268,51 @@ def adsSyncReadReq(adr, indexGroup, indexOffset, plcDataType):
else:
## if we return structures, they may not have a value attribute
return (errCode, data)

def adsSyncReadByName(adr, dataName, plcDataType):
"""
:summary: Read data synchronous from an ADS-device from data name
:param pyads.structs.AmsAddr adr: local or remote AmsAddr
:param string dataName: data name
:param int plcDataType: type of the data given to the PLC, according to
PLCTYPE constants
:rtype: (int, PLCTYPE)
:return: (errCode, value): **errCode** error-state of the function,
**value**
"""
#Get the handle of the PLC-variable
(err_code, hnl) = adsSyncReadWriteReq(adr, ADSIGRP_SYM_HNDBYNAME, 0x0, PLCTYPE_UDINT, dataName, PLCTYPE_STRING )
if err_code :
return (err_code, 0)
#Read the value of a PLC-variable, via handle
(err_code,value) = adsSyncReadReq(adr, ADSIGRP_SYM_VALBYHND, hnl, plcDataType )
if err_code :
return (err_code,0)
#Release the handle of the PLC-variable
err_code = adsSyncWriteReq(adr, ADSIGRP_SYM_RELEASEHND, 0, hnl, PLCTYPE_UDINT )
return ( err_code, value )

def adsSyncWriteByName(adr, dataName, value, plcDataType):
"""
:summary: Send data synchronous to an ADS-device from data name
:param pyads.structs.AmsAddr adr: local or remote AmsAddr
:param string dataName: PLC storage address
:param value: value to write to the storage address of the PLC
:param int plcDataType: type of the data given to the PLC,
according to PLCTYPE constants
:rtype: int
:return: error-state of the function
"""
#Get the handle of the PLC-variable
(err_code, hnl) = adsSyncReadWriteReq(adr, ADSIGRP_SYM_HNDBYNAME, 0x0, PLCTYPE_UDINT, dataName, PLCTYPE_STRING )
if err_code :
return err_code
#Read the value of a PLC-variable, via handle
err_code = adsSyncWriteReq(adr, ADSIGRP_SYM_VALBYHND, hnl, value, plcDataType )
if err_code :
return err_code
#Release the handle of the PLC-variable
return adsSyncWriteReq(adr, ADSIGRP_SYM_RELEASEHND, 0, hnl, PLCTYPE_UDINT )

0 comments on commit 5435576

Please sign in to comment.