##// END OF EJS Templates
imanay -
r92:93
parent child
Show More
@@ -0,0 +1,165
1 import socket
2
3 class TCPComm:
4
5 __HEADER = "JRO"
6 __TYPE = "ABS"
7
8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
9
10 self.ipSource = ipSource
11 self.ipDestino = ipDestino
12 self.portDestino = portDestino
13 self.addr = (ipDestino,portDestino)
14
15 self.sc = "none"
16 self.answer = "none" #test
17 self.asServer = False
18 self.len = 0
19 self.crc = 0
20
21 self.openSocket(asServer)
22
23 def openSocket(self, asServer):
24
25 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
26 # self.socket_c = socket.socket()
27 # self.socket_c.connect((self.ipDestino, self.portDestino))
28
29 if asServer:
30 self.socket_c = socket.socket()
31 # self.configAsServer()
32 self.socket_c.bind(self.addr)
33 self.asServer = True
34 else:
35 # self.configAsClient()
36 self.asServer = False #Socket is opened at the sendData function
37
38 # def configAsClient(self):
39 #Buscar broadcast TCP
40 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
41 # self.socket_c.connect(self.addr)
42 # pass
43
44 # def configAsServer(self):
45 #
46 # self.socket_c.bind(self.addr)
47
48 def waitData(self, nbytes = 1024):
49
50 print "\nWaiting some client."
51
52 if self.asServer == False:
53 # Short data through ethernet
54 trama_rx = self.socket_c.recv(nbytes)
55 else:
56 self.socket_c.listen(1)
57 sc, addr = self.socket_c.accept()
58 self.sc = sc
59 self.answer = addr
60 # Big data through ethernet
61 trama_rx = ""
62 while True:
63 tmp = self.sc.recv(nbytes)
64 trama_rx = trama_rx + tmp
65 if trama_rx[-4:] == "quit":
66 break
67
68 print "\nThis socket has received some data."
69
70 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
71
72 return ipSource, ipDestino, cmd, data
73
74 def waitServer(self, nbytes = 1024):
75
76 print "\nWaiting some client."
77 self.socket_c.listen(1)
78 sc, addr = self.socket_c.accept()
79 self.sc = sc
80 self.answer = addr
81 # Big data through ethernet
82 trama_rx = ""
83 while True:
84 tmp = self.sc.recv(nbytes)
85 trama_rx = trama_rx + tmp
86 if trama_rx[-4:] == "quit":
87 break
88
89 print "\nThis socket has received some data from: " + str(self.answer)
90
91 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
92
93 return ipSource, ipDestino, cmd, data
94
95 def waitClient(self, nbytes = 1024):
96
97 print "\nWaiting the server."
98 # Short data through ethernet
99 trama_rx = self.socket_c.recv(nbytes)
100
101 print "\nThis socket has received this data: " + str(trama_rx)
102
103 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
104
105 return ipSource, ipDestino, cmd, data
106
107 def sendData(self, cmd, data, id):
108
109 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + \
110 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc)
111
112 if self.portDestino == 7000:
113 trama_tx = trama_tx + ":" + "quit"
114 # Send messages
115 if self.asServer == False:
116 host = "192.168.1." + str(id)
117 self.socket_c.connect((host, self.portDestino))
118 self.socket_c.send(trama_tx)
119 else:
120 self.sc.send(trama_tx)
121 print "Sending message:[" + trama_tx + "]"
122
123 def sendData2(self, cmd, data, ipDestino):
124
125 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \
126 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit"
127
128 if self.asServer == True:
129 self.SendAsServer(trama_tx)
130 else:
131 self.SendAsClient(ipDestino, trama_tx)
132
133 def SendAsServer(self, trama_tx):
134
135 self.sc.send(trama_tx)
136 print "Sending message:[" + trama_tx + "] to: " + str(self.answer)
137
138
139 def SendAsClient(self, ipDestino, trama_tx):
140
141 self.socket_c.connect((ipDestino, self.portDestino))
142 self.socket_c.send(trama_tx)
143 print "Sending message:[" + trama_tx + "] to: " + ipDestino
144
145 def __getTrama(self, trama):
146
147 FrameList = trama.split(':')
148
149 header = FrameList[0]
150 TypeOfInstrument = FrameList[1]
151 ipSource = FrameList[2]
152 ipDestino = FrameList[3]
153 len = FrameList[4]
154 cmd = FrameList[5]
155 data = FrameList[6]
156 crc = FrameList[7]
157 trash = FrameList[8]
158
159 return ipSource, ipDestino, cmd, data
160
161 def close_socket(self):
162 self.socket_c.close()
163
164 def open_socket(self):
165 self.socket_c = socket.socket() No newline at end of file
@@ -1,491 +1,500
1 1 # imports needed for the file convertion
2 2 import os
3 3 import sys
4 4 import time
5 5 import numpy as np
6 6
7 import library
7 import library2
8 8
9 9 class ABSClient:
10 10
11 def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000):
11 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000):
12 12
13 13 self.ipSource = ipSource
14 14 self.ipDestino = ipDestino
15 15 self.portDestino = portDestino
16 16
17 17 self.createObjects()
18 18
19 19 def createObjects(self):
20 20
21 self.commObj = library.TCPComm(self.ipSource, self.ipDestino, self.portDestino)
21 self.commObj = library2.TCPComm(self.ipSource, self.ipDestino, self.portDestino)
22 22
23 23 def sendFile(self, filename):
24 24
25 25 #From matriz to control module format
26 26 self.FuncionMaestra_GeneraFormatoControlCentral(filename)
27 27 FileName = "FormatoControlCentral.txt"
28 28 F_Obj = open(FileName,"r")
29 29 FileList = F_Obj.readlines()
30 30 F_Obj.close()
31 31 FileStr = "".join(FileList)
32 32 data = FileStr
33
34 self.commObj.sendData(cmd="SNDF", data=data)
35 self.commObj.waitData()
33
34 self.commObj.open_socket()
35 # self.commObj.sendData(cmd="SNDF", data=data, id = 117)
36 self.commObj.sendData2(cmd="SNDF", data=data, ipDestino = self.ipDestino)
37 # self.commObj.waitData()
38 self.commObj.waitClient()
36 39 self.commObj.close_socket()
37 40
38 41 def changeBeam(self, newBeam):
39
40 self.commObj.sendData(cmd="CHGB", data=newBeam)
41 self.commObj.waitData()
42
43 self.commObj.open_socket()
44 # self.commObj.sendData(cmd="CHGB", data=newBeam, id = 117)
45 self.commObj.sendData2(cmd="CHGB", data=newBeam, ipDestino = self.ipDestino)
46 # self.commObj.waitData()
47 self.commObj.waitClient()
42 48 self.commObj.close_socket()
43 49
44 50 def __writeFile(self, filename, data):
45 51
46 52 fobj = open(filename,"w")
47 53 fobj.writelines(data)
48 54 fobj.close()
49 55
50 56 def getStatus(self, data):
51
52 self.commObj.sendData(cmd="ANST", data = data)
53 ipSource, ipDestino, cmd, data = self.commObj.waitData()
57
58 self.commObj.open_socket()
59 # self.commObj.sendData(cmd="ANST", data = data, id = 117)
60 self.commObj.sendData2(cmd="ANST", data = data, ipDestino = self.ipDestino)
61 # ipSource, ipDestino, cmd, data = self.commObj.waitData()
62 ipSource, ipDestino, cmd, data = self.commObj.waitClient()
54 63 self.commObj.close_socket()
55 64 self.__writeFile("report.txt", data)
56 65
57 66 ##########
58 67
59 68 def FuncionMaestra_GeneraFormatoControlCentral(self,archivo):
60 69 """ Funcion que genera un archivo para el control central"""
61 70
62 71 # CarpetaDeTrabajo='/home/redes/ABS_Control_2012_09_24/Control_Module_v1_Client_09_24/'
63 72 CarpetaDeTrabajo = os.getcwd() + '/'
64 73 #print CarpetaDeTrabajo
65 74 #CarpetaDeTrabajo='/home/redes/workspace/ABS_Client_v2/Debug/'
66 75
67 76 def lst2string(lst):
68 77 string=''
69 78 for i in lst:
70 79 string=string+i
71 80 return string
72 81
73 82 def string2lst(string):
74 83 lst = []
75 84 for i in string:
76 85 lst.append(i)
77 86 return lst
78 87
79 88
80 89 def file1(string, type):
81 90 lst = string2lst(archivo)
82 91 fin = -1
83 92 t = len(lst)
84 93 for i in np.arange(-1,-t,-1):
85 94 if lst[i]=='/':
86 95 fin=i
87 96 break
88 97 if type == '1':
89 98 nombre2 = lst[fin+1:]
90 99 nombre2[-1]='s'
91 100 nombre2 = lst2string(nombre2)
92 101 return nombre2
93 102 if type == '2':
94 103 nombre2 = lst[fin+1:]
95 104 nombre2[-1]='1'
96 105 nombre2 = lst2string(nombre2)
97 106 return nombre2
98 107
99 108
100 109 def EliminaSaltoDeLinea(cadena):
101 110 i = 0
102 111 for elemento in cadena:
103 112 if elemento =='\n' or elemento =='\r':
104 113 pass
105 114 else:
106 115 i=i+1
107 116 return cadena [:i]
108 117
109 118 def NumeroDeExperimentos(path):
110 119 fichero1=open(path,'r')
111 120 cont=0
112 121 for cadena in fichero1:
113 122 cont=cont+1
114 123 if cont==3:
115 124 nexp=''
116 125 pos=0
117 126 for elemento in cadena:
118 127 pos=pos+1
119 128 if elemento=='=':
120 129 nexp=int(cadena[pos:])
121 130 return nexp
122 131 fichero1.close()
123 132
124 133 def Paridad(numero):
125 134 if numero%2==0: return 'par'
126 135 elif numero%2==1: return 'impar'
127 136
128 137 def EvaluaCadena(cadena):
129 138 if len(cadena)>35:
130 139 if cadena[-1]=='$':
131 140 return cadena[-35:-2]
132 141 elif cadena[-1]==']':
133 142 return cadena[-34:-1]
134 143 else:
135 144 return None
136 145
137 146 def GuardaEnLista(path):
138 147 fichero=open(path,'r')
139 148 lista=[]
140 149 for cadena in fichero:
141 150 cadena = EliminaSaltoDeLinea(cadena)
142 151 cadena = EvaluaCadena(cadena)
143 152 if cadena != None:
144 153 lista.append(cadena)
145 154 fichero.close()
146 155 return lista
147 156
148 157 def CreaFicherosPrevios():
149 158 vector = GuardaEnLista(archivo)
150 159 for i in range(1,NumeroDeExperimentos(archivo)+1):
151 160 fichero =open(CarpetaDeTrabajo+str(i)+'.txt','w')
152 161 for j in range(0,16):
153 162 fichero.write(vector[j+16*(i-1)]+'\n')
154 163 fichero.close()
155 164
156 165 def CapturaValoresEnArchivo(path,polarizacion='up'):
157 166 fichero =open(path,'r')
158 167 cnt=0
159 168 lstup=[]
160 169 lstdw=[]
161 170 for cadena in fichero:
162 171 cnt=cnt+1
163 172 if cnt==5:
164 173 su01=cadena[17:20]
165 174 su02=cadena[21:24]
166 175 su03=cadena[25:28]
167 176 su04=cadena[29:32]
168 177 if cnt==6:
169 178 su05=cadena[17:20]
170 179 su06=cadena[21:24]
171 180 su07=cadena[25:28]
172 181 su08=cadena[29:32]
173 182 if cnt==7:
174 183 su09=cadena[17:20]
175 184 su10=cadena[21:24]
176 185 su11=cadena[25:28]
177 186 su12=cadena[29:32]
178 187 if cnt==8:
179 188 su13=cadena[17:20]
180 189 su14=cadena[21:24]
181 190 su15=cadena[25:28]
182 191 su16=cadena[29:32]
183 192 if cnt==13:
184 193 sd01=cadena[17:20]
185 194 sd02=cadena[21:24]
186 195 sd03=cadena[25:28]
187 196 sd04=cadena[29:32]
188 197 if cnt==14:
189 198 sd05=cadena[17:20]
190 199 sd06=cadena[21:24]
191 200 sd07=cadena[25:28]
192 201 sd08=cadena[29:32]
193 202 if cnt==15:
194 203 sd09=cadena[17:20]
195 204 sd10=cadena[21:24]
196 205 sd11=cadena[25:28]
197 206 sd12=cadena[29:32]
198 207 if cnt==16:
199 208 sd13=cadena[17:20]
200 209 sd14=cadena[21:24]
201 210 sd15=cadena[25:28]
202 211 sd16=cadena[29:32]
203 212 lstup=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16]
204 213 lstdw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16]
205 214 if polarizacion=='up':
206 215 return lstup
207 216 elif polarizacion=='dw':
208 217 return lstdw
209 218 fichero.close()
210 219
211 220 def CapturaValoresEnArchivo2(path,polarizacion='up'):
212 221 fichero =open(path,'r')
213 222 cnt=0
214 223 lstup=[]
215 224 lstdw=[]
216 225 for cadena in fichero:
217 226 cnt=cnt+1
218 227 if cnt==1:
219 228 nu01=cadena[1:4]
220 229 nu02=cadena[5:8]
221 230 nu03=cadena[9:12]
222 231 nu04=cadena[13:16]
223 232 eu01=cadena[17:20]
224 233 eu02=cadena[21:24]
225 234 eu03=cadena[25:28]
226 235 eu04=cadena[29:32]
227 236 if cnt==2:
228 237 nu05=cadena[1:4]
229 238 nu06=cadena[5:8]
230 239 nu07=cadena[9:12]
231 240 nu08=cadena[13:16]
232 241 eu05=cadena[17:20]
233 242 eu06=cadena[21:24]
234 243 eu07=cadena[25:28]
235 244 eu08=cadena[29:32]
236 245 if cnt==3:
237 246 nu09=cadena[1:4]
238 247 nu10=cadena[5:8]
239 248 nu11=cadena[9:12]
240 249 nu12=cadena[13:16]
241 250 eu09=cadena[17:20]
242 251 eu10=cadena[21:24]
243 252 eu11=cadena[25:28]
244 253 eu12=cadena[29:32]
245 254 if cnt==4:
246 255 nu13=cadena[1:4]
247 256 nu14=cadena[5:8]
248 257 nu15=cadena[9:12]
249 258 nu16=cadena[13:16]
250 259 eu13=cadena[17:20]
251 260 eu14=cadena[21:24]
252 261 eu15=cadena[25:28]
253 262 eu16=cadena[29:32]
254 263 if cnt==5:
255 264 wu01=cadena[1:4]
256 265 wu02=cadena[5:8]
257 266 wu03=cadena[9:12]
258 267 wu04=cadena[13:16]
259 268 su01=cadena[17:20]
260 269 su02=cadena[21:24]
261 270 su03=cadena[25:28]
262 271 su04=cadena[29:32]
263 272 if cnt==6:
264 273 wu05=cadena[1:4]
265 274 wu06=cadena[5:8]
266 275 wu07=cadena[9:12]
267 276 wu08=cadena[13:16]
268 277 su05=cadena[17:20]
269 278 su06=cadena[21:24]
270 279 su07=cadena[25:28]
271 280 su08=cadena[29:32]
272 281 if cnt==7:
273 282 wu09=cadena[1:4]
274 283 wu10=cadena[5:8]
275 284 wu11=cadena[9:12]
276 285 wu12=cadena[13:16]
277 286 su09=cadena[17:20]
278 287 su10=cadena[21:24]
279 288 su11=cadena[25:28]
280 289 su12=cadena[29:32]
281 290 if cnt==8:
282 291 wu13=cadena[1:4]
283 292 wu14=cadena[5:8]
284 293 wu15=cadena[9:12]
285 294 wu16=cadena[13:16]
286 295 su13=cadena[17:20]
287 296 su14=cadena[21:24]
288 297 su15=cadena[25:28]
289 298 su16=cadena[29:32]
290 299 if cnt==9:
291 300 nd01=cadena[1:4]
292 301 nd02=cadena[5:8]
293 302 nd03=cadena[9:12]
294 303 nd04=cadena[13:16]
295 304 ed01=cadena[17:20]
296 305 ed02=cadena[21:24]
297 306 ed03=cadena[25:28]
298 307 ed04=cadena[29:32]
299 308 if cnt==10:
300 309 nd05=cadena[1:4]
301 310 nd06=cadena[5:8]
302 311 nd07=cadena[9:12]
303 312 nd08=cadena[13:16]
304 313 ed05=cadena[17:20]
305 314 ed06=cadena[21:24]
306 315 ed07=cadena[25:28]
307 316 ed08=cadena[29:32]
308 317 if cnt==11:
309 318 nd09=cadena[1:4]
310 319 nd10=cadena[5:8]
311 320 nd11=cadena[9:12]
312 321 nd12=cadena[13:16]
313 322 ed09=cadena[17:20]
314 323 ed10=cadena[21:24]
315 324 ed11=cadena[25:28]
316 325 ed12=cadena[29:32]
317 326 if cnt==12:
318 327 nd13=cadena[1:4]
319 328 nd14=cadena[5:8]
320 329 nd15=cadena[9:12]
321 330 nd16=cadena[13:16]
322 331 ed13=cadena[17:20]
323 332 ed14=cadena[21:24]
324 333 ed15=cadena[25:28]
325 334 ed16=cadena[29:32]
326 335 if cnt==13:
327 336 wd01=cadena[1:4]
328 337 wd02=cadena[5:8]
329 338 wd03=cadena[9:12]
330 339 wd04=cadena[13:16]
331 340 sd01=cadena[17:20]
332 341 sd02=cadena[21:24]
333 342 sd03=cadena[25:28]
334 343 sd04=cadena[29:32]
335 344 if cnt==14:
336 345 wd05=cadena[1:4]
337 346 wd06=cadena[5:8]
338 347 wd07=cadena[9:12]
339 348 wd08=cadena[13:16]
340 349 sd05=cadena[17:20]
341 350 sd06=cadena[21:24]
342 351 sd07=cadena[25:28]
343 352 sd08=cadena[29:32]
344 353 if cnt==15:
345 354 wd09=cadena[1:4]
346 355 wd10=cadena[5:8]
347 356 wd11=cadena[9:12]
348 357 wd12=cadena[13:16]
349 358 sd09=cadena[17:20]
350 359 sd10=cadena[21:24]
351 360 sd11=cadena[25:28]
352 361 sd12=cadena[29:32]
353 362 if cnt==16:
354 363 wd13=cadena[1:4]
355 364 wd14=cadena[5:8]
356 365 wd15=cadena[9:12]
357 366 wd16=cadena[13:16]
358 367 sd13=cadena[17:20]
359 368 sd14=cadena[21:24]
360 369 sd15=cadena[25:28]
361 370 sd16=cadena[29:32]
362 371 lst_n_up=[nu01,nu02,nu03,nu04,nu05,nu06,nu07,nu08,nu09,nu10,nu11,nu12,nu13,nu14,nu15,nu16]
363 372 lst_n_dw=[nd01,nd02,nd03,nd04,nd05,nd06,nd07,nd08,nd09,nd10,nd11,nd12,nd13,nd14,nd15,nd16]
364 373 lst_s_up=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16]
365 374 lst_s_dw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16]
366 375 lst_w_up=[wu01,wu02,wu03,wu04,wu05,wu06,wu07,wu08,wu09,wu10,wu11,wu12,wu13,wu14,wu15,wu16]
367 376 lst_w_dw=[wd01,wd02,wd03,wd04,wd05,wd06,wd07,wd08,wd09,wd10,wd11,wd12,wd13,wd14,wd15,wd16]
368 377 lst_e_up=[eu01,eu02,eu03,eu04,eu05,eu06,eu07,eu08,eu09,eu10,eu11,eu12,eu13,eu14,eu15,eu16]
369 378 lst_e_dw=[ed01,ed02,ed03,ed04,ed05,ed06,ed07,ed08,ed09,ed10,ed11,ed12,ed13,ed14,ed15,ed16]
370 379
371 380 lstup = lst_s_up + lst_w_up + lst_n_up + lst_e_up
372 381 lstdw = lst_s_dw + lst_w_dw + lst_n_up + lst_e_up
373 382
374 383 if polarizacion=='up':
375 384 return lstup
376 385 elif polarizacion=='dw':
377 386 return lstdw
378 387 fichero.close()
379 388
380 389
381 390 def CreaFormatoFinal():
382 391 ne=NumeroDeExperimentos(archivo)
383 392
384 393 #nombre01 = file1(archivo,'1')
385 394 nombre02 = file1(archivo,'2')
386 395 fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w')
387 396 fichero.write(nombre02+'\n')
388 397 fichero.write(str(ne)+'\n')
389 398 for i in range(1,17):
390 399
391 400 if i<10:
392 401 nmod = '0'+str(i)
393 402 else: nmod = str(i)
394 403
395 404
396 405 fichero.write('S'+nmod+'\n')
397 406 for j in range(1,ne+1):
398 407 ruta=CarpetaDeTrabajo+str(j)+'.txt'
399 408 lu=CapturaValoresEnArchivo(ruta,polarizacion='up')
400 409 ld=CapturaValoresEnArchivo(ruta,polarizacion='dw')
401 410 part1=''
402 411 part2=''
403 412 if lu[i-1]=='1.0': part1='000'
404 413 if lu[i-1]=='2.0': part1='001'
405 414 if lu[i-1]=='3.0': part1='010'
406 415 if lu[i-1]=='0.0': part1='011'
407 416 if lu[i-1]=='0.5': part1='100'
408 417 if lu[i-1]=='1.5': part1='101'
409 418 if lu[i-1]=='2.5': part1='110'
410 419 if lu[i-1]=='3.5': part1='111'
411 420 if ld[i-1]=='1.0': part2='000'
412 421 if ld[i-1]=='2.0': part2='001'
413 422 if ld[i-1]=='3.0': part2='010'
414 423 if ld[i-1]=='0.0': part2='011'
415 424 if ld[i-1]=='0.5': part2='100'
416 425 if ld[i-1]=='1.5': part2='101'
417 426 if ld[i-1]=='2.5': part2='110'
418 427 if ld[i-1]=='3.5': part2='111'
419 428 fichero.write(part1+part2+'\n')
420 429 fichero.write('------'+'\n')
421 430 fichero.close()
422 431
423 432 def CreaFormatoFinal2():
424 433 ne=NumeroDeExperimentos(archivo)
425 434
426 435 #nombre01 = file1(archivo,'1')
427 436 nombre02 = file1(archivo,'2')
428 437 fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w')
429 438 fichero.write(nombre02+'\n')
430 439 fichero.write(str(ne)+'\n')
431 440
432 441 for i in range(1,65):
433 442
434 443 if i<10:
435 444 nmod = '0'+str(i)
436 445 else: nmod = str(i)
437 446
438 447 fichero.write("ABS_" + nmod+'\n')
439 448
440 449 for j in range(1,ne+1):
441 450 ruta=CarpetaDeTrabajo+str(j)+'.txt'
442 451 lu=CapturaValoresEnArchivo2(ruta,polarizacion='up')
443 452 ld=CapturaValoresEnArchivo2(ruta,polarizacion='dw')
444 453 part1=''
445 454 part2=''
446 455 if lu[i-1]=='1.0': part1='000'
447 456 if lu[i-1]=='2.0': part1='001'
448 457 if lu[i-1]=='3.0': part1='010'
449 458 if lu[i-1]=='0.0': part1='011'
450 459 if lu[i-1]=='0.5': part1='100'
451 460 if lu[i-1]=='1.5': part1='101'
452 461 if lu[i-1]=='2.5': part1='110'
453 462 if lu[i-1]=='3.5': part1='111'
454 463 if ld[i-1]=='1.0': part2='000'
455 464 if ld[i-1]=='2.0': part2='001'
456 465 if ld[i-1]=='3.0': part2='010'
457 466 if ld[i-1]=='0.0': part2='011'
458 467 if ld[i-1]=='0.5': part2='100'
459 468 if ld[i-1]=='1.5': part2='101'
460 469 if ld[i-1]=='2.5': part2='110'
461 470 if ld[i-1]=='3.5': part2='111'
462 471 fichero.write(part1+part2+'\n')
463 472 fichero.write('------'+'\n')
464 473 fichero.close()
465 474
466 475 def EliminaArchivosEnLaCarpeta():
467 476 ne=NumeroDeExperimentos(archivo)
468 477 for i in range(1,ne+1):
469 478 os.remove(CarpetaDeTrabajo+str(i)+'.txt')
470 479
471 480 CreaFicherosPrevios()
472 481 CreaFormatoFinal2()
473 482 EliminaArchivosEnLaCarpeta()
474 483
475 484 ##########
476 485
477 486 if __name__ == '__main__':
478 487
479 488 filename = "experimento1.abs"
480 489
481 490 absObj = ABSClient()
482 absObj.sendFile(filename)
491 # absObj.sendFile(filename)
483 492 # absObj.changeBeam("0")
484 # absObj.changeBeam("1")
493 absObj.changeBeam("1")
485 494 # absObj.changeBeam("2")
486 495 # absObj.changeBeam("3")
487 496 # absObj.changeBeam("4")
488 497 # absObj.changeBeam("5")
489 498 # absObj.changeBeam("6")
490 499 # absObj.changeBeam("7")
491 500 # absObj.getStatus(5) No newline at end of file
@@ -1,195 +1,209
1 1 #import tftpy
2 2 import socket
3 3
4 4 class UDPComm:
5 5
6 6 __HEADER = "ABS"
7 7
8 8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
9 9
10 10 self.ipSource = ipSource
11 11 self.ipDestino = ipDestino
12 12 self.portDestino = portDestino
13 13 self.addr = (ipDestino,portDestino)
14 14 self.answer = "none" #test
15 15 self.mode = "none"
16
16 17
17 18 self.openSocket(asServer)
18 19
19 20 def openSocket(self, asServer):
20 21
21 22 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
22 23 self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
23 24 # self.socket_c.connect((self.ipDestino, self.portDestino))
24 25
25 26 if asServer:
26 27 self.configAsServer()
27 28 self.mode = "server"
28 29 else:
29 30 self.configAsClient()
30 31 self.mode = "client"
31 32
32 33 def configAsClient(self):
33 34 #Configurar broadcast
34 35 self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
35 36
36 37 def configAsServer(self):
37 38
38 39 self.socket_c.bind(self.addr)
39 40 print "\nServer initialized"
40 41
41 42 def waitData(self, nbytes = 16384):
42 43
43 44 print "\nWaiting some data"
44 45 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
45 46 print "\nThis socket has received some data from:"
46 47 print self.answer
47 48 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
48 49
49 50 return ipSource, ipDestino, cmd, data
50 51
51 52 def sendData(self, cmd, data):
52 53
53 54 if self.portDestino == 7000:
54 55 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
55 56 else:
56 57 trama_tx = data
57 58
58 59 if self.mode == "client":
59 60 destiny = self.addr
60 61 else:
61 62 destiny = self.answer
62 63 # Send messages
63 64 if(self.socket_c.sendto(trama_tx, destiny)):
64 65 print "Sending message:[" + trama_tx + "] to " + str(destiny)
65 66
66 67 def __getTrama(self, trama):
67 68
68 69 FrameList = trama.split(':')
69 70
70 71 header = FrameList[0]
71 72 ipSource = FrameList[1]
72 73 ipDestino = FrameList[2]
73 74 cmd = FrameList[3]
74 75 data = FrameList[4]
75 76 trash = FrameList[5]
76 77
77 78 return ipSource, ipDestino, cmd, data
78 79
79 80 class TCPComm:
80 81
81 __HEADER = "ABS"
82 __HEADER = "JRO"
83 __TYPE = "ABS"
82 84
83 85 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
84 86
85 87 self.ipSource = ipSource
86 88 self.ipDestino = ipDestino
87 89 self.portDestino = portDestino
88 90 self.addr = (ipDestino,portDestino)
89 91
90 92 self.sc = "none"
91 93 self.answer = "none" #test
92 94 self.mode = "none"
95 self.len = 0
96 self.crc = 0
93 97
94 98 self.openSocket(asServer)
95 99
96 100 def openSocket(self, asServer):
97 101
98 102 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
99 self.socket_c = socket.socket()
103 # self.socket_c = socket.socket()
100 104 # self.socket_c.connect((self.ipDestino, self.portDestino))
101 105
102 106 if asServer:
103 self.configAsServer()
107 self.socket_c = socket.socket()
108 # self.configAsServer()
109 self.socket_c.bind(self.addr)
104 110 self.mode = "server"
105 111 else:
106 self.configAsClient()
107 self.mode = "client"
108
109 def configAsClient(self):
112 # self.configAsClient()
113 self.mode = "client" #Socket is opened at the sendData function
114
115 # def configAsClient(self):
110 116 #Buscar broadcast TCP
111 117 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
112 self.socket_c.connect(self.addr)
113
114 def configAsServer(self):
115
116 self.socket_c.bind(self.addr)
118 # self.socket_c.connect(self.addr)
119 # pass
120
121 # def configAsServer(self):
122 #
123 # self.socket_c.bind(self.addr)
117 124
118 125 def waitData(self, nbytes = 1024):
119 126
120 127 print "\nWaiting some client."
121 128
122 129 if self.mode == "client":
123 130 # Short data through ethernet
124 131 trama_rx = self.socket_c.recv(nbytes)
125 132 else:
126 133 self.socket_c.listen(1)
127 134 sc, addr = self.socket_c.accept()
128 135 self.sc = sc
129 136 self.answer = addr
130 137 # Big data through ethernet
131 138 trama_rx = ""
132 139 while True:
133 140 tmp = self.sc.recv(nbytes)
134 141 trama_rx = trama_rx + tmp
135 142 if trama_rx[-4:] == "quit":
136 143 break
137 144
138 print "\nThis socket has received some data from:"
139 print self.answer
145 print "\nThis socket has received some data."
140 146
141 147 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
142 148
143 149 return ipSource, ipDestino, cmd, data
144 150
145 def sendData(self, cmd, data):
146
151 def sendData(self, cmd, data, id):
152
153 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc)
154
147 155 if self.portDestino == 7000:
148 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" + "quit"
149 else:
150 trama_tx = data
156 trama_tx = trama_tx + ":" + "quit"
151 157 # Send messages
152 158 if self.mode == "client":
159 host = "192.168.1." + str(id)
160 self.socket_c.connect((host, self.portDestino))
153 161 self.socket_c.send(trama_tx)
154 162 else:
155 163 self.sc.send(trama_tx)
156 164 print "Sending message:[" + trama_tx + "]"
157 165
158 166 def __getTrama(self, trama):
159 167
160 168 FrameList = trama.split(':')
161 169
162 170 header = FrameList[0]
163 ipSource = FrameList[1]
164 ipDestino = FrameList[2]
165 cmd = FrameList[3]
166 data = FrameList[4]
167 trash = FrameList[5]
171 TypeOfInstrument = FrameList[1]
172 ipSource = FrameList[2]
173 ipDestino = FrameList[3]
174 len = FrameList[4]
175 cmd = FrameList[5]
176 data = FrameList[6]
177 crc = FrameList[7]
178 trash = FrameList[8]
168 179
169 180 return ipSource, ipDestino, cmd, data
170 181
171 182 def close_socket(self):
172 183 self.socket_c.close()
184
185 def open_socket(self):
186 self.socket_c = socket.socket()
173 187
174 188 #class FTPComm:
175 189 #
176 190 # ftp_servidor = 'ftp.servidor.com'
177 191 # ftp_usuario = 'miusuario'
178 192 # ftp_clave = 'miclave'
179 193 # ftp_raiz = '/public_html'
180 194 #
181 195 # def __init__(self):
182 196 #
183 197 # self.client = tftpy.TftpClient(self.ftp_servidor, '69')
184 198 #
185 199 #
186 200 # def sendFile(self, filename):
187 201 #
188 202 # self.client.upload(filename)
189 203 #
190 204 #if __name__ == '__main__':
191 205 #
192 206 # obj = FTPComm()
193 207
194 208
195 209 No newline at end of file
@@ -1,280 +1,369
1 1 import os
2 import library
2 import library2
3 3 import time
4 4
5 5 class ABSServer:
6 6
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000, ipDestino2="192.168.1.225", portDestino2=5500, ftpPortDestino=None):
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
8 8
9 9 self.ipSource = ipSource
10 10 self.ipDestino = ipDestino
11 11 self.portDestino = portDestino
12 12
13 13 self.ipDestino2 = ipDestino2
14 14 self.portDestino2 = portDestino2
15 15
16 self.ftpPortDestino = ftpPortDestino
17 self.experiment_name = "default"
18 16 self.tx_buffer = "default"
17 self.rx_buffer = "default"
19 18
20 19 self.createObjects()
21 20
22 21 def createObjects(self):
23 22
24 23 asServer = True
25 self.commServerObj = library.TCPComm(self.ipSource, self.ipDestino, self.portDestino, asServer)
26 self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2)
27 #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino)
28
24 self.commServerObj = library2.TCPComm("Central_Control", self.ipDestino, self.portDestino, asServer)
25 self.commClientObj = library2.TCPComm("Central_Control", self.ipDestino2, self.portDestino2)
29 26
30 27 def waitRequest(self):
31 28
32 ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData()
33
34 datarpta = "OK"
29 # Using rx buffer
30 ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitServer()
35 31
36 32 if cmd == "SNDF":
37 self.sendFile2Modules()
33 datarpta = self.sendFile2Modules(mode=2, cmd = cmd)
38 34
39 35 if cmd == "CHGB":
40 self.changeBeam()
36 datarpta = self.changeBeam(cmd = cmd)
41 37
42 38 if cmd == "ANST":
43 self.getStatus(mode=3)
39 self.getStatus(mode=4, cmd = cmd)
40 # Using tx buffer
44 41 datarpta = self.tx_buffer
45 42
46 self.commServerObj.sendData(cmd=cmd, data=datarpta)
43 self.commServerObj.sendData2(cmd=cmd, data=datarpta, ipDestino = ipSource)
47 44
48 45 def checkModule(self, address):
49 46
50 47 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
51 48 status = os.system(cmd)
52 49
53 50 if status == 256:
54 51 return False
55 52
56 53 return True
57 54
58 55 def __writeReport(self, enaModules):
59 56
60 57 status_array = ["Status of modules\n"]
61 58 status_array.append("----------------\n")
62 59
63 60 for address in range(1,65):
64 61 if address in enaModules:
65 # status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n")
66 62 status_array.append("192.168.1." + str(address) + " [1 1]\n")
67 63 else:
68 64 status_array.append("192.168.1." + str(address) + " [0 0]\n")
69 65
70 66 f = open("module_status.txt","w")
71 67 f.writelines(status_array)
72 68 f.close()
73 69
74 70 def checkAntenna(self):
75 71
76 72 """
77 73 Direccion de los modulos de las antenas:
78 74
79 75 Norte : 01-16
80 76 Este : 17-32
81 77 Oeste: : 33-48
82 78 Sur : 49-64
83 79
84 80 """
85 81
86 82 enaModules = []
87 83
88 84 for address in range(1,65):
89 85 if self.checkModule(address):
90 86 enaModules.append(address)
91 87
92 88 self.__writeReport(enaModules)
93 89 return enaModules
94 90
95 def sendFile2Modules(self):
91 def sendFile2Modules(self, mode, cmd):
92
93 if mode == 1:
94 self.__sendFile2Modules1()
95 else:
96 self.__sendFile2Modules2(cmd)
97
98 def __sendFile2Modules1(self):
96 99
97 100 #Needed for the loop
98 rx_frame_list = self.datarx.split('\n',2)
101 rx_frame_list = self.rx_buffer.split('\n',2)
99 102
100 103 self.experiment_name = rx_frame_list[0]
101 104 experiment_number = rx_frame_list[1]
102 105 str_control_modules = rx_frame_list[2]
103 106
104 107 lst_control_modules = str_control_modules.split("------\n")
105 108
106 enaModules = self.checkAntenna()
109 # enaModules = self.checkAntenna()
110 enaModules = [11,12,13,14]
107 111
108 112 for address in range(1,65):
109 113
110 114 if address not in enaModules:
111 115 continue
112 116
113 117 self.__writeModuleFile(self.experiment_name, lst_control_modules[address-1])
114 118
115 119 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + self.experiment_name
116 120 print cmd
117 121 os.system(cmd)
118 122
119 123 self.__loadFile()
124
125 def __sendFile2Modules2(self,cmd):
126
127 #Needed for the loop
128 rx_frame_list = self.rx_buffer.split('\n',2)
129 correct = 0
130 failure = 0
131 header = rx_frame_list[0] + "\n"
132 str_control_modules = rx_frame_list[2]
133
134 lst_control_modules = str_control_modules.split("------\n")
135
136 # enaModules = self.checkAntenna()
137 enaModules = [11,12,13,14]
138
139 for id in range(1,65):
140
141 if id not in enaModules:
142 continue
143 #tcp client needed
144 self.commClientObj.open_socket()
145 ip = "192.168.1." + str(id)
146 self.commClientObj.sendData2(cmd, header + lst_control_modules[id-1], ip)
147 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient()
148 self.commClientObj.close_socket()
149
150 if tmp == "OK":
151 correct = correct + 1
152 else:
153 failure = failure + 1
154
155 if correct == len(enaModules):
156 rpta = "OK"
157 else:
158 rpta = "Failure"
159
160 return rpta
161
120 162
121 163 def __writeModuleFile(self, filename, str):
122 164
123 165 fobj = open(filename,"w")
124 166 fobj.write(filename + "\n")
125 167 fobj.write("------\n")
126 168 fobj.write(str)
127 169 fobj.write("------\n")
128 170 fobj.close()
129 171
130 172 def __readModuleFile(self, filename):
131 173
132 174 fobj1 = open(filename,"r")
133 175 file_list_1 = fobj1.readlines()
134 176 fobj1.close()
135 177 content_str = ''.join(file_list_1[2:-1])
136 178
137 179 return content_str
138 180
139 181 def __loadFile(self):
140 182
141 183 #Working with the UDP socket
142 184 self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":")
143 185 self.commClientObj.sendData("none", "CAMBIA:0:")
144 186
145 def changeBeam(self):
146
147 #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0")
148 self.commClientObj.sendData("none", "CAMBIA:" + self.datarx + ":")
149
150 def getStatus(self,mode):
187 def changeBeam(self, cmd):
188
189 correct = 0
190 failure = 0
191 # enaModules = self.checkAntenna()
192 enaModules = [11,12,13,14]
193
194 for id in range(1,65):
195 if id not in enaModules:
196 continue
197
198 self.commClientObj.open_socket()
199 ip = "192.168.1." + str(id)
200 self.commClientObj.sendData2(cmd, self.rx_buffer, ip)
201 # ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
202 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient()
203 self.commClientObj.close_socket()
204
205 if tmp == "OK":
206 correct = correct + 1
207 else:
208 failure = failure + 1
209
210 if correct == len(enaModules):
211 rpta = "OK"
212 else:
213 rpta = "Failure"
214
215 return rpta
216
217 def getStatus(self, mode, cmd):
151 218
152 219 if mode == 1:
153 220 self.__getStsMode1()
154 221 elif mode == 2:
155 222 self.__getStsMode2()
223 elif mode == 3:
224 self.__getStsMode3()
156 225 else:
157 self.__getStsMode3()
226 self.__getStsMode4(cmd)
158 227
159 228
160 229 def __getStsMode1(self):
161 230 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
162 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
163 seconds = int (self.datarx)
231 self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":")
232 seconds = int (self.rx_buffer)
164 233 # Give 5 seconds to the control modules
165 234 time.sleep(seconds)
166 235 # Checking the module connection
167 236 module_list = self.connection_status(10)
168 237 #Generating the complete report
169 238 module = 1
170 239 number_of_modules = 16
171 240 filename1 = "Verificacion"
172 241 filename2 = "report.txt"
173 242 fobj2 = open(filename2,"w")
174 243 fobj2.write("Verification_file\n")
175 244 fobj2.write("-----------------\n")
176 245 fobj2.close()
177 246 while module <= number_of_modules:
178 247 if module_list[module -1] == "1":
179 248 #Preparing and doing the tftp command
180 249 cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1
181 250 print cmd
182 251 os.system(cmd)
183 252 # Getting data from the control module file
184 253 fobj1 = open(filename1,"r")
185 254 file_list_1 = fobj1.readlines()
186 255 fobj1.close()
187 256 content = file_list_1[2:-1]
188 257 #
189 258 fobj2 = open(filename2,"a")
190 259 if base == 10:
191 260 fobj2.write("S" + str(module) + "\n")
192 261 else:
193 262 fobj2.write("N" + str(module) + "\n")
194 263 fobj2.writelines(content)
195 264 fobj2.write("------\n")
196 265 fobj2.close()
197 266 module = module + 1
198 267
199 268 def __getStsMode2(self):
200 269
201 270 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
202 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
203 seconds = int (self.datarx)
271 self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":")
272 seconds = int (self.rx_buffer)
204 273 # Give 5 seconds to the control modules
205 274 time.sleep(seconds)
206 275 # Checking the module connection
207 276 enaModules = self.checkAntenna()
208 277 #Generating the complete report
209 278 filename1 = "Verificacion"
210 279 line1 = "Verification_file\n"
211 280 line2 = "-----------------\n"
212 281 report_list = [line1, line2]
213 282
214 283 for address in range(1,65):
215 284
216 285 if address not in enaModules:
217 286 continue
218 287 #Preparing and doing the tftp command
219 288 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename1
220 289 print cmd
221 290 os.system(cmd)
222 291 #Sub_header
223 292 report_list.append("ABS_" + str(address) + "\n")
224 293 # Content
225 294 fobj1 = open(filename1,"r")
226 295 file_list_1 = fobj1.readlines()
227 296 fobj1.close()
228 297 content = ''.join(file_list_1[2:-1])
229 298 report_list.append(content)
230 299 #Ending
231 300 report_list.append("------\n")
232 301 #print "\nFinalizado"
233 302 self.tx_buffer = ''.join(report_list)
234 303
235 304 def __AddingHeader(self,content_list, title):
236 305
237 306 line1 = title + "\n"
238 307 line2 = "-----------------\n"
239 308 header_list = [line1, line2]
240 309 verification_list = header_list + content_list
241 310 # Arming the frame
242 311 self.tx_buffer = ''.join(verification_list)
243 312
244 313 def __getModuleFile(self, filename):
245 314
246 315 enaModules = self.checkAntenna()
247 316 content_list = []
248 317 for address in range(1,65):
249 318
250 319 if address not in enaModules:
251 320 continue
252 321 #Preparing and doing the tftp command
253 322 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename
254 323 print cmd
255 324 os.system(cmd)
256 325 #Sub_header
257 326 content_list.append("ABS_" + str(address) + "\n")
258 327 # From module file to list
259 328 content_str = self.__readModuleFile(filename)
260 329 content_list.append(content_str)
261 330 content_list.append("------\n")
262 331
263 332 self.__AddingHeader(content_list, title = "Verification_file")
264 333
265 334 def __getStsMode3(self):
266 335
267 self.commClientObj.sendData("none", "CHEQUEO:" + self.datarx + ":")
268 seconds = int (self.datarx)
336 self.commClientObj.sendData("none", "CHEQUEO:" + self.rx_buffer + ":")
337 seconds = int (self.rx_buffer)
269 338 # Give 5 seconds to the control modules
270 339 time.sleep(seconds)
271 340
272 341 self.__getModuleFile(filename = "Verificacion")
273
342
343 def __getStsMode4(self, cmd):
344
345 content_str = ""
346 # enaModules = self.checkAntenna()
347 enaModules = [11,12,13,14]
348
349 for id in range(1,65):
350 if id not in enaModules:
351 continue
352
353 self.commClientObj.open_socket()
354 ip = "192.168.1." + str(id)
355 self.commClientObj.sendData2(cmd, self.rx_buffer, ip)
356 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient()
357 self.commClientObj.close_socket()
358
359 content_str = content_str + tmp
360 # self.__AddingHeader(content_list, title = "Verification_file")
361 #Using tx buffer
362 self.tx_buffer = content_str
274 363
275 364 if __name__ == '__main__':
276 365
277 366 absObj = ABSServer()
278 367
279 368 while 1:
280 369 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now