equal
deleted
inserted
replaced
4 import time |
4 import time |
5 from threading import Lock |
5 from threading import Lock |
6 |
6 |
7 MAX_PACKET_SIZE=64 |
7 MAX_PACKET_SIZE=64 |
8 |
8 |
9 LPC_STATUS=dict(STARTED = 0x01, |
9 LPC_STATUS={0x01 : "Started", |
10 STOPPED = 0x02, |
10 0x02 : "Stopped"} |
11 DEBUG = 0x03) |
|
12 |
11 |
13 class LPCError(exceptions.Exception): |
12 class LPCError(exceptions.Exception): |
14 """Exception class""" |
13 """Exception class""" |
15 def __init__(self, msg): |
14 def __init__(self, msg): |
16 self.msg = msg |
15 self.msg = msg |
38 def flush(self_): |
37 def flush(self_): |
39 self._serialPort.flush() |
38 self._serialPort.flush() |
40 self.serialPort = myser() |
39 self.serialPort = myser() |
41 # start with empty |
40 # start with empty |
42 self.serialPort.flush() |
41 self.serialPort.flush() |
43 # handshake |
|
44 self.HandleTransaction(IDLETransaction()) |
|
45 |
42 |
46 def HandleTransaction(self, transaction): |
43 def HandleTransaction(self, transaction): |
47 self.TransactionLock.acquire() |
44 self.TransactionLock.acquire() |
48 try: |
45 try: |
49 transaction.SetPseudoFile(self.serialPort) |
46 transaction.SetPseudoFile(self.serialPort) |
54 res = transaction.ExchangeData() |
51 res = transaction.ExchangeData() |
55 else: |
52 else: |
56 raise LPCError("LPC transaction error - controller did not answer as expected") |
53 raise LPCError("LPC transaction error - controller did not answer as expected") |
57 finally: |
54 finally: |
58 self.TransactionLock.release() |
55 self.TransactionLock.release() |
59 return current_plc_status, res |
56 return LPC_STATUS.get(current_plc_status,"Broken"), res |
60 |
57 |
61 class LPCTransaction: |
58 class LPCTransaction: |
62 def __init__(self, command, optdata = ""): |
59 def __init__(self, command, optdata = ""): |
63 self.Command = command |
60 self.Command = command |
64 self.OptData = optdata |
61 self.OptData = optdata |
131 class GET_TRACE_VARIABLETransaction(LPCTransaction): |
128 class GET_TRACE_VARIABLETransaction(LPCTransaction): |
132 def __init__(self): |
129 def __init__(self): |
133 LPCTransaction.__init__(self, 0x05) |
130 LPCTransaction.__init__(self, 0x05) |
134 ExchangeData = LPCTransaction.GetData |
131 ExchangeData = LPCTransaction.GetData |
135 |
132 |
136 class SET_FORCED_VARIABLETransaction(LPCTransaction): |
|
137 def __init__(self, data): |
|
138 LPCTransaction.__init__(self, 0x06, data) |
|
139 ExchangeData = LPCTransaction.SendData |
|
140 |
|
141 class GET_PLCIDTransaction(LPCTransaction): |
133 class GET_PLCIDTransaction(LPCTransaction): |
142 def __init__(self): |
134 def __init__(self): |
143 LPCTransaction.__init__(self, 0x07) |
135 LPCTransaction.__init__(self, 0x07) |
144 ExchangeData = LPCTransaction.GetData |
136 ExchangeData = LPCTransaction.GetData |
145 |
137 |
148 # TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
140 # TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
149 TestConnection.HandleTransaction(STARTTransaction()) |
141 TestConnection.HandleTransaction(STARTTransaction()) |
150 # TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
142 # TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
151 # "\x03\x00\x00\x00"*200)) |
143 # "\x03\x00\x00\x00"*200)) |
152 # TestConnection.HandleTransaction(STARTTransaction()) |
144 # TestConnection.HandleTransaction(STARTTransaction()) |
153 TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
145 while True: |
154 "\x01\x00\x00\x00"+ |
146 TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
155 "\x04"+ |
147 "\x01\x00\x00\x00"+ |
156 "\x01\x02\x02\x04"+ |
148 "\x04"+ |
157 "\x01\x00\x00\x00"+ |
149 "\x01\x02\x02\x04"+ |
158 "\x08"+ |
150 "\x01\x00\x00\x00"+ |
159 "\x01\x02\x02\x04"+ |
151 "\x08"+ |
160 "\x01\x02\x02\x04"+ |
152 "\x01\x02\x02\x04"+ |
161 "\x01\x00\x00\x00"+ |
153 "\x01\x02\x02\x04"+ |
162 "\x04"+ |
154 "\x01\x00\x00\x00"+ |
163 "\x01\x02\x02\x04")) |
155 "\x04"+ |
|
156 "\x01\x02\x02\x04")) |
164 #status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
157 #status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
165 #print len(res) |
158 #print len(res) |
166 #print "GOT : ", map(hex, map(ord, res)) |
159 #print "GOT : ", map(hex, map(ord, res)) |
167 #TestConnection.HandleTransaction(STOPTransaction()) |
160 #TestConnection.HandleTransaction(STOPTransaction()) |