@@ -0,0 +1,500 | |||
|
1 | # imports needed for the file convertion | |
|
2 | import os | |
|
3 | import sys | |
|
4 | import time | |
|
5 | import numpy as np | |
|
6 | ||
|
7 | import library3 | |
|
8 | ||
|
9 | class ABSClient: | |
|
10 | ||
|
11 | def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000): | |
|
12 | ||
|
13 | self.ipSource = ipSource | |
|
14 | self.ipDestino = ipDestino | |
|
15 | self.portDestino = portDestino | |
|
16 | ||
|
17 | self.createObjects() | |
|
18 | ||
|
19 | def createObjects(self): | |
|
20 | ||
|
21 | self.commObj = library3.TCPComm(self.ipSource, self.ipDestino, self.portDestino) | |
|
22 | ||
|
23 | def sendFile(self, filename): | |
|
24 | ||
|
25 | #From matriz to control module format | |
|
26 | self.FuncionMaestra_GeneraFormatoControlCentral(filename) | |
|
27 | FileName = "FormatoControlCentral.txt" | |
|
28 | F_Obj = open(FileName,"r") | |
|
29 | FileList = F_Obj.readlines() | |
|
30 | F_Obj.close() | |
|
31 | FileStr = "".join(FileList) | |
|
32 | data = FileStr | |
|
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() | |
|
39 | self.commObj.close_socket() | |
|
40 | ||
|
41 | def changeBeam(self, newBeam): | |
|
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() | |
|
48 | self.commObj.close_socket() | |
|
49 | ||
|
50 | def __writeFile(self, filename, data): | |
|
51 | ||
|
52 | fobj = open(filename,"w") | |
|
53 | fobj.writelines(data) | |
|
54 | fobj.close() | |
|
55 | ||
|
56 | def getStatus(self, data): | |
|
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() | |
|
63 | self.commObj.close_socket() | |
|
64 | self.__writeFile("report.txt", data) | |
|
65 | ||
|
66 | ########## | |
|
67 | ||
|
68 | def FuncionMaestra_GeneraFormatoControlCentral(self,archivo): | |
|
69 | """ Funcion que genera un archivo para el control central""" | |
|
70 | ||
|
71 | # CarpetaDeTrabajo='/home/redes/ABS_Control_2012_09_24/Control_Module_v1_Client_09_24/' | |
|
72 | CarpetaDeTrabajo = os.getcwd() + '/' | |
|
73 | #print CarpetaDeTrabajo | |
|
74 | #CarpetaDeTrabajo='/home/redes/workspace/ABS_Client_v2/Debug/' | |
|
75 | ||
|
76 | def lst2string(lst): | |
|
77 | string='' | |
|
78 | for i in lst: | |
|
79 | string=string+i | |
|
80 | return string | |
|
81 | ||
|
82 | def string2lst(string): | |
|
83 | lst = [] | |
|
84 | for i in string: | |
|
85 | lst.append(i) | |
|
86 | return lst | |
|
87 | ||
|
88 | ||
|
89 | def file1(string, type): | |
|
90 | lst = string2lst(archivo) | |
|
91 | fin = -1 | |
|
92 | t = len(lst) | |
|
93 | for i in np.arange(-1,-t,-1): | |
|
94 | if lst[i]=='/': | |
|
95 | fin=i | |
|
96 | break | |
|
97 | if type == '1': | |
|
98 | nombre2 = lst[fin+1:] | |
|
99 | nombre2[-1]='s' | |
|
100 | nombre2 = lst2string(nombre2) | |
|
101 | return nombre2 | |
|
102 | if type == '2': | |
|
103 | nombre2 = lst[fin+1:] | |
|
104 | nombre2[-1]='1' | |
|
105 | nombre2 = lst2string(nombre2) | |
|
106 | return nombre2 | |
|
107 | ||
|
108 | ||
|
109 | def EliminaSaltoDeLinea(cadena): | |
|
110 | i = 0 | |
|
111 | for elemento in cadena: | |
|
112 | if elemento =='\n' or elemento =='\r': | |
|
113 | pass | |
|
114 | else: | |
|
115 | i=i+1 | |
|
116 | return cadena [:i] | |
|
117 | ||
|
118 | def NumeroDeExperimentos(path): | |
|
119 | fichero1=open(path,'r') | |
|
120 | cont=0 | |
|
121 | for cadena in fichero1: | |
|
122 | cont=cont+1 | |
|
123 | if cont==3: | |
|
124 | nexp='' | |
|
125 | pos=0 | |
|
126 | for elemento in cadena: | |
|
127 | pos=pos+1 | |
|
128 | if elemento=='=': | |
|
129 | nexp=int(cadena[pos:]) | |
|
130 | return nexp | |
|
131 | fichero1.close() | |
|
132 | ||
|
133 | def Paridad(numero): | |
|
134 | if numero%2==0: return 'par' | |
|
135 | elif numero%2==1: return 'impar' | |
|
136 | ||
|
137 | def EvaluaCadena(cadena): | |
|
138 | if len(cadena)>35: | |
|
139 | if cadena[-1]=='$': | |
|
140 | return cadena[-35:-2] | |
|
141 | elif cadena[-1]==']': | |
|
142 | return cadena[-34:-1] | |
|
143 | else: | |
|
144 | return None | |
|
145 | ||
|
146 | def GuardaEnLista(path): | |
|
147 | fichero=open(path,'r') | |
|
148 | lista=[] | |
|
149 | for cadena in fichero: | |
|
150 | cadena = EliminaSaltoDeLinea(cadena) | |
|
151 | cadena = EvaluaCadena(cadena) | |
|
152 | if cadena != None: | |
|
153 | lista.append(cadena) | |
|
154 | fichero.close() | |
|
155 | return lista | |
|
156 | ||
|
157 | def CreaFicherosPrevios(): | |
|
158 | vector = GuardaEnLista(archivo) | |
|
159 | for i in range(1,NumeroDeExperimentos(archivo)+1): | |
|
160 | fichero =open(CarpetaDeTrabajo+str(i)+'.txt','w') | |
|
161 | for j in range(0,16): | |
|
162 | fichero.write(vector[j+16*(i-1)]+'\n') | |
|
163 | fichero.close() | |
|
164 | ||
|
165 | def CapturaValoresEnArchivo(path,polarizacion='up'): | |
|
166 | fichero =open(path,'r') | |
|
167 | cnt=0 | |
|
168 | lstup=[] | |
|
169 | lstdw=[] | |
|
170 | for cadena in fichero: | |
|
171 | cnt=cnt+1 | |
|
172 | if cnt==5: | |
|
173 | su01=cadena[17:20] | |
|
174 | su02=cadena[21:24] | |
|
175 | su03=cadena[25:28] | |
|
176 | su04=cadena[29:32] | |
|
177 | if cnt==6: | |
|
178 | su05=cadena[17:20] | |
|
179 | su06=cadena[21:24] | |
|
180 | su07=cadena[25:28] | |
|
181 | su08=cadena[29:32] | |
|
182 | if cnt==7: | |
|
183 | su09=cadena[17:20] | |
|
184 | su10=cadena[21:24] | |
|
185 | su11=cadena[25:28] | |
|
186 | su12=cadena[29:32] | |
|
187 | if cnt==8: | |
|
188 | su13=cadena[17:20] | |
|
189 | su14=cadena[21:24] | |
|
190 | su15=cadena[25:28] | |
|
191 | su16=cadena[29:32] | |
|
192 | if cnt==13: | |
|
193 | sd01=cadena[17:20] | |
|
194 | sd02=cadena[21:24] | |
|
195 | sd03=cadena[25:28] | |
|
196 | sd04=cadena[29:32] | |
|
197 | if cnt==14: | |
|
198 | sd05=cadena[17:20] | |
|
199 | sd06=cadena[21:24] | |
|
200 | sd07=cadena[25:28] | |
|
201 | sd08=cadena[29:32] | |
|
202 | if cnt==15: | |
|
203 | sd09=cadena[17:20] | |
|
204 | sd10=cadena[21:24] | |
|
205 | sd11=cadena[25:28] | |
|
206 | sd12=cadena[29:32] | |
|
207 | if cnt==16: | |
|
208 | sd13=cadena[17:20] | |
|
209 | sd14=cadena[21:24] | |
|
210 | sd15=cadena[25:28] | |
|
211 | sd16=cadena[29:32] | |
|
212 | lstup=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16] | |
|
213 | lstdw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16] | |
|
214 | if polarizacion=='up': | |
|
215 | return lstup | |
|
216 | elif polarizacion=='dw': | |
|
217 | return lstdw | |
|
218 | fichero.close() | |
|
219 | ||
|
220 | def CapturaValoresEnArchivo2(path,polarizacion='up'): | |
|
221 | fichero =open(path,'r') | |
|
222 | cnt=0 | |
|
223 | lstup=[] | |
|
224 | lstdw=[] | |
|
225 | for cadena in fichero: | |
|
226 | cnt=cnt+1 | |
|
227 | if cnt==1: | |
|
228 | nu01=cadena[1:4] | |
|
229 | nu02=cadena[5:8] | |
|
230 | nu03=cadena[9:12] | |
|
231 | nu04=cadena[13:16] | |
|
232 | eu01=cadena[17:20] | |
|
233 | eu02=cadena[21:24] | |
|
234 | eu03=cadena[25:28] | |
|
235 | eu04=cadena[29:32] | |
|
236 | if cnt==2: | |
|
237 | nu05=cadena[1:4] | |
|
238 | nu06=cadena[5:8] | |
|
239 | nu07=cadena[9:12] | |
|
240 | nu08=cadena[13:16] | |
|
241 | eu05=cadena[17:20] | |
|
242 | eu06=cadena[21:24] | |
|
243 | eu07=cadena[25:28] | |
|
244 | eu08=cadena[29:32] | |
|
245 | if cnt==3: | |
|
246 | nu09=cadena[1:4] | |
|
247 | nu10=cadena[5:8] | |
|
248 | nu11=cadena[9:12] | |
|
249 | nu12=cadena[13:16] | |
|
250 | eu09=cadena[17:20] | |
|
251 | eu10=cadena[21:24] | |
|
252 | eu11=cadena[25:28] | |
|
253 | eu12=cadena[29:32] | |
|
254 | if cnt==4: | |
|
255 | nu13=cadena[1:4] | |
|
256 | nu14=cadena[5:8] | |
|
257 | nu15=cadena[9:12] | |
|
258 | nu16=cadena[13:16] | |
|
259 | eu13=cadena[17:20] | |
|
260 | eu14=cadena[21:24] | |
|
261 | eu15=cadena[25:28] | |
|
262 | eu16=cadena[29:32] | |
|
263 | if cnt==5: | |
|
264 | wu01=cadena[1:4] | |
|
265 | wu02=cadena[5:8] | |
|
266 | wu03=cadena[9:12] | |
|
267 | wu04=cadena[13:16] | |
|
268 | su01=cadena[17:20] | |
|
269 | su02=cadena[21:24] | |
|
270 | su03=cadena[25:28] | |
|
271 | su04=cadena[29:32] | |
|
272 | if cnt==6: | |
|
273 | wu05=cadena[1:4] | |
|
274 | wu06=cadena[5:8] | |
|
275 | wu07=cadena[9:12] | |
|
276 | wu08=cadena[13:16] | |
|
277 | su05=cadena[17:20] | |
|
278 | su06=cadena[21:24] | |
|
279 | su07=cadena[25:28] | |
|
280 | su08=cadena[29:32] | |
|
281 | if cnt==7: | |
|
282 | wu09=cadena[1:4] | |
|
283 | wu10=cadena[5:8] | |
|
284 | wu11=cadena[9:12] | |
|
285 | wu12=cadena[13:16] | |
|
286 | su09=cadena[17:20] | |
|
287 | su10=cadena[21:24] | |
|
288 | su11=cadena[25:28] | |
|
289 | su12=cadena[29:32] | |
|
290 | if cnt==8: | |
|
291 | wu13=cadena[1:4] | |
|
292 | wu14=cadena[5:8] | |
|
293 | wu15=cadena[9:12] | |
|
294 | wu16=cadena[13:16] | |
|
295 | su13=cadena[17:20] | |
|
296 | su14=cadena[21:24] | |
|
297 | su15=cadena[25:28] | |
|
298 | su16=cadena[29:32] | |
|
299 | if cnt==9: | |
|
300 | nd01=cadena[1:4] | |
|
301 | nd02=cadena[5:8] | |
|
302 | nd03=cadena[9:12] | |
|
303 | nd04=cadena[13:16] | |
|
304 | ed01=cadena[17:20] | |
|
305 | ed02=cadena[21:24] | |
|
306 | ed03=cadena[25:28] | |
|
307 | ed04=cadena[29:32] | |
|
308 | if cnt==10: | |
|
309 | nd05=cadena[1:4] | |
|
310 | nd06=cadena[5:8] | |
|
311 | nd07=cadena[9:12] | |
|
312 | nd08=cadena[13:16] | |
|
313 | ed05=cadena[17:20] | |
|
314 | ed06=cadena[21:24] | |
|
315 | ed07=cadena[25:28] | |
|
316 | ed08=cadena[29:32] | |
|
317 | if cnt==11: | |
|
318 | nd09=cadena[1:4] | |
|
319 | nd10=cadena[5:8] | |
|
320 | nd11=cadena[9:12] | |
|
321 | nd12=cadena[13:16] | |
|
322 | ed09=cadena[17:20] | |
|
323 | ed10=cadena[21:24] | |
|
324 | ed11=cadena[25:28] | |
|
325 | ed12=cadena[29:32] | |
|
326 | if cnt==12: | |
|
327 | nd13=cadena[1:4] | |
|
328 | nd14=cadena[5:8] | |
|
329 | nd15=cadena[9:12] | |
|
330 | nd16=cadena[13:16] | |
|
331 | ed13=cadena[17:20] | |
|
332 | ed14=cadena[21:24] | |
|
333 | ed15=cadena[25:28] | |
|
334 | ed16=cadena[29:32] | |
|
335 | if cnt==13: | |
|
336 | wd01=cadena[1:4] | |
|
337 | wd02=cadena[5:8] | |
|
338 | wd03=cadena[9:12] | |
|
339 | wd04=cadena[13:16] | |
|
340 | sd01=cadena[17:20] | |
|
341 | sd02=cadena[21:24] | |
|
342 | sd03=cadena[25:28] | |
|
343 | sd04=cadena[29:32] | |
|
344 | if cnt==14: | |
|
345 | wd05=cadena[1:4] | |
|
346 | wd06=cadena[5:8] | |
|
347 | wd07=cadena[9:12] | |
|
348 | wd08=cadena[13:16] | |
|
349 | sd05=cadena[17:20] | |
|
350 | sd06=cadena[21:24] | |
|
351 | sd07=cadena[25:28] | |
|
352 | sd08=cadena[29:32] | |
|
353 | if cnt==15: | |
|
354 | wd09=cadena[1:4] | |
|
355 | wd10=cadena[5:8] | |
|
356 | wd11=cadena[9:12] | |
|
357 | wd12=cadena[13:16] | |
|
358 | sd09=cadena[17:20] | |
|
359 | sd10=cadena[21:24] | |
|
360 | sd11=cadena[25:28] | |
|
361 | sd12=cadena[29:32] | |
|
362 | if cnt==16: | |
|
363 | wd13=cadena[1:4] | |
|
364 | wd14=cadena[5:8] | |
|
365 | wd15=cadena[9:12] | |
|
366 | wd16=cadena[13:16] | |
|
367 | sd13=cadena[17:20] | |
|
368 | sd14=cadena[21:24] | |
|
369 | sd15=cadena[25:28] | |
|
370 | sd16=cadena[29:32] | |
|
371 | lst_n_up=[nu01,nu02,nu03,nu04,nu05,nu06,nu07,nu08,nu09,nu10,nu11,nu12,nu13,nu14,nu15,nu16] | |
|
372 | lst_n_dw=[nd01,nd02,nd03,nd04,nd05,nd06,nd07,nd08,nd09,nd10,nd11,nd12,nd13,nd14,nd15,nd16] | |
|
373 | lst_s_up=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16] | |
|
374 | lst_s_dw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16] | |
|
375 | lst_w_up=[wu01,wu02,wu03,wu04,wu05,wu06,wu07,wu08,wu09,wu10,wu11,wu12,wu13,wu14,wu15,wu16] | |
|
376 | lst_w_dw=[wd01,wd02,wd03,wd04,wd05,wd06,wd07,wd08,wd09,wd10,wd11,wd12,wd13,wd14,wd15,wd16] | |
|
377 | lst_e_up=[eu01,eu02,eu03,eu04,eu05,eu06,eu07,eu08,eu09,eu10,eu11,eu12,eu13,eu14,eu15,eu16] | |
|
378 | lst_e_dw=[ed01,ed02,ed03,ed04,ed05,ed06,ed07,ed08,ed09,ed10,ed11,ed12,ed13,ed14,ed15,ed16] | |
|
379 | ||
|
380 | lstup = lst_s_up + lst_w_up + lst_n_up + lst_e_up | |
|
381 | lstdw = lst_s_dw + lst_w_dw + lst_n_up + lst_e_up | |
|
382 | ||
|
383 | if polarizacion=='up': | |
|
384 | return lstup | |
|
385 | elif polarizacion=='dw': | |
|
386 | return lstdw | |
|
387 | fichero.close() | |
|
388 | ||
|
389 | ||
|
390 | def CreaFormatoFinal(): | |
|
391 | ne=NumeroDeExperimentos(archivo) | |
|
392 | ||
|
393 | #nombre01 = file1(archivo,'1') | |
|
394 | nombre02 = file1(archivo,'2') | |
|
395 | fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w') | |
|
396 | fichero.write(nombre02+'\n') | |
|
397 | fichero.write(str(ne)+'\n') | |
|
398 | for i in range(1,17): | |
|
399 | ||
|
400 | if i<10: | |
|
401 | nmod = '0'+str(i) | |
|
402 | else: nmod = str(i) | |
|
403 | ||
|
404 | ||
|
405 | fichero.write('S'+nmod+'\n') | |
|
406 | for j in range(1,ne+1): | |
|
407 | ruta=CarpetaDeTrabajo+str(j)+'.txt' | |
|
408 | lu=CapturaValoresEnArchivo(ruta,polarizacion='up') | |
|
409 | ld=CapturaValoresEnArchivo(ruta,polarizacion='dw') | |
|
410 | part1='' | |
|
411 | part2='' | |
|
412 | if lu[i-1]=='1.0': part1='000' | |
|
413 | if lu[i-1]=='2.0': part1='001' | |
|
414 | if lu[i-1]=='3.0': part1='010' | |
|
415 | if lu[i-1]=='0.0': part1='011' | |
|
416 | if lu[i-1]=='0.5': part1='100' | |
|
417 | if lu[i-1]=='1.5': part1='101' | |
|
418 | if lu[i-1]=='2.5': part1='110' | |
|
419 | if lu[i-1]=='3.5': part1='111' | |
|
420 | if ld[i-1]=='1.0': part2='000' | |
|
421 | if ld[i-1]=='2.0': part2='001' | |
|
422 | if ld[i-1]=='3.0': part2='010' | |
|
423 | if ld[i-1]=='0.0': part2='011' | |
|
424 | if ld[i-1]=='0.5': part2='100' | |
|
425 | if ld[i-1]=='1.5': part2='101' | |
|
426 | if ld[i-1]=='2.5': part2='110' | |
|
427 | if ld[i-1]=='3.5': part2='111' | |
|
428 | fichero.write(part1+part2+'\n') | |
|
429 | fichero.write('------'+'\n') | |
|
430 | fichero.close() | |
|
431 | ||
|
432 | def CreaFormatoFinal2(): | |
|
433 | ne=NumeroDeExperimentos(archivo) | |
|
434 | ||
|
435 | #nombre01 = file1(archivo,'1') | |
|
436 | nombre02 = file1(archivo,'2') | |
|
437 | fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w') | |
|
438 | fichero.write(nombre02+'\n') | |
|
439 | fichero.write(str(ne)+'\n') | |
|
440 | ||
|
441 | for i in range(1,65): | |
|
442 | ||
|
443 | if i<10: | |
|
444 | nmod = '0'+str(i) | |
|
445 | else: nmod = str(i) | |
|
446 | ||
|
447 | fichero.write("ABS_" + nmod+'\n') | |
|
448 | ||
|
449 | for j in range(1,ne+1): | |
|
450 | ruta=CarpetaDeTrabajo+str(j)+'.txt' | |
|
451 | lu=CapturaValoresEnArchivo2(ruta,polarizacion='up') | |
|
452 | ld=CapturaValoresEnArchivo2(ruta,polarizacion='dw') | |
|
453 | part1='' | |
|
454 | part2='' | |
|
455 | if lu[i-1]=='1.0': part1='000' | |
|
456 | if lu[i-1]=='2.0': part1='001' | |
|
457 | if lu[i-1]=='3.0': part1='010' | |
|
458 | if lu[i-1]=='0.0': part1='011' | |
|
459 | if lu[i-1]=='0.5': part1='100' | |
|
460 | if lu[i-1]=='1.5': part1='101' | |
|
461 | if lu[i-1]=='2.5': part1='110' | |
|
462 | if lu[i-1]=='3.5': part1='111' | |
|
463 | if ld[i-1]=='1.0': part2='000' | |
|
464 | if ld[i-1]=='2.0': part2='001' | |
|
465 | if ld[i-1]=='3.0': part2='010' | |
|
466 | if ld[i-1]=='0.0': part2='011' | |
|
467 | if ld[i-1]=='0.5': part2='100' | |
|
468 | if ld[i-1]=='1.5': part2='101' | |
|
469 | if ld[i-1]=='2.5': part2='110' | |
|
470 | if ld[i-1]=='3.5': part2='111' | |
|
471 | fichero.write(part1+part2+'\n') | |
|
472 | fichero.write('------'+'\n') | |
|
473 | fichero.close() | |
|
474 | ||
|
475 | def EliminaArchivosEnLaCarpeta(): | |
|
476 | ne=NumeroDeExperimentos(archivo) | |
|
477 | for i in range(1,ne+1): | |
|
478 | os.remove(CarpetaDeTrabajo+str(i)+'.txt') | |
|
479 | ||
|
480 | CreaFicherosPrevios() | |
|
481 | CreaFormatoFinal2() | |
|
482 | EliminaArchivosEnLaCarpeta() | |
|
483 | ||
|
484 | ########## | |
|
485 | ||
|
486 | if __name__ == '__main__': | |
|
487 | ||
|
488 | filename = "experimento1.abs" | |
|
489 | ||
|
490 | absObj = ABSClient() | |
|
491 | # absObj.sendFile(filename) | |
|
492 | # absObj.changeBeam("0") | |
|
493 | # absObj.changeBeam("1") | |
|
494 | # absObj.changeBeam("2") | |
|
495 | # absObj.changeBeam("3") | |
|
496 | # absObj.changeBeam("4") | |
|
497 | # absObj.changeBeam("5") | |
|
498 | # absObj.changeBeam("6") | |
|
499 | # absObj.changeBeam("7") | |
|
500 | absObj.getStatus(5) No newline at end of file |
@@ -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 |
General Comments 0
You need to be logged in to leave comments.
Login now