##// END OF EJS Templates
changes with frame corrected
imanay -
r108:109
parent child
Show More
@@ -1,529 +1,531
1 # Needed for the FilesStuff class
1 # Needed for the FilesStuff class
2 import os
2 import os
3 import numpy as np
3 import numpy as np
4 # Needed for the TCPComm class
4 # Needed for the TCPComm class
5 import socket
5 import socket
6
6
7 class TCPComm:
7 class TCPComm:
8
8
9 __HEADER = "JRO"
9 __HEADER = "JRO"
10 __TYPE = "ABS"
10 __TYPE = "ABS"
11
11
12 def __init__(self, ipSource, iDSource, ipDestino, iDDestino, portDestino, asServer=False):
12 def __init__(self, ipSource, iDSource, ipDestino, iDDestino, portDestino, asServer=False):
13
13
14 self.ipSource = ipSource
14 self.ipSource = ipSource
15 self.iDSource = iDSource
15 self.iDSource = iDSource
16 self.ipDestino = ipDestino
16 self.ipDestino = ipDestino
17 self.iDDestino = iDDestino
17 self.iDDestino = iDDestino
18 self.portDestino = portDestino
18 self.portDestino = portDestino
19 self.addr = (ipDestino,portDestino)
19 self.addr = (ipDestino,portDestino)
20
20
21 self.sc = "none"
21 self.sc = "none"
22 self.answer = "none" #test
22 self.answer = "none" #test
23 self.asServer = False
23 self.asServer = False
24 self.len = "000000"
24 self.len = "000000"
25 self.crc = "0"
25 self.crc = "0"
26
26
27 self.openSocket(asServer)
27 self.openSocket(asServer)
28
28
29 def openSocket(self, asServer):
29 def openSocket(self, asServer):
30
30
31 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
31 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
32 # self.socket_c = socket.socket()
32 # self.socket_c = socket.socket()
33 # self.socket_c.connect((self.ipDestino, self.portDestino))
33 # self.socket_c.connect((self.ipDestino, self.portDestino))
34
34
35 if asServer:
35 if asServer:
36 self.socket_c = socket.socket()
36 self.socket_c = socket.socket()
37 # self.configAsServer()
37 # self.configAsServer()
38 self.socket_c.bind(self.addr)
38 self.socket_c.bind(self.addr)
39 self.asServer = True
39 self.asServer = True
40 else:
40 else:
41 # self.configAsClient()
41 # self.configAsClient()
42 self.asServer = False #Socket is opened at the sendData function
42 self.asServer = False #Socket is opened at the sendData function
43
43
44 # def configAsClient(self):
44 # def configAsClient(self):
45 #Buscar broadcast TCP
45 #Buscar broadcast TCP
46 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
46 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
47 # self.socket_c.connect(self.addr)
47 # self.socket_c.connect(self.addr)
48 # pass
48 # pass
49
49
50 # def configAsServer(self):
50 # def configAsServer(self):
51 #
51 #
52 # self.socket_c.bind(self.addr)
52 # self.socket_c.bind(self.addr)
53
53
54 def waitData(self):
54 def waitData(self):
55 if self.asServer == True:
55 if self.asServer == True:
56 trama_rx, l = self.waitAsServer()
56 trama_rx, l = self.waitAsServer()
57 else:
57 else:
58 trama_rx, l = self.waitAsClient()
58 trama_rx, l = self.waitAsClient()
59
59
60 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx, l)
60 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx, l)
61
61
62 return ipSource, ipDestino, cmd, data
62 return ipSource, ipDestino, cmd, data
63
63
64 def waitAsClient2(self, nbytes = 1024):
64 def waitAsClient2(self, nbytes = 1024):
65 print "\nWaiting the server."
65 print "\nWaiting the server."
66 # Short data through ethernet
66 # Short data through ethernet
67 trama_rx = self.socket_c.recv(nbytes)
67 trama_rx = self.socket_c.recv(nbytes)
68 print "\nThis socket has received this data: " + str(trama_rx)
68 print "\nThis socket has received this data: " + str(trama_rx)
69
69
70 return trama_rx
70 return trama_rx
71
71
72 def waitAsServer2(self, nbytes = 1024):
72 def waitAsServer2(self, nbytes = 1024):
73 print "\nWaiting some client."
73 print "\nWaiting some client."
74 self.socket_c.listen(1)
74 self.socket_c.listen(1)
75 sc, addr = self.socket_c.accept()
75 sc, addr = self.socket_c.accept()
76 self.sc = sc
76 self.sc = sc
77 self.answer = addr
77 self.answer = addr
78 # Big data through ethernet
78 # Big data through ethernet
79 trama_rx = ""
79 trama_rx = ""
80 while True:
80 while True:
81 tmp = self.sc.recv(nbytes)
81 tmp = self.sc.recv(nbytes)
82 trama_rx = trama_rx + tmp
82 trama_rx = trama_rx + tmp
83 if trama_rx[-4:] == "quit":
83 if trama_rx[-4:] == "quit":
84 break
84 break
85
85
86 print "\nThis socket has received some data from: " + str(self.answer)
86 print "\nThis socket has received some data from: " + str(self.answer)
87
87
88 return trama_rx
88 return trama_rx
89
89
90 def waitAsServer(self, nbytes = 1024):
90 def waitAsServer(self, nbytes = 1024):
91 print "\nWaiting some client."
91 print "\nWaiting some client."
92 self.socket_c.listen(1)
92 self.socket_c.listen(1)
93 sc, addr = self.socket_c.accept()
93 sc, addr = self.socket_c.accept()
94 self.sc = sc
94 self.sc = sc
95 self.answer = addr
95 self.answer = addr
96 # Big data through ethernet
96 # Big data through ethernet
97 cnt = 0;
97 cnt = 0;
98 first = 0;
98 first = 0;
99 trama_rx = ""
99 trama_rx = ""
100 while True:
100 while True:
101 tmp = self.sc.recv(nbytes)
101 tmp = self.sc.recv(nbytes)
102 trama_rx = trama_rx + tmp
102 trama_rx = trama_rx + tmp
103 cnt = len(trama_rx)
103 cnt = len(trama_rx)
104 if first == 0:
104 if first == 0:
105 first = 1
105 first = 1
106 lng = int(trama_rx[20:26])
106 lng = int(trama_rx[20:26])
107 print str(lng)
107 if cnt == lng:
108 if cnt == lng:
108 break
109 break
109
110
110 print "\nThis socket has received some data from: " + str(self.answer)
111 print "\nThis socket has received some data from: " + str(self.answer)
111
112
112 return trama_rx, lng
113 return trama_rx, lng
113
114
114 def waitAsClient(self, nbytes = 1024):
115 def waitAsClient(self, nbytes = 1024):
115 print "\nWaiting the server."
116 print "\nWaiting the server."
116 # Short data through ethernet
117 # Short data through ethernet
117 trama_rx = self.socket_c.recv(nbytes)
118 trama_rx = self.socket_c.recv(nbytes)
118 lng = int(trama_rx[20:26])
119 lng = int(trama_rx[20:26])
120 print str(lng)
119 print "\nThis socket has received this data: " + str(trama_rx)
121 print "\nThis socket has received this data: " + str(trama_rx)
120
122
121 return trama_rx, lng
123 return trama_rx, lng
122
124
123 def sendData2(self, cmd, data, ipDestino):
125 def sendData2(self, cmd, data, ipDestino):
124
126
125 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \
127 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \
126 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit"
128 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit"
127
129
128 if self.asServer == True:
130 if self.asServer == True:
129 self.SendAsServer(trama_tx)
131 self.SendAsServer(trama_tx)
130 else:
132 else:
131 self.SendAsClient(ipDestino, trama_tx)
133 self.SendAsClient(ipDestino, trama_tx)
132
134
133 def sendData(self, cmd, data, ipDestino):
135 def sendData(self, cmd, data, ipDestino):
134
136
135 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
137 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
136 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
138 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
137
139
138 self.len = self.int2str(len(trama_tx))
140 self.len = self.int2str(len(trama_tx))
139
141
140 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
142 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
141 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
143 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
142
144
143 print trama_tx
145 print trama_tx
144
146
145 if self.asServer == True:
147 if self.asServer == True:
146 self.SendAsServer(trama_tx)
148 self.SendAsServer(trama_tx)
147 else:
149 else:
148 self.SendAsClient(ipDestino, trama_tx)
150 self.SendAsClient(ipDestino, trama_tx)
149
151
150 def SendAsServer(self, trama_tx):
152 def SendAsServer(self, trama_tx):
151
153
152 self.sc.send(trama_tx)
154 self.sc.send(trama_tx)
153 print "Sending message:[" + trama_tx + "] to: " + str(self.answer)
155 print "Sending message:[" + trama_tx + "] to: " + str(self.answer)
154
156
155
157
156 def SendAsClient(self, ipDestino, trama_tx):
158 def SendAsClient(self, ipDestino, trama_tx):
157
159
158 self.socket_c.connect((ipDestino, self.portDestino))
160 self.socket_c.connect((ipDestino, self.portDestino))
159 self.socket_c.send(trama_tx)
161 self.socket_c.send(trama_tx)
160 print "Sending message:[" + trama_tx + "] to: " + ipDestino
162 print "Sending message:[" + trama_tx + "] to: " + ipDestino
161
163
162 def __getTrama2(self, trama):
164 def __getTrama2(self, trama):
163
165
164 FrameList = trama.split(':')
166 FrameList = trama.split(':')
165
167
166 header = FrameList[0]
168 header = FrameList[0]
167 TypeOfInstrument = FrameList[1]
169 TypeOfInstrument = FrameList[1]
168 ipSource = FrameList[2]
170 ipSource = FrameList[2]
169 ipDestino = FrameList[3]
171 ipDestino = FrameList[3]
170 len = FrameList[4]
172 len = FrameList[4]
171 cmd = FrameList[5]
173 cmd = FrameList[5]
172 data = FrameList[6]
174 data = FrameList[6]
173 crc = FrameList[7]
175 crc = FrameList[7]
174 trash = FrameList[8]
176 trash = FrameList[8]
175
177
176 return ipSource, ipDestino, cmd, data
178 return ipSource, ipDestino, cmd, data
177
179
178 def __getTrama(self, trama, l):
180 def __getTrama(self, trama, l):
179
181
180 header = trama[0:3]
182 header = trama[0:3]
181 TypeOfInstrument = trama[3:6]
183 TypeOfInstrument = trama[3:6]
182 ipSource = trama[6:13]
184 ipSource = trama[6:13]
183 ipDestino = trama[13:20]
185 ipDestino = trama[13:20]
184 len = trama[20:26]
186 len = trama[20:26]
185 cmd = trama[26:30]
187 cmd = trama[26:30]
186 ldata = l-31
188 ldata = l-31
187 data = trama[30:30+ldata]
189 data = trama[30:30+ldata]
188 crc = trama[30+ldata:]
190 crc = trama[30+ldata:]
189
191
190 return ipSource, ipDestino, cmd, data
192 return ipSource, ipDestino, cmd, data
191
193
192 def close_socket(self):
194 def close_socket(self):
193 self.socket_c.close()
195 self.socket_c.close()
194
196
195 def open_socket(self):
197 def open_socket(self):
196 self.socket_c = socket.socket()
198 self.socket_c = socket.socket()
197
199
198 def int2str(self, n):
200 def int2str(self, n):
199
201
200 str_n = str(n)
202 str_n = str(n)
201 l_n = len(str_n)
203 l_n = len(str_n)
202 if l_n == 1:
204 if l_n == 1:
203 str_n = "00000" + str_n
205 str_n = "00000" + str_n
204 elif l_n == 2:
206 elif l_n == 2:
205 str_n = "0000" + str_n
207 str_n = "0000" + str_n
206 elif l_n == 3:
208 elif l_n == 3:
207 str_n = "000" + str_n
209 str_n = "000" + str_n
208 elif l_n == 4:
210 elif l_n == 4:
209 str_n = "00" + str_n
211 str_n = "00" + str_n
210 elif l_n == 5:
212 elif l_n == 5:
211 str_n = "0" + str_n
213 str_n = "0" + str_n
212 return str_n
214 return str_n
213
215
214 class FilesStuff():
216 class FilesStuff():
215
217
216 def lst2string(self, lst):
218 def lst2string(self, lst):
217 string=''
219 string=''
218 for i in lst:
220 for i in lst:
219 string=string+i
221 string=string+i
220 return string
222 return string
221
223
222 def string2lst(self, string):
224 def string2lst(self, string):
223 lst = []
225 lst = []
224 for i in string:
226 for i in string:
225 lst.append(i)
227 lst.append(i)
226 return lst
228 return lst
227
229
228
230
229 def file1(self, filename, type):
231 def file1(self, filename, type):
230 lst = self.string2lst(filename)
232 lst = self.string2lst(filename)
231 fin = -1
233 fin = -1
232 t = len(lst)
234 t = len(lst)
233 for i in np.arange(-1,-t,-1):
235 for i in np.arange(-1,-t,-1):
234 if lst[i]=='/':
236 if lst[i]=='/':
235 fin=i
237 fin=i
236 break
238 break
237 if type == '1':
239 if type == '1':
238 nombre2 = lst[fin+1:]
240 nombre2 = lst[fin+1:]
239 nombre2[-1]='s'
241 nombre2[-1]='s'
240 nombre2 = self.lst2string(nombre2)
242 nombre2 = self.lst2string(nombre2)
241 return nombre2
243 return nombre2
242 if type == '2':
244 if type == '2':
243 nombre2 = lst[fin+1:]
245 nombre2 = lst[fin+1:]
244 nombre2[-1]='1'
246 nombre2[-1]='1'
245 nombre2 = self.lst2string(nombre2)
247 nombre2 = self.lst2string(nombre2)
246 return nombre2
248 return nombre2
247
249
248
250
249 def EliminaSaltoDeLinea(self,cadena):
251 def EliminaSaltoDeLinea(self,cadena):
250 i = 0
252 i = 0
251 for elemento in cadena:
253 for elemento in cadena:
252 if elemento =='\n' or elemento =='\r':
254 if elemento =='\n' or elemento =='\r':
253 pass
255 pass
254 else:
256 else:
255 i=i+1
257 i=i+1
256 return cadena [:i]
258 return cadena [:i]
257
259
258 def NumeroDeExperimentos(self, path):
260 def NumeroDeExperimentos(self, path):
259 fichero1=open(path,'r')
261 fichero1=open(path,'r')
260 cont=0
262 cont=0
261 for cadena in fichero1:
263 for cadena in fichero1:
262 cont=cont+1
264 cont=cont+1
263 if cont==3:
265 if cont==3:
264 nexp=''
266 nexp=''
265 pos=0
267 pos=0
266 for elemento in cadena:
268 for elemento in cadena:
267 pos=pos+1
269 pos=pos+1
268 if elemento=='=':
270 if elemento=='=':
269 nexp=int(cadena[pos:])
271 nexp=int(cadena[pos:])
270 return nexp
272 return nexp
271 fichero1.close()
273 fichero1.close()
272
274
273 def Paridad(numero):
275 def Paridad(numero):
274 if numero%2==0: return 'par'
276 if numero%2==0: return 'par'
275 elif numero%2==1: return 'impar'
277 elif numero%2==1: return 'impar'
276
278
277 def EvaluaCadena(self,cadena):
279 def EvaluaCadena(self,cadena):
278 if len(cadena)>35:
280 if len(cadena)>35:
279 if cadena[-1]=='$':
281 if cadena[-1]=='$':
280 return cadena[-35:-2]
282 return cadena[-35:-2]
281 elif cadena[-1]==']':
283 elif cadena[-1]==']':
282 return cadena[-34:-1]
284 return cadena[-34:-1]
283 else:
285 else:
284 return None
286 return None
285
287
286 def GuardaEnLista(self,path):
288 def GuardaEnLista(self,path):
287 fichero=open(path,'r')
289 fichero=open(path,'r')
288 lista=[]
290 lista=[]
289 for cadena in fichero:
291 for cadena in fichero:
290 cadena = self.EliminaSaltoDeLinea(cadena)
292 cadena = self.EliminaSaltoDeLinea(cadena)
291 cadena = self.EvaluaCadena(cadena)
293 cadena = self.EvaluaCadena(cadena)
292 if cadena != None:
294 if cadena != None:
293 lista.append(cadena)
295 lista.append(cadena)
294 fichero.close()
296 fichero.close()
295 return lista
297 return lista
296
298
297 def CreaFicherosPrevios(self, path, archivo):
299 def CreaFicherosPrevios(self, path, archivo):
298 vector = self.GuardaEnLista(archivo)
300 vector = self.GuardaEnLista(archivo)
299 for i in range(1,self.NumeroDeExperimentos(archivo)+1):
301 for i in range(1,self.NumeroDeExperimentos(archivo)+1):
300 fichero =open(path + str(i)+ '.txt','w')
302 fichero =open(path + str(i)+ '.txt','w')
301 for j in range(0,16):
303 for j in range(0,16):
302 fichero.write(vector[j+16*(i-1)]+'\n')
304 fichero.write(vector[j+16*(i-1)]+'\n')
303 fichero.close()
305 fichero.close()
304
306
305 def CapturaValoresEnArchivo(self, path, polarizacion='up'):
307 def CapturaValoresEnArchivo(self, path, polarizacion='up'):
306 fichero =open(path,'r')
308 fichero =open(path,'r')
307 cnt=0
309 cnt=0
308 lstup=[]
310 lstup=[]
309 lstdw=[]
311 lstdw=[]
310 for cadena in fichero:
312 for cadena in fichero:
311 cnt=cnt+1
313 cnt=cnt+1
312 if cnt==1:
314 if cnt==1:
313 nu01=cadena[1:4]
315 nu01=cadena[1:4]
314 nu02=cadena[5:8]
316 nu02=cadena[5:8]
315 nu03=cadena[9:12]
317 nu03=cadena[9:12]
316 nu04=cadena[13:16]
318 nu04=cadena[13:16]
317 eu01=cadena[17:20]
319 eu01=cadena[17:20]
318 eu02=cadena[21:24]
320 eu02=cadena[21:24]
319 eu03=cadena[25:28]
321 eu03=cadena[25:28]
320 eu04=cadena[29:32]
322 eu04=cadena[29:32]
321 if cnt==2:
323 if cnt==2:
322 nu05=cadena[1:4]
324 nu05=cadena[1:4]
323 nu06=cadena[5:8]
325 nu06=cadena[5:8]
324 nu07=cadena[9:12]
326 nu07=cadena[9:12]
325 nu08=cadena[13:16]
327 nu08=cadena[13:16]
326 eu05=cadena[17:20]
328 eu05=cadena[17:20]
327 eu06=cadena[21:24]
329 eu06=cadena[21:24]
328 eu07=cadena[25:28]
330 eu07=cadena[25:28]
329 eu08=cadena[29:32]
331 eu08=cadena[29:32]
330 if cnt==3:
332 if cnt==3:
331 nu09=cadena[1:4]
333 nu09=cadena[1:4]
332 nu10=cadena[5:8]
334 nu10=cadena[5:8]
333 nu11=cadena[9:12]
335 nu11=cadena[9:12]
334 nu12=cadena[13:16]
336 nu12=cadena[13:16]
335 eu09=cadena[17:20]
337 eu09=cadena[17:20]
336 eu10=cadena[21:24]
338 eu10=cadena[21:24]
337 eu11=cadena[25:28]
339 eu11=cadena[25:28]
338 eu12=cadena[29:32]
340 eu12=cadena[29:32]
339 if cnt==4:
341 if cnt==4:
340 nu13=cadena[1:4]
342 nu13=cadena[1:4]
341 nu14=cadena[5:8]
343 nu14=cadena[5:8]
342 nu15=cadena[9:12]
344 nu15=cadena[9:12]
343 nu16=cadena[13:16]
345 nu16=cadena[13:16]
344 eu13=cadena[17:20]
346 eu13=cadena[17:20]
345 eu14=cadena[21:24]
347 eu14=cadena[21:24]
346 eu15=cadena[25:28]
348 eu15=cadena[25:28]
347 eu16=cadena[29:32]
349 eu16=cadena[29:32]
348 if cnt==5:
350 if cnt==5:
349 wu01=cadena[1:4]
351 wu01=cadena[1:4]
350 wu02=cadena[5:8]
352 wu02=cadena[5:8]
351 wu03=cadena[9:12]
353 wu03=cadena[9:12]
352 wu04=cadena[13:16]
354 wu04=cadena[13:16]
353 su01=cadena[17:20]
355 su01=cadena[17:20]
354 su02=cadena[21:24]
356 su02=cadena[21:24]
355 su03=cadena[25:28]
357 su03=cadena[25:28]
356 su04=cadena[29:32]
358 su04=cadena[29:32]
357 if cnt==6:
359 if cnt==6:
358 wu05=cadena[1:4]
360 wu05=cadena[1:4]
359 wu06=cadena[5:8]
361 wu06=cadena[5:8]
360 wu07=cadena[9:12]
362 wu07=cadena[9:12]
361 wu08=cadena[13:16]
363 wu08=cadena[13:16]
362 su05=cadena[17:20]
364 su05=cadena[17:20]
363 su06=cadena[21:24]
365 su06=cadena[21:24]
364 su07=cadena[25:28]
366 su07=cadena[25:28]
365 su08=cadena[29:32]
367 su08=cadena[29:32]
366 if cnt==7:
368 if cnt==7:
367 wu09=cadena[1:4]
369 wu09=cadena[1:4]
368 wu10=cadena[5:8]
370 wu10=cadena[5:8]
369 wu11=cadena[9:12]
371 wu11=cadena[9:12]
370 wu12=cadena[13:16]
372 wu12=cadena[13:16]
371 su09=cadena[17:20]
373 su09=cadena[17:20]
372 su10=cadena[21:24]
374 su10=cadena[21:24]
373 su11=cadena[25:28]
375 su11=cadena[25:28]
374 su12=cadena[29:32]
376 su12=cadena[29:32]
375 if cnt==8:
377 if cnt==8:
376 wu13=cadena[1:4]
378 wu13=cadena[1:4]
377 wu14=cadena[5:8]
379 wu14=cadena[5:8]
378 wu15=cadena[9:12]
380 wu15=cadena[9:12]
379 wu16=cadena[13:16]
381 wu16=cadena[13:16]
380 su13=cadena[17:20]
382 su13=cadena[17:20]
381 su14=cadena[21:24]
383 su14=cadena[21:24]
382 su15=cadena[25:28]
384 su15=cadena[25:28]
383 su16=cadena[29:32]
385 su16=cadena[29:32]
384 if cnt==9:
386 if cnt==9:
385 nd01=cadena[1:4]
387 nd01=cadena[1:4]
386 nd02=cadena[5:8]
388 nd02=cadena[5:8]
387 nd03=cadena[9:12]
389 nd03=cadena[9:12]
388 nd04=cadena[13:16]
390 nd04=cadena[13:16]
389 ed01=cadena[17:20]
391 ed01=cadena[17:20]
390 ed02=cadena[21:24]
392 ed02=cadena[21:24]
391 ed03=cadena[25:28]
393 ed03=cadena[25:28]
392 ed04=cadena[29:32]
394 ed04=cadena[29:32]
393 if cnt==10:
395 if cnt==10:
394 nd05=cadena[1:4]
396 nd05=cadena[1:4]
395 nd06=cadena[5:8]
397 nd06=cadena[5:8]
396 nd07=cadena[9:12]
398 nd07=cadena[9:12]
397 nd08=cadena[13:16]
399 nd08=cadena[13:16]
398 ed05=cadena[17:20]
400 ed05=cadena[17:20]
399 ed06=cadena[21:24]
401 ed06=cadena[21:24]
400 ed07=cadena[25:28]
402 ed07=cadena[25:28]
401 ed08=cadena[29:32]
403 ed08=cadena[29:32]
402 if cnt==11:
404 if cnt==11:
403 nd09=cadena[1:4]
405 nd09=cadena[1:4]
404 nd10=cadena[5:8]
406 nd10=cadena[5:8]
405 nd11=cadena[9:12]
407 nd11=cadena[9:12]
406 nd12=cadena[13:16]
408 nd12=cadena[13:16]
407 ed09=cadena[17:20]
409 ed09=cadena[17:20]
408 ed10=cadena[21:24]
410 ed10=cadena[21:24]
409 ed11=cadena[25:28]
411 ed11=cadena[25:28]
410 ed12=cadena[29:32]
412 ed12=cadena[29:32]
411 if cnt==12:
413 if cnt==12:
412 nd13=cadena[1:4]
414 nd13=cadena[1:4]
413 nd14=cadena[5:8]
415 nd14=cadena[5:8]
414 nd15=cadena[9:12]
416 nd15=cadena[9:12]
415 nd16=cadena[13:16]
417 nd16=cadena[13:16]
416 ed13=cadena[17:20]
418 ed13=cadena[17:20]
417 ed14=cadena[21:24]
419 ed14=cadena[21:24]
418 ed15=cadena[25:28]
420 ed15=cadena[25:28]
419 ed16=cadena[29:32]
421 ed16=cadena[29:32]
420 if cnt==13:
422 if cnt==13:
421 wd01=cadena[1:4]
423 wd01=cadena[1:4]
422 wd02=cadena[5:8]
424 wd02=cadena[5:8]
423 wd03=cadena[9:12]
425 wd03=cadena[9:12]
424 wd04=cadena[13:16]
426 wd04=cadena[13:16]
425 sd01=cadena[17:20]
427 sd01=cadena[17:20]
426 sd02=cadena[21:24]
428 sd02=cadena[21:24]
427 sd03=cadena[25:28]
429 sd03=cadena[25:28]
428 sd04=cadena[29:32]
430 sd04=cadena[29:32]
429 if cnt==14:
431 if cnt==14:
430 wd05=cadena[1:4]
432 wd05=cadena[1:4]
431 wd06=cadena[5:8]
433 wd06=cadena[5:8]
432 wd07=cadena[9:12]
434 wd07=cadena[9:12]
433 wd08=cadena[13:16]
435 wd08=cadena[13:16]
434 sd05=cadena[17:20]
436 sd05=cadena[17:20]
435 sd06=cadena[21:24]
437 sd06=cadena[21:24]
436 sd07=cadena[25:28]
438 sd07=cadena[25:28]
437 sd08=cadena[29:32]
439 sd08=cadena[29:32]
438 if cnt==15:
440 if cnt==15:
439 wd09=cadena[1:4]
441 wd09=cadena[1:4]
440 wd10=cadena[5:8]
442 wd10=cadena[5:8]
441 wd11=cadena[9:12]
443 wd11=cadena[9:12]
442 wd12=cadena[13:16]
444 wd12=cadena[13:16]
443 sd09=cadena[17:20]
445 sd09=cadena[17:20]
444 sd10=cadena[21:24]
446 sd10=cadena[21:24]
445 sd11=cadena[25:28]
447 sd11=cadena[25:28]
446 sd12=cadena[29:32]
448 sd12=cadena[29:32]
447 if cnt==16:
449 if cnt==16:
448 wd13=cadena[1:4]
450 wd13=cadena[1:4]
449 wd14=cadena[5:8]
451 wd14=cadena[5:8]
450 wd15=cadena[9:12]
452 wd15=cadena[9:12]
451 wd16=cadena[13:16]
453 wd16=cadena[13:16]
452 sd13=cadena[17:20]
454 sd13=cadena[17:20]
453 sd14=cadena[21:24]
455 sd14=cadena[21:24]
454 sd15=cadena[25:28]
456 sd15=cadena[25:28]
455 sd16=cadena[29:32]
457 sd16=cadena[29:32]
456 blck_1_up = [nu01,nu02,nu03,nu04,eu01,eu02,eu03,eu04,nu05,nu06,nu07,nu08,eu05,eu06,eu07,eu08]
458 blck_1_up = [nu01,nu02,nu03,nu04,eu01,eu02,eu03,eu04,nu05,nu06,nu07,nu08,eu05,eu06,eu07,eu08]
457 blck_1_dw = [nd01,nd02,nd03,nd04,ed01,ed02,ed03,ed04,nd05,nd06,nd07,nd08,ed05,ed06,ed07,ed08]
459 blck_1_dw = [nd01,nd02,nd03,nd04,ed01,ed02,ed03,ed04,nd05,nd06,nd07,nd08,ed05,ed06,ed07,ed08]
458 blck_2_up = [nu09,nu10,nu11,nu12,eu09,eu10,eu11,eu12,nu13,nu14,nu15,nu16,eu13,eu14,eu15,eu16]
460 blck_2_up = [nu09,nu10,nu11,nu12,eu09,eu10,eu11,eu12,nu13,nu14,nu15,nu16,eu13,eu14,eu15,eu16]
459 blck_2_dw = [nd09,nd10,nd11,nd12,ed09,ed10,ed11,ed12,nd13,nd14,nd15,nd16,ed13,ed14,ed15,ed16]
461 blck_2_dw = [nd09,nd10,nd11,nd12,ed09,ed10,ed11,ed12,nd13,nd14,nd15,nd16,ed13,ed14,ed15,ed16]
460 blck_3_up = [wu01,wu02,wu03,wu04,su01,su02,su03,su04,wu05,wu06,wu07,wu08,su05,su06,su07,su08]
462 blck_3_up = [wu01,wu02,wu03,wu04,su01,su02,su03,su04,wu05,wu06,wu07,wu08,su05,su06,su07,su08]
461 blck_3_dw = [wd01,wd02,wd03,wd04,sd01,sd02,sd03,sd04,wd05,wd06,wd07,wd08,sd05,sd06,sd07,sd08]
463 blck_3_dw = [wd01,wd02,wd03,wd04,sd01,sd02,sd03,sd04,wd05,wd06,wd07,wd08,sd05,sd06,sd07,sd08]
462 blck_4_up = [wu09,wu10,wu11,wu12,su09,su10,su11,su12,wu13,wu14,wu15,wu16,su13,su14,su15,su16]
464 blck_4_up = [wu09,wu10,wu11,wu12,su09,su10,su11,su12,wu13,wu14,wu15,wu16,su13,su14,su15,su16]
463 blck_4_dw = [wd09,wd10,wd11,wd12,sd09,sd10,sd11,sd12,wd13,wd14,wd15,wd16,sd13,sd14,sd15,sd16]
465 blck_4_dw = [wd09,wd10,wd11,wd12,sd09,sd10,sd11,sd12,wd13,wd14,wd15,wd16,sd13,sd14,sd15,sd16]
464
466
465 lstup = blck_1_up + blck_2_up + blck_3_up + blck_4_up
467 lstup = blck_1_up + blck_2_up + blck_3_up + blck_4_up
466 lstdw = blck_1_dw + blck_2_dw + blck_3_dw + blck_4_dw
468 lstdw = blck_1_dw + blck_2_dw + blck_3_dw + blck_4_dw
467
469
468 if polarizacion=='up':
470 if polarizacion=='up':
469 return lstup
471 return lstup
470 elif polarizacion=='dw':
472 elif polarizacion=='dw':
471 return lstdw
473 return lstdw
472 fichero.close()
474 fichero.close()
473
475
474 def CreaFormatoFinal(self, path, filename):
476 def CreaFormatoFinal(self, path, filename):
475 ne=self.NumeroDeExperimentos(filename)
477 ne=self.NumeroDeExperimentos(filename)
476
478
477 #nombre01 = file1(archivo,'1')
479 #nombre01 = file1(archivo,'1')
478 nombre02 = self.file1(filename,'2')
480 nombre02 = self.file1(filename,'2')
479 fichero=open(path + 'CentralControlFormat.txt','w')
481 fichero=open(path + 'CentralControlFormat.txt','w')
480 fichero.write(nombre02+'\n')
482 fichero.write(nombre02+'\n')
481 fichero.write(str(ne)+'\n')
483 fichero.write(str(ne)+'\n')
482
484
483 for i in range(1,65):
485 for i in range(1,65):
484
486
485 if i<10:
487 if i<10:
486 nmod = '0'+str(i)
488 nmod = '0'+str(i)
487 else: nmod = str(i)
489 else: nmod = str(i)
488
490
489 fichero.write("ABS_" + nmod+'\n')
491 fichero.write("ABS_" + nmod+'\n')
490
492
491 for j in range(1,ne+1):
493 for j in range(1,ne+1):
492 ruta=path+str(j)+'.txt'
494 ruta=path+str(j)+'.txt'
493 lu=self.CapturaValoresEnArchivo(ruta,polarizacion='up')
495 lu=self.CapturaValoresEnArchivo(ruta,polarizacion='up')
494 ld=self.CapturaValoresEnArchivo(ruta,polarizacion='dw')
496 ld=self.CapturaValoresEnArchivo(ruta,polarizacion='dw')
495 part1=''
497 part1=''
496 part2=''
498 part2=''
497 if lu[i-1]=='1.0': part1='000'
499 if lu[i-1]=='1.0': part1='000'
498 if lu[i-1]=='2.0': part1='001'
500 if lu[i-1]=='2.0': part1='001'
499 if lu[i-1]=='3.0': part1='010'
501 if lu[i-1]=='3.0': part1='010'
500 if lu[i-1]=='0.0': part1='011'
502 if lu[i-1]=='0.0': part1='011'
501 if lu[i-1]=='0.5': part1='100'
503 if lu[i-1]=='0.5': part1='100'
502 if lu[i-1]=='1.5': part1='101'
504 if lu[i-1]=='1.5': part1='101'
503 if lu[i-1]=='2.5': part1='110'
505 if lu[i-1]=='2.5': part1='110'
504 if lu[i-1]=='3.5': part1='111'
506 if lu[i-1]=='3.5': part1='111'
505 if ld[i-1]=='1.0': part2='000'
507 if ld[i-1]=='1.0': part2='000'
506 if ld[i-1]=='2.0': part2='001'
508 if ld[i-1]=='2.0': part2='001'
507 if ld[i-1]=='3.0': part2='010'
509 if ld[i-1]=='3.0': part2='010'
508 if ld[i-1]=='0.0': part2='011'
510 if ld[i-1]=='0.0': part2='011'
509 if ld[i-1]=='0.5': part2='100'
511 if ld[i-1]=='0.5': part2='100'
510 if ld[i-1]=='1.5': part2='101'
512 if ld[i-1]=='1.5': part2='101'
511 if ld[i-1]=='2.5': part2='110'
513 if ld[i-1]=='2.5': part2='110'
512 if ld[i-1]=='3.5': part2='111'
514 if ld[i-1]=='3.5': part2='111'
513 fichero.write(part1+part2+'\n')
515 fichero.write(part1+part2+'\n')
514 fichero.write('------'+'\n')
516 fichero.write('------'+'\n')
515 fichero.close()
517 fichero.close()
516
518
517 def EliminaArchivosEnLaCarpeta(self, path, filename):
519 def EliminaArchivosEnLaCarpeta(self, path, filename):
518 ne=self.NumeroDeExperimentos(filename)
520 ne=self.NumeroDeExperimentos(filename)
519 for i in range(1,ne+1):
521 for i in range(1,ne+1):
520 os.remove(path + str(i)+'.txt')
522 os.remove(path + str(i)+'.txt')
521
523
522
524
523 def toCentralControlFormat(self, filename):
525 def toCentralControlFormat(self, filename):
524 """ Funcion que genera un archivo para el control central"""
526 """ Funcion que genera un archivo para el control central"""
525
527
526 path = os.getcwd() + '/'
528 path = os.getcwd() + '/'
527 self.CreaFicherosPrevios(path, filename)
529 self.CreaFicherosPrevios(path, filename)
528 self.CreaFormatoFinal(path, filename)
530 self.CreaFormatoFinal(path, filename)
529 self.EliminaArchivosEnLaCarpeta(path, filename)
531 self.EliminaArchivosEnLaCarpeta(path, filename)
@@ -1,186 +1,187
1 import os
1 import os
2 import library3
2 import library3
3 import time
3 import time
4
4
5 class ABSServer:
5 class ABSServer:
6
6
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
8
8
9 self.ipSource = ipSource
9 self.ipSource = ipSource
10 self.ipDestino = ipDestino
10 self.ipDestino = ipDestino
11 self.portDestino = portDestino
11 self.portDestino = portDestino
12
12
13 self.ipDestino2 = ipDestino2
13 self.ipDestino2 = ipDestino2
14 self.portDestino2 = portDestino2
14 self.portDestino2 = portDestino2
15
15
16 self.tx_buffer = "default"
16 self.tx_buffer = "default"
17 self.rx_buffer = "default"
17 self.rx_buffer = "default"
18 self.enaModules = []
18 self.enaModules = []
19
19
20 self.createObjects()
20 self.createObjects()
21
21
22 def createObjects(self):
22 def createObjects(self):
23
23
24 asServer = True
24 asServer = True
25 self.commServerObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino, "CnMod01", self.portDestino, asServer)
25 self.commServerObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino, "CnMod01", self.portDestino, asServer)
26 self.commClientObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino2, "CnMod01", self.portDestino2)
26 self.commClientObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino2, "CnMod01", self.portDestino2)
27
27
28 def waitRequest(self):
28 def waitRequest(self):
29
29
30 #Using rx buffer
30 #Using rx buffer
31 ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitData()
31 ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitData()
32
32
33 if cmd == "SNDF":
33 if cmd == "SNDF":
34 datarpta = self.__sendFile2Modules(cmd = cmd)
34 datarpta = self.__sendFile2Modules(cmd = cmd)
35
35
36 if cmd == "CHGB":
36 if cmd == "CHGB":
37 datarpta = self.__changeBeam(cmd = cmd)
37 datarpta = self.__changeBeam(cmd = cmd)
38
38
39 if cmd == "ANST":
39 if cmd == "ANST":
40 self.__getControlModuleStatus(cmd = cmd)
40 self.__getControlModuleStatus(cmd = cmd)
41 #Using tx buffer
41 #Using tx buffer
42 datarpta = self.tx_buffer
42 datarpta = self.tx_buffer
43
43
44 if cmd == "NTST":
44 if cmd == "NTST":
45 self.enaModules = self.__getConnectionStatus()
45 self.enaModules = self.__getConnectionStatus()
46 #Using tx buffer
46 #Using tx buffer
47 datarpta = self.enaModules
47 datarpta = self.enaModules
48
48
49 self.commServerObj.sendData(cmd=cmd, data=datarpta, ipDestino = ipSource)
49 self.commServerObj.sendData(cmd=cmd, data=datarpta, ipDestino = ipSource)
50
50
51 def checkModule(self, address):
51 def checkModule(self, address):
52
52
53 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
53 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
54 status = os.system(cmd)
54 status = os.system(cmd)
55
55
56 if status == 256:
56 if status == 256:
57 return False
57 return False
58
58
59 return True
59 return True
60
60
61 def __writeReport(self, enaModules):
61 def __writeReport(self, enaModules):
62
62
63 status_array = ["Status of modules\n"]
63 status_array = ["Status of modules\n"]
64 status_array.append("----------------\n")
64 status_array.append("----------------\n")
65
65
66 for address in range(1,65):
66 for address in range(1,65):
67 if address in enaModules:
67 if address in enaModules:
68 status_array.append("192.168.1." + str(address) + " [1 1]\n")
68 status_array.append("192.168.1." + str(address) + " [1 1]\n")
69 else:
69 else:
70 status_array.append("192.168.1." + str(address) + " [0 0]\n")
70 status_array.append("192.168.1." + str(address) + " [0 0]\n")
71
71
72 f = open("module_status.txt","w")
72 f = open("module_status.txt","w")
73 f.writelines(status_array)
73 f.writelines(status_array)
74 f.close()
74 f.close()
75
75
76 def checkAntenna(self):
76 def checkAntenna(self):
77
77
78 """
78 """
79 Direccion de los modulos de las antenas:
79 Direccion de los modulos de las antenas:
80
80
81 Norte : 01-16
81 Norte : 01-16
82 Este : 17-32
82 Este : 17-32
83 Oeste: : 33-48
83 Oeste: : 33-48
84 Sur : 49-64
84 Sur : 49-64
85
85
86 """
86 """
87
87
88 enaModules2 = []
88 enaModules2 = []
89
89
90 for address in range(1,65):
90 for address in range(1,65):
91 if self.checkModule(address):
91 if self.checkModule(address):
92 enaModules2.append(address)
92 enaModules2.append(address)
93
93
94 self.__writeReport(enaModules2)
94 self.__writeReport(enaModules2)
95 return enaModules2
95 return enaModules2
96
96
97 def __ConnectionWithControlModules(self,data,cmd,id):
97 def __ConnectionWithControlModules(self,data,cmd,id):
98
98
99 self.commClientObj.open_socket()
99 self.commClientObj.open_socket()
100 ip = "192.168.1." + str(id)
100 ip = "192.168.1." + str(id)
101 self.commClientObj.sendData(cmd, data, ip)
101 self.commClientObj.sendData(cmd, data, ip)
102 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
102 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
103 self.commClientObj.close_socket()
103 self.commClientObj.close_socket()
104
104
105 return tmp
105 return tmp
106
106
107 def __All2Blocks(self,input):
107 def __All2Blocks(self,input):
108
108
109 rx_frame_lst = input.split('\n',2)
109 rx_frame_lst = input.split('\n',2)
110
110
111 header = rx_frame_lst[0] + "\n"
111 header = rx_frame_lst[0] + "\n"
112 control_modules_str = rx_frame_lst[2]
112 control_modules_str = rx_frame_lst[2]
113 control_modules_lst = control_modules_str.split("------\n")
113 control_modules_lst = control_modules_str.split("------\n")
114
114
115 return header, control_modules_lst
115 return header, control_modules_lst
116
116
117
117
118 def __sendFile2Modules(self,cmd):
118 def __sendFile2Modules(self,cmd):
119
119
120 #Needed for the loop
120 #Needed for the loop
121 header, control_modules_lst = self.__All2Blocks(self.rx_buffer)
121 header, control_modules_lst = self.__All2Blocks(self.rx_buffer)
122 correct = 0
122 correct = 0
123
123
124 for id in range(1,65):
124 for id in range(1,65):
125
125
126 if id not in self.enaModules:
126 if id not in self.enaModules:
127 continue
127 continue
128
128
129 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
129 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
130 correct = correct + 1
130 correct = correct + 1
131
131
132 if correct == len(self.enaModules):
132 if correct == len(self.enaModules):
133 rpta = "OK"
133 rpta = "OK"
134 else:
134 else:
135 rpta = "Failure"
135 rpta = "Failure"
136
136
137 return rpta
137 return rpta
138
138
139 def __changeBeam(self, cmd):
139 def __changeBeam(self, cmd):
140
140
141 correct = 0
141 correct = 0
142 # enaModules = self.checkAntenna()
142 # enaModules = self.checkAntenna()
143 # enaModules = [11,12,13,14]
143 # enaModules = [11,12,13,14]
144
144
145 for id in range(1,65):
145 for id in range(1,65):
146 if id not in self.enaModules:
146 if id not in self.enaModules:
147 continue
147 continue
148
148
149 if self.__ConnectionWithControlModules(self.rx_buffer,cmd,id) == "OK":
149 if self.__ConnectionWithControlModules(self.rx_buffer,cmd,id) == "OK":
150 correct = correct + 1
150 correct = correct + 1
151
151
152 if correct == len(self.enaModules):
152 if correct == len(self.enaModules):
153 rpta = "OK"
153 rpta = "OK"
154 else:
154 else:
155 rpta = "Failure"
155 rpta = "Failure"
156
156
157 return rpta
157 return rpta
158
158
159 def __getControlModuleStatus(self, cmd):
159 def __getControlModuleStatus(self, cmd):
160
160
161 all_blocks = ""
161 all_blocks = ""
162 # enaModules = self.checkAntenna()
162 # enaModules = self.checkAntenna()
163 # enaModules = [11,12,13,14]
163 # enaModules = [11,12,13,14]
164
164
165 for id in range(1,65):
165 for id in range(1,65):
166 if id not in self.enaModules:
166 if id not in self.enaModules:
167 continue
167 continue
168
168
169 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
169 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
170
170
171 all_blocks = all_blocks + one_block
171 all_blocks = all_blocks + one_block
172 #Using tx buffer
172 #Using tx buffer
173 print all_blocks
173 self.tx_buffer = all_blocks
174 self.tx_buffer = all_blocks
174
175
175 def __getConnectionStatus(self):
176 def __getConnectionStatus(self):
176
177
177 enaModules = self.checkAntenna()
178 enaModules = self.checkAntenna()
178
179
179 return enaModules
180 return enaModules
180
181
181 if __name__ == '__main__':
182 if __name__ == '__main__':
182
183
183 absObj = ABSServer()
184 absObj = ABSServer()
184
185
185 while 1:
186 while 1:
186 absObj.waitRequest() No newline at end of file
187 absObj.waitRequest()
General Comments 0
You need to be logged in to leave comments. Login now