Package pyads :: Module pyads
[hide private]
[frames] | no frames]

Source Code for Module pyads.pyads

  1  # -*- coding: utf-8 -*- 
  2  ''' 
  3  Created on 19.09.2013 
  4  @author: lehmann 
  5  ''' 
  6   
  7  from ctypes import * 
  8  from constants import * 
  9   
 10  #ADS-DLL laden 
 11  _adsDLL = CDLL("AdsDll.dll") #: ADS-DLL (Beckhoff TwinCAT) 
 12   
 13   
14 -def adsGetDllVersion():
15 ''' 16 @summary: returns version, revision and build of the ads-dll 17 18 @rtype: AdsVersion 19 @return: version, revision and build of the ads-dll 20 ''' 21 #Aufruf der API-Funktion, Rückgabetyp ist Long 22 resLong = c_long(_adsDLL.AdsGetDllVersion()) 23 24 #Struktur-Objekt erstellen 25 stVersion = SAdsVersion() 26 #Speicherbereich des Long-Wertes in die Struktur kopieren 27 fit = min(sizeof(stVersion), sizeof(resLong)) 28 memmove(addressof(stVersion), addressof(resLong), fit) 29 30 return AdsVersion(stVersion.version, stVersion.revision, stVersion.build)
31
32 -def adsPortOpen():
33 """ 34 @summary: connects to the TwinCAT message router 35 @rtype: int 36 @return: port number 37 """ 38 adsPortOpenFct = _adsDLL.AdsPortOpen 39 adsPortOpenFct.restype = c_long 40 41 portNr = adsPortOpenFct() 42 return portNr
43
44 -def adsPortClose():
45 """ 46 @summary: closes the connection to the TwinCAT message router 47 @rtype: int 48 @return: error state 49 """ 50 adsPortCloseFct = _adsDLL.AdsPortClose 51 adsPortCloseFct.restype = c_long 52 53 errCode = adsPortCloseFct() 54 return errCode
55
56 -def adsGetLocalAddress():
57 """ 58 @summary: returns the local AMS-address and the port number 59 @rtype: AmsAddr 60 @return: AMS-address 61 """ 62 adsGetLocalAddressFct = _adsDLL.AdsGetLocalAddress 63 stAmsAddr = SAmsAddr() 64 65 errCode = adsGetLocalAddressFct(pointer(stAmsAddr)) 66 67 if errCode: 68 return None 69 70 adsLocalAddr = AmsAddr(errCode, stAmsAddr) 71 72 return adsLocalAddr
73
74 -def adsSyncReadStateReq(adr):
75 """ 76 @summary: reads the current ADS-state and the machine-state from the ADS-server 77 @type adr: AmsAddr 78 @param adr: local or remote AmsAddr 79 @rtype: (int, int, int) 80 @return: errCode, adsState, deviceState 81 """ 82 adsSyncReadStateReqFct = _adsDLL.AdsSyncReadStateReq 83 84 pAmsAddr = pointer(adr.amsAddrStruct()) 85 adsState = c_int() 86 pAdsState = pointer(adsState) 87 deviceState = c_int() 88 pDeviceState = pointer(deviceState) 89 90 errCode = adsSyncReadStateReqFct(pAmsAddr, pAdsState, pDeviceState) 91 return (errCode, adsState.value, deviceState.value)
92
93 -def adsSyncReadDeviceInfoReq(adr):
94 """ 95 @summary: reads the name and the version-number of the ADS-server 96 @type adr: AmsAddr 97 @param adr: local or remote AmsAddr 98 @rtype: int, string, AdsVersion 99 @return: errCode, device name, version 100 """ 101 adsSyncReadDeviceInfoReqFct = _adsDLL.AdsSyncReadDeviceInfoReq 102 103 pAmsAddr = pointer(adr.amsAddrStruct()) 104 devNameStringBuffer = create_string_buffer(20) 105 pDevName = pointer(devNameStringBuffer) 106 stVersion = SAdsVersion() 107 pVersion = pointer(stVersion) 108 109 errCode = adsSyncReadDeviceInfoReqFct(pAmsAddr, pDevName, pVersion) 110 return (errCode, devNameStringBuffer.value, AdsVersion(stVersion))
111
112 -def adsSyncWriteControlReq(adr, adsState, deviceState, data, plcDataType):
113 """ 114 @summary: changes the ads-state and the machine-state of the ADS-server 115 116 @type adr: AmsAddr 117 @param adr: local or remote AmsAddr 118 119 @type adsState: int 120 @param adsState: new ADS-state, according to ADSTATE constants 121 122 @type deviceState: int 123 @param deviceState: new machine-state 124 125 @param data: additional data 126 127 @type plcDataType: int 128 @param plcDataType: PLC-datatype, according to PLCTYPE constants 129 130 @rtype: int 131 @return: error-state of the function 132 133 @note: Despite changing the ADS-state and the machine-state it is possible to send additional 134 data to the ADS-server. For current ADS-devices additional data is not progressed. 135 Every ADS-device is able to communicate its current state to other devices. There is a difference 136 between the device-state and the state of the ADS-interface (AdsState). The possible states of an 137 ADS-interface are defined in the ADS-specification. 138 """ 139 adsSyncWriteControlReqFct = _adsDLL.AdsSyncWriteControlReq 140 141 pAddr = pointer(adr.amsAddrStruct()) 142 nAdsState = c_ulong(adsState) 143 nDeviceState = c_ulong(deviceState) 144 145 if plcDataType == PLCTYPE_STRING: 146 nData = c_char_p(data) 147 pData = nData 148 nLength = len(pData.value)+1 149 else: 150 nData = plcDataType(data) 151 pData = pointer(nData) 152 nLength = sizeof(nData) 153 154 errCode = adsSyncWriteControlReqFct(pAddr, nAdsState, nDeviceState, nLength, pData) 155 return errCode
156
157 -def adsSyncWriteReq(adr, indexGroup, indexOffset, value, plcDataType):
158 """ 159 @summary: sends data synchronous to an ADS-device 160 161 @type adr: AmsAddr 162 @param adr: local or remote AmsAddr 163 164 @type indexGroup: int 165 @param indexGroup: PLC storage area, according to the INDEXGROUP constants 166 167 @type indexOffset: int 168 @param indexOffset: PLC storage address 169 170 @param value: value to write to the storage address of the PLC 171 172 @type plcDataType: int 173 @param plcDataType: type of the data given to the PLC, according to PLCTYPE constants 174 175 @rtype: int 176 @return: error-state of the function 177 """ 178 179 adsSyncWriteReqFct = _adsDLL.AdsSyncWriteReq 180 181 pAmsAddr = pointer(adr.amsAddrStruct()) 182 nIndexGroup = c_ulong(indexGroup) 183 nIndexOffset = c_ulong(indexOffset) 184 185 if plcDataType == PLCTYPE_STRING: 186 nData = c_char_p(value) 187 pData = nData 188 nLength = len(pData.value)+1 189 else: 190 nData = plcDataType(value) 191 pData = pointer(nData) 192 nLength = sizeof(nData) 193 194 195 errCode = adsSyncWriteReqFct(pAmsAddr, nIndexGroup, nIndexOffset, nLength, pData) 196 return errCode
197
198 -def adsSyncReadReq(adr, indexGroup, indexOffset, plcDataType):
199 """ 200 @summary: reads data synchronous from an ADS-device 201 202 @type adr: AmsAddr 203 @param adr: local or remote AmsAddr 204 205 @type indexGroup: int 206 @param indexGroup: PLC storage area, according to the INDEXGROUP constants 207 208 @type indexOffset: int 209 @param indexOffset: PLC storage address 210 211 @type plcDataType: int 212 @param plcDataType: type of the data given to the PLC, according to PLCTYPE constants 213 214 @rtype: (int, PLCTYPE) 215 @return: (errCode, value): B{errCode} error-state of the function, B{value} 216 """ 217 218 adsSyncReadReqFct = _adsDLL.AdsSyncReadReq 219 220 pAmsAddr = pointer(adr.amsAddrStruct()) 221 nIndexGroup = c_ulong(indexGroup) 222 nIndexOffset = c_ulong(indexOffset) 223 224 data = plcDataType() 225 pData = pointer(data) 226 nLength = c_ulong(sizeof(data)) 227 errCode = adsSyncReadReqFct(pAmsAddr, nIndexGroup, nIndexOffset, nLength, pData) 228 229 return (errCode, data.value)
230 231 ''' 232 def adsSyncAddDeviceNotificationReq(adr, indexGroup, indexOffset, noteAttrib, noteFunc, user, notification): 233 adsSyncAddDeviceNotificationReq = _adsDLL.AdsSyncAddDeviceNotificationReq 234 235 pAmsAddr = pointer(adr.amsAddrStruct()) 236 nIndexGroup = c_ulong(indexGroup) 237 nIndexOffset = c_ulong(indexOffset) 238 #pNoteAttrib = 239 ''' 240