##// END OF EJS Templates
imanay -
r92:93
parent child
Show More
@@ -0,0 +1,165
1 import socket
No newline at end of file
2
No newline at end of file
3 class TCPComm:
No newline at end of file
4
No newline at end of file
5 __HEADER = "JRO"
No newline at end of file
6 __TYPE = "ABS"
No newline at end of file
7
No newline at end of file
8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
No newline at end of file
9
No newline at end of file
10 self.ipSource = ipSource
No newline at end of file
11 self.ipDestino = ipDestino
No newline at end of file
12 self.portDestino = portDestino
No newline at end of file
13 self.addr = (ipDestino,portDestino)
No newline at end of file
14
No newline at end of file
15 self.sc = "none"
No newline at end of file
16 self.answer = "none" #test
No newline at end of file
17 self.asServer = False
No newline at end of file
18 self.len = 0
No newline at end of file
19 self.crc = 0
No newline at end of file
20
No newline at end of file
21 self.openSocket(asServer)
No newline at end of file
22
No newline at end of file
23 def openSocket(self, asServer):
No newline at end of file
24
No newline at end of file
25 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
No newline at end of file
26 # self.socket_c = socket.socket()
No newline at end of file
27 # self.socket_c.connect((self.ipDestino, self.portDestino))
No newline at end of file
28
No newline at end of file
29 if asServer:
No newline at end of file
30 self.socket_c = socket.socket()
No newline at end of file
31 # self.configAsServer()
No newline at end of file
32 self.socket_c.bind(self.addr)
No newline at end of file
33 self.asServer = True
No newline at end of file
34 else:
No newline at end of file
35 # self.configAsClient()
No newline at end of file
36 self.asServer = False #Socket is opened at the sendData function
No newline at end of file
37
No newline at end of file
38 # def configAsClient(self):
No newline at end of file
39 #Buscar broadcast TCP
No newline at end of file
40 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
No newline at end of file
41 # self.socket_c.connect(self.addr)
No newline at end of file
42 # pass
No newline at end of file
43
No newline at end of file
44 # def configAsServer(self):
No newline at end of file
45 #
No newline at end of file
46 # self.socket_c.bind(self.addr)
No newline at end of file
47
No newline at end of file
48 def waitData(self, nbytes = 1024):
No newline at end of file
49
No newline at end of file
50 print "\nWaiting some client."
No newline at end of file
51
No newline at end of file
52 if self.asServer == False:
No newline at end of file
53 # Short data through ethernet
No newline at end of file
54 trama_rx = self.socket_c.recv(nbytes)
No newline at end of file
55 else:
No newline at end of file
56 self.socket_c.listen(1)
No newline at end of file
57 sc, addr = self.socket_c.accept()
No newline at end of file
58 self.sc = sc
No newline at end of file
59 self.answer = addr
No newline at end of file
60 # Big data through ethernet
No newline at end of file
61 trama_rx = ""
No newline at end of file
62 while True:
No newline at end of file
63 tmp = self.sc.recv(nbytes)
No newline at end of file
64 trama_rx = trama_rx + tmp
No newline at end of file
65 if trama_rx[-4:] == "quit":
No newline at end of file
66 break
No newline at end of file
67
No newline at end of file
68 print "\nThis socket has received some data."
No newline at end of file
69
No newline at end of file
70 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
No newline at end of file
71
No newline at end of file
72 return ipSource, ipDestino, cmd, data
No newline at end of file
73
No newline at end of file
74 def waitServer(self, nbytes = 1024):
No newline at end of file
75
No newline at end of file
76 print "\nWaiting some client."
No newline at end of file
77 self.socket_c.listen(1)
No newline at end of file
78 sc, addr = self.socket_c.accept()
No newline at end of file
79 self.sc = sc
No newline at end of file
80 self.answer = addr
No newline at end of file
81 # Big data through ethernet
No newline at end of file
82 trama_rx = ""
No newline at end of file
83 while True:
No newline at end of file
84 tmp = self.sc.recv(nbytes)
No newline at end of file
85 trama_rx = trama_rx + tmp
No newline at end of file
86 if trama_rx[-4:] == "quit":
No newline at end of file
87 break
No newline at end of file
88
No newline at end of file
89 print "\nThis socket has received some data from: " + str(self.answer)
No newline at end of file
90
No newline at end of file
91 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
No newline at end of file
92
No newline at end of file
93 return ipSource, ipDestino, cmd, data
No newline at end of file
94
No newline at end of file
95 def waitClient(self, nbytes = 1024):
No newline at end of file
96
No newline at end of file
97 print "\nWaiting the server."
No newline at end of file
98 # Short data through ethernet
No newline at end of file
99 trama_rx = self.socket_c.recv(nbytes)
No newline at end of file
100
No newline at end of file
101 print "\nThis socket has received this data: " + str(trama_rx)
No newline at end of file
102
No newline at end of file
103 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
No newline at end of file
104
No newline at end of file
105 return ipSource, ipDestino, cmd, data
No newline at end of file
106
No newline at end of file
107 def sendData(self, cmd, data, id):
No newline at end of file
108
No newline at end of file
109 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + \
No newline at end of file
110 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc)
No newline at end of file
111
No newline at end of file
112 if self.portDestino == 7000:
No newline at end of file
113 trama_tx = trama_tx + ":" + "quit"
No newline at end of file
114 # Send messages
No newline at end of file
115 if self.asServer == False:
No newline at end of file
116 host = "192.168.1." + str(id)
No newline at end of file
117 self.socket_c.connect((host, self.portDestino))
No newline at end of file
118 self.socket_c.send(trama_tx)
No newline at end of file
119 else:
No newline at end of file
120 self.sc.send(trama_tx)
No newline at end of file
121 print "Sending message:[" + trama_tx + "]"
No newline at end of file
122
No newline at end of file
123 def sendData2(self, cmd, data, ipDestino):
No newline at end of file
124
No newline at end of file
125 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \
No newline at end of file
126 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit"
No newline at end of file
127
No newline at end of file
128 if self.asServer == True:
No newline at end of file
129 self.SendAsServer(trama_tx)
No newline at end of file
130 else:
No newline at end of file
131 self.SendAsClient(ipDestino, trama_tx)
No newline at end of file
132
No newline at end of file
133 def SendAsServer(self, trama_tx):
No newline at end of file
134
No newline at end of file
135 self.sc.send(trama_tx)
No newline at end of file
136 print "Sending message:[" + trama_tx + "] to: " + str(self.answer)
No newline at end of file
137
No newline at end of file
138
No newline at end of file
139 def SendAsClient(self, ipDestino, trama_tx):
No newline at end of file
140
No newline at end of file
141 self.socket_c.connect((ipDestino, self.portDestino))
No newline at end of file
142 self.socket_c.send(trama_tx)
No newline at end of file
143 print "Sending message:[" + trama_tx + "] to: " + ipDestino
No newline at end of file
144
No newline at end of file
145 def __getTrama(self, trama):
No newline at end of file
146
No newline at end of file
147 FrameList = trama.split(':')
No newline at end of file
148
No newline at end of file
149 header = FrameList[0]
No newline at end of file
150 TypeOfInstrument = FrameList[1]
No newline at end of file
151 ipSource = FrameList[2]
No newline at end of file
152 ipDestino = FrameList[3]
No newline at end of file
153 len = FrameList[4]
No newline at end of file
154 cmd = FrameList[5]
No newline at end of file
155 data = FrameList[6]
No newline at end of file
156 crc = FrameList[7]
No newline at end of file
157 trash = FrameList[8]
No newline at end of file
158
No newline at end of file
159 return ipSource, ipDestino, cmd, data
No newline at end of file
160
No newline at end of file
161 def close_socket(self):
No newline at end of file
162 self.socket_c.close()
No newline at end of file
163
No newline at end of file
164 def open_socket(self):
No newline at end of file
165 self.socket_c = socket.socket() No newline at end of file
@@ -4,11 +4,11
4 import time No newline at end of file
4 import time
5 import numpy as np No newline at end of file
5 import numpy as np
6
6
No newline at end of file
7 import library2 No newline at end of file
7 import library No newline at end of file
8 No newline at end of file
8
9 class ABSClient: No newline at end of file
9 class ABSClient:
10
10
No newline at end of file
11 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000): No newline at end of file
11 def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000): No newline at end of file
12 No newline at end of file
12
13 self.ipSource = ipSource No newline at end of file
13 self.ipSource = ipSource
14 self.ipDestino = ipDestino No newline at end of file
14 self.ipDestino = ipDestino
@@ -78,7 +79,8
78 No newline at end of file
79
79 class TCPComm: No newline at end of file
80 class TCPComm:
80
81
No newline at end of file
82 __HEADER = "JRO"
81 __HEADER = "ABS" No newline at end of file
No newline at end of file
83 __TYPE = "ABS" No newline at end of file
82 No newline at end of file
84
83 def __init__(self, ipSource, ipDestino, portDestino, asServer=False): No newline at end of file
85 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
84 No newline at end of file
86
@@ -1,10 +1,10
1 import os
1 import os
No newline at end of file
2 import library2 No newline at end of file
2 import library No newline at end of file
3 import time No newline at end of file
3 import time
4 No newline at end of file
4
5 class ABSServer: No newline at end of file
5 class ABSServer:
6
6
No newline at end of file
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500): No newline at end of file
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000, ipDestino2="192.168.1.225", portDestino2=5500, ftpPortDestino=None): No newline at end of file
8 No newline at end of file
8
9 self.ipSource = ipSource No newline at end of file
9 self.ipSource = ipSource
10 self.ipDestino = ipDestino No newline at end of file
10 self.ipDestino = ipDestino
General Comments 0
You need to be logged in to leave comments. Login now