@@ -2,17 +2,19 | |||||
2 |
|
2 | |||
3 | class ABSClient: |
|
3 | class ABSClient: | |
4 |
|
4 | |||
5 |
def __init__(self,ipSource=" |
|
5 | def __init__(self,ipSource="192.168.1.117", iDSource="Clnt_01", ipDestino="192.168.1.117", iDDestino = "CeCnMod", portDestino=7000): | |
6 |
|
6 | |||
7 | self.ipSource = ipSource |
|
7 | self.ipSource = ipSource | |
|
8 | self.iDSource = iDSource | |||
8 | self.ipDestino = ipDestino |
|
9 | self.ipDestino = ipDestino | |
|
10 | self.iDDestino = iDDestino | |||
9 | self.portDestino = portDestino |
|
11 | self.portDestino = portDestino | |
10 |
|
12 | |||
11 | self.createObjects() |
|
13 | self.createObjects() | |
12 |
|
14 | |||
13 | def createObjects(self): |
|
15 | def createObjects(self): | |
14 |
|
16 | |||
15 | self.commObj = library3.TCPComm(self.ipSource, self.ipDestino, self.portDestino) |
|
17 | self.commObj = library3.TCPComm(self.ipSource, self.iDSource, self.ipDestino, self.iDDestino, self.portDestino) | |
16 | self.wFiles = library3.FilesStuff() |
|
18 | self.wFiles = library3.FilesStuff() | |
17 |
|
19 | |||
18 | def __ConnectionWithCentralControl(self, cmd, data): |
|
20 | def __ConnectionWithCentralControl(self, cmd, data): |
@@ -9,18 +9,20 | |||||
9 | __HEADER = "JRO" |
|
9 | __HEADER = "JRO" | |
10 | __TYPE = "ABS" |
|
10 | __TYPE = "ABS" | |
11 |
|
11 | |||
12 | def __init__(self, ipSource, ipDestino, 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.ipDestino = ipDestino |
|
16 | self.ipDestino = ipDestino | |
|
17 | self.iDDestino = iDDestino | |||
16 | self.portDestino = portDestino |
|
18 | self.portDestino = portDestino | |
17 | self.addr = (ipDestino,portDestino) |
|
19 | self.addr = (ipDestino,portDestino) | |
18 |
|
20 | |||
19 | self.sc = "none" |
|
21 | self.sc = "none" | |
20 | self.answer = "none" #test |
|
22 | self.answer = "none" #test | |
21 | self.asServer = False |
|
23 | self.asServer = False | |
22 | self.len = 0 |
|
24 | self.len = "000000" | |
23 | self.crc = 0 |
|
25 | self.crc = "0" | |
24 |
|
26 | |||
25 | self.openSocket(asServer) |
|
27 | self.openSocket(asServer) | |
26 |
|
28 | |||
@@ -51,15 +53,23 | |||||
51 |
|
53 | |||
52 | def waitData(self): |
|
54 | def waitData(self): | |
53 | if self.asServer == True: |
|
55 | if self.asServer == True: | |
54 | trama_rx = self.waitAsServer() |
|
56 | trama_rx, l = self.waitAsServer() | |
55 | else: |
|
57 | else: | |
56 | trama_rx = self.waitAsClient() |
|
58 | trama_rx, l = self.waitAsClient() | |
57 |
|
59 | |||
58 | ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) |
|
60 | ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx, l) | |
59 |
|
61 | |||
60 | return ipSource, ipDestino, cmd, data |
|
62 | return ipSource, ipDestino, cmd, data | |
61 |
|
63 | |||
62 |
def waitAs |
|
64 | def waitAsClient2(self, nbytes = 1024): | |
|
65 | print "\nWaiting the server." | |||
|
66 | # Short data through ethernet | |||
|
67 | trama_rx = self.socket_c.recv(nbytes) | |||
|
68 | print "\nThis socket has received this data: " + str(trama_rx) | |||
|
69 | ||||
|
70 | return trama_rx | |||
|
71 | ||||
|
72 | def waitAsServer2(self, nbytes = 1024): | |||
63 | print "\nWaiting some client." |
|
73 | print "\nWaiting some client." | |
64 | self.socket_c.listen(1) |
|
74 | self.socket_c.listen(1) | |
65 | sc, addr = self.socket_c.accept() |
|
75 | sc, addr = self.socket_c.accept() | |
@@ -75,17 +85,42 | |||||
75 |
|
85 | |||
76 | print "\nThis socket has received some data from: " + str(self.answer) |
|
86 | print "\nThis socket has received some data from: " + str(self.answer) | |
77 |
|
87 | |||
78 | return trama_rx |
|
88 | return trama_rx | |
|
89 | ||||
|
90 | def waitAsServer(self, nbytes = 1024): | |||
|
91 | print "\nWaiting some client." | |||
|
92 | self.socket_c.listen(1) | |||
|
93 | sc, addr = self.socket_c.accept() | |||
|
94 | self.sc = sc | |||
|
95 | self.answer = addr | |||
|
96 | # Big data through ethernet | |||
|
97 | cnt = 0; | |||
|
98 | first = 0; | |||
|
99 | trama_rx = "" | |||
|
100 | while True: | |||
|
101 | tmp = self.sc.recv(nbytes) | |||
|
102 | trama_rx = trama_rx + tmp | |||
|
103 | cnt = len(trama_rx) | |||
|
104 | if first == 0: | |||
|
105 | first = 1 | |||
|
106 | lng = int(trama_rx[20:26]) | |||
|
107 | if cnt == lng: | |||
|
108 | break | |||
|
109 | ||||
|
110 | print "\nThis socket has received some data from: " + str(self.answer) | |||
|
111 | ||||
|
112 | return trama_rx, lng | |||
79 |
|
113 | |||
80 | def waitAsClient(self, nbytes = 1024): |
|
114 | def waitAsClient(self, nbytes = 1024): | |
81 | print "\nWaiting the server." |
|
115 | print "\nWaiting the server." | |
82 | # Short data through ethernet |
|
116 | # Short data through ethernet | |
83 | trama_rx = self.socket_c.recv(nbytes) |
|
117 | trama_rx = self.socket_c.recv(nbytes) | |
|
118 | lng = int(trama_rx[20:26]) | |||
84 | print "\nThis socket has received this data: " + str(trama_rx) |
|
119 | print "\nThis socket has received this data: " + str(trama_rx) | |
85 |
|
120 | |||
86 | return trama_rx |
|
121 | return trama_rx, lng | |
87 |
|
122 | |||
88 | def sendData(self, cmd, data, ipDestino): |
|
123 | def sendData2(self, cmd, data, ipDestino): | |
89 |
|
124 | |||
90 | trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \ |
|
125 | trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \ | |
91 | ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit" |
|
126 | ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit" | |
@@ -94,6 +129,21 | |||||
94 | self.SendAsServer(trama_tx) |
|
129 | self.SendAsServer(trama_tx) | |
95 | else: |
|
130 | else: | |
96 | self.SendAsClient(ipDestino, trama_tx) |
|
131 | self.SendAsClient(ipDestino, trama_tx) | |
|
132 | ||||
|
133 | def sendData(self, cmd, data, ipDestino): | |||
|
134 | ||||
|
135 | trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \ | |||
|
136 | self.iDDestino + self.len + str(cmd) + str(data) + self.crc | |||
|
137 | ||||
|
138 | self.len = self.int2str(len(trama_tx)) | |||
|
139 | ||||
|
140 | trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \ | |||
|
141 | self.iDDestino + self.len + str(cmd) + str(data) + self.crc | |||
|
142 | ||||
|
143 | if self.asServer == True: | |||
|
144 | self.SendAsServer(trama_tx) | |||
|
145 | else: | |||
|
146 | self.SendAsClient(ipDestino, trama_tx) | |||
97 |
|
147 | |||
98 | def SendAsServer(self, trama_tx): |
|
148 | def SendAsServer(self, trama_tx): | |
99 |
|
149 | |||
@@ -107,7 +157,7 | |||||
107 | self.socket_c.send(trama_tx) |
|
157 | self.socket_c.send(trama_tx) | |
108 | print "Sending message:[" + trama_tx + "] to: " + ipDestino |
|
158 | print "Sending message:[" + trama_tx + "] to: " + ipDestino | |
109 |
|
159 | |||
110 | def __getTrama(self, trama): |
|
160 | def __getTrama2(self, trama): | |
111 |
|
161 | |||
112 | FrameList = trama.split(':') |
|
162 | FrameList = trama.split(':') | |
113 |
|
163 | |||
@@ -123,11 +173,41 | |||||
123 |
|
173 | |||
124 | return ipSource, ipDestino, cmd, data |
|
174 | return ipSource, ipDestino, cmd, data | |
125 |
|
175 | |||
|
176 | def __getTrama(self, trama, l): | |||
|
177 | ||||
|
178 | header = trama[0:3] | |||
|
179 | TypeOfInstrument = trama[3:6] | |||
|
180 | ipSource = trama[6:13] | |||
|
181 | ipDestino = trama[13:20] | |||
|
182 | len = trama[20:26] | |||
|
183 | cmd = trama[26:30] | |||
|
184 | ldata = l-31 | |||
|
185 | data = trama[30:30+ldata] | |||
|
186 | crc = trama[30+ldata:] | |||
|
187 | ||||
|
188 | return ipSource, ipDestino, cmd, data | |||
|
189 | ||||
126 | def close_socket(self): |
|
190 | def close_socket(self): | |
127 | self.socket_c.close() |
|
191 | self.socket_c.close() | |
128 |
|
192 | |||
129 | def open_socket(self): |
|
193 | def open_socket(self): | |
130 | self.socket_c = socket.socket() |
|
194 | self.socket_c = socket.socket() | |
|
195 | ||||
|
196 | def int2str(self, n): | |||
|
197 | ||||
|
198 | str_n = str(n) | |||
|
199 | l_n = len(str_n) | |||
|
200 | if l_n == 1: | |||
|
201 | str_n = "00000" + str_n | |||
|
202 | elif l_n == 2: | |||
|
203 | str_n = "0000" + str_n | |||
|
204 | elif l_n == 3: | |||
|
205 | str_n = "000" + str_n | |||
|
206 | elif l_n == 4: | |||
|
207 | str_n = "00" + str_n | |||
|
208 | elif l_n == 5: | |||
|
209 | str_n = "0" + str_n | |||
|
210 | return str_n | |||
131 |
|
211 | |||
132 | class FilesStuff(): |
|
212 | class FilesStuff(): | |
133 |
|
213 |
@@ -22,8 +22,8 | |||||
22 | def createObjects(self): |
|
22 | def createObjects(self): | |
23 |
|
23 | |||
24 | asServer = True |
|
24 | asServer = True | |
25 | self.commServerObj = library3.TCPComm("Central_Control", self.ipDestino, self.portDestino, asServer) |
|
25 | self.commServerObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino, "CnMod01", self.portDestino, asServer) | |
26 | self.commClientObj = library3.TCPComm("Central_Control", self.ipDestino2, 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 |
General Comments 0
You need to be logged in to leave comments.
Login now