1
2 '''
3 Created on 19.09.2013
4 @author: lehmann
5 '''
6 from constants import INDEXGROUP_MEMORYBIT, PLCTYPE_BOOL, INDEXGROUP_MEMORYBYTE
7 from pyads import adsSyncWriteReq, adsSyncReadReq
8
10 '''
11 @summary: Objects of this class represent a plc process value. The class accomblishes a connection between
12 plc and the gui. For implementing the interaction between gui objects and plc values subclass and implement
13 mapAdsToGui according to the given examples.
14
15 B{sample code:}
16
17 >>> class TextBoxMapper (ADSMapper):
18 >>> def mapAdsToGui(self, guiObject, value):
19 >>> guiObject.setText(str(value))
20
21 >>> class ComboBoxMapper (ADSMapper):
22 >>> def mapAdsToGui(self, guiObject, value):
23 >>> index = guiObject.findData(QVariant(value))
24 >>> guiObject.setCurrentIndex(index)
25
26 >>> class DSpinBoxMapper (ADSMapper):
27 >>> def mapAdsToGui(self, guiObject, value):
28 >>> guiObject.setValue(float(value))
29
30 >>> class SpinBoxMapper (ADSMapper):
31 >>> def mapAdsToGui(self, guiObject, value):
32 >>> guiObject.setValue(int(value))
33
34 >>> class BinaryMapper (ADSMapper):
35 >>> def mapAdsToGui(self, guiObject, value):
36 >>> guiObject.setText(__builtin__.bin(int(value)))
37
38 @version: 1.0.0
39
40 '''
41 - def __init__(self, plcAddress, plcDataType, guiObjects, hint=None):
42 '''
43 @type plcAddress: int
44 @param plcAddress: plc address
45 @type plcDataType: int
46 @param plcDataType: plc data type
47 @param guiObjects: list/tuple or single gui objects (for instance Qt objects)
48 @type hint: string
49 '''
50 self.hint = hint
51 self.plcAdr = plcAddress
52 self.plcDataType = plcDataType
53 self.currentValue = None
54 self.guiObjects = guiObjects
55
56 if isinstance(guiObjects, (list, tuple)):
57 for o in guiObjects:
58 o.plcObject = self
59 else:
60 guiObjects.plcObject = self
61
62 - def write (self, adsAdr, value):
63 '''
64 writes the value to the plc address
65
66 @type adsAdr: adsPy.AmsAdr
67 @param adsAdr: address to the ADS device
68
69 @param value: value to be written
70 '''
71 self.currentValue = value
72 indexgroup = INDEXGROUP_MEMORYBIT if self.plcDataType == PLCTYPE_BOOL else INDEXGROUP_MEMORYBYTE
73
74
75 err = adsSyncWriteReq(adsAdr, indexgroup, self.plcAdr, self.currentValue, self.plcDataType)
76
77 if err == 0:
78 return
79
80 raise Exception("error writing on address %i. error number %i" % (self.plcAdr, err) )
81
82 - def read(self, adsAdr):
83 '''
84 reads from plc address and writes in self.currentValue, calls mapAdsToGui to show the value on
85 the connected gui objects
86
87 @type adsAdr: adsPy.AmsAdr
88 @param adsAdr: address to the ADS device
89
90 @return: current value
91 '''
92 indexgroup = INDEXGROUP_MEMORYBIT if self.plcDataType == PLCTYPE_BOOL else INDEXGROUP_MEMORYBYTE
93
94 (err, value) = adsSyncReadReq(adsAdr, indexgroup, self.plcAdr, self.plcDataType)
95
96 if err:
97 raise Exception("error reading from address %i (%s). error number %i" % (self.plcAdr, self.name, err))
98
99
100 if isinstance(self.guiObjects, (list, tuple)):
101 for o in self.guiObjects:
102 self.mapAdsToGui(o, value)
103 else:
104 self.mapAdsToGui(self.guiObjects, value)
105
106 self.currentValue = value
107 return value
108
110 '''
111 @summary: displays the value on the connected gui object, this function should be overriden, by default the value is printed on the console.
112
113 @type guiObject: QObject
114 @param guiObject: gui object for value output
115
116 @param value: value to display in the gui object
117
118 '''
119 print value
120