22 #License along with this library; if not, write to the Free Software |
22 #License along with this library; if not, write to the Free Software |
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
24 |
24 |
25 from LPCProto import * |
25 from LPCProto import * |
26 |
26 |
27 |
|
28 |
|
29 class LPCObject(): |
27 class LPCObject(): |
30 def __init__(self, confnodesroot, comportstr): |
28 def __init__(self, confnodesroot, comportstr): |
|
29 self.TransactionLock = Lock() |
31 self.PLCStatus = "Disconnected" |
30 self.PLCStatus = "Disconnected" |
32 self.confnodesroot = confnodesroot |
31 self.confnodesroot = confnodesroot |
33 self.PLCprint = confnodesroot.logger.writeyield |
32 self.PLCprint = confnodesroot.logger.writeyield |
34 self._Idxs = [] |
33 self._Idxs = [] |
35 comport = int(comportstr[3:]) - 1 |
|
36 try: |
34 try: |
37 self.connect(comportstr) |
35 self.connect(comportstr) |
38 except Exception,e: |
36 except Exception,e: |
39 self.confnodesroot.logger.write_error(str(e)+"\n") |
37 self.confnodesroot.logger.write_error(str(e)+"\n") |
40 self.SerialConnection = None |
38 self.SerialConnection = None |
41 self.PLCStatus = "Disconnected" |
39 self.PLCStatus = "Disconnected" |
42 |
40 |
43 def HandleSerialTransaction(self, transaction): |
41 def HandleSerialTransaction(self, transaction): |
|
42 res = None |
|
43 disconnected=False |
|
44 failure=None |
|
45 self.TransactionLock.acquire() |
44 if self.SerialConnection is not None: |
46 if self.SerialConnection is not None: |
45 try: |
47 try: |
46 self.PLCStatus, res = self.SerialConnection.HandleTransaction(transaction) |
48 self.PLCStatus, res = \ |
47 return res |
49 self.SerialConnection.HandleTransaction(transaction) |
48 except LPCProtoError,e: |
50 except LPCProtoError,e: |
49 self.confnodesroot.logger.write(_("PLC disconnected\n")) |
51 disconnected=True |
50 if self.SerialConnection is not None: |
52 if self.SerialConnection is not None: |
51 self.SerialConnection.close() |
53 self.SerialConnection.close() |
52 self.SerialConnection = None |
54 self.SerialConnection = None |
53 self.PLCStatus = "Disconnected" |
55 self.PLCStatus = "Disconnected" |
54 return None |
|
55 except Exception,e: |
56 except Exception,e: |
56 self.confnodesroot.logger.write_warning(str(e)+"\n") |
57 failure = str(e) |
|
58 self.TransactionLock.release() |
|
59 if disconnected: |
|
60 self.confnodesroot.logger.write(_("PLC disconnected\n")) |
|
61 if failure is not None: |
|
62 self.confnodesroot.logger.write_warning(failure+"\n") |
|
63 return res |
57 |
64 |
58 def StartPLC(self, debug=False): |
65 def StartPLC(self, debug=False): |
59 raise LPCProtoError("Not implemented") |
66 raise LPCProtoError("Not implemented") |
60 |
67 |
61 def StopPLC(self): |
68 def StopPLC(self): |