Commit 3264ede5209d0f9027fcaa10125eaf82d37436e9

Authored by Alain Klotz
1 parent 05522f81
Exists in dev

Debut d'ajout pour protocole TCP.

Showing 1 changed file with 99 additions and 50 deletions   Show diff stats
mount_tnc/raspberry/mountchannel.py
1 1 # -*- coding: utf-8 -*-
2 2 import time
3 3 import serial
  4 +import socket
4 5  
5 6 #import RPi.GPIO as io
6 7  
7 8 # #####################################################################
8 9 # #####################################################################
9 10 # #####################################################################
10   -# Class Astromecca
  11 +# Class Mountchannel for communication clients
11 12 # #####################################################################
12 13 # #####################################################################
13 14 # #####################################################################
... ... @@ -26,6 +27,7 @@ class Mountchannel():
26 27 ERR_CHAN_NOT_OPENED = 205
27 28 ERR_CHAN_PORT_NOT_DEFINED = 206
28 29 ERR_CHAN_EVER_OPENED = 207
  30 + ERR_CHAN_HOSTNAME_NOT_DEFINED = 208
29 31  
30 32 # --- main communication
31 33 _fid_chan = None
... ... @@ -55,7 +57,7 @@ class Mountchannel():
55 57 # --- Dico of axis_types and their parameters
56 58 channel_types = {}
57 59 channel_types["SERIAL"]= {"MANDATORY" : {"PORT":[str,"//./COM1"]}, "OPTIONAL" : {"BAUD_RATE":[int,9600]} }
58   - #channel_types["ETHERNET"]= {"MANDATORY" : {"PORT":[int,"5000"], "HOSTNAME":[str,"127.0.0.1"]}, "OPTIONAL" : {} }
  60 + channel_types["TCP"]= {"MANDATORY" : {"PORT":[int,"5000"], "HOSTNAME":[str,"127.0.0.1"]}, "OPTIONAL" : {} }
59 61 # ---
60 62 argc = len(args)
61 63 # kwargc = len(kwargs)
... ... @@ -136,8 +138,10 @@ class Mountchannel():
136 138 if self._channel_type=="SERIAL":
137 139 self._port_chan = self._channel_params["PORT"]
138 140 self._baud_rate = self._channel_params["BAUD_RATE"]
  141 + if self._channel_type=="TCL":
  142 + self._port_chan = self._channel_params["PORT"]
  143 + self._hostname = self._channel_params["HOSTNAME"]
139 144  
140   -
141 145 def _set_port_chan(self, port:str):
142 146 self._port_chan = port
143 147 return self._port_chan
... ... @@ -145,6 +149,13 @@ class Mountchannel():
145 149 def _get_port_chan(self):
146 150 return self._port_chan
147 151  
  152 + def _set_hostname_chan(self, hostname:str):
  153 + self._hostname_chan = hostname
  154 + return self._hostname_chan
  155 +
  156 + def _get_hostname_chan(self):
  157 + return self._hostname_chan
  158 +
148 159 def _my_open_chan(self):
149 160 # --- abstract method
150 161 # --- Please overload it according your language protocol
... ... @@ -181,8 +192,28 @@ class Mountchannel():
181 192 self._fid_chan = fid
182 193 time.sleep(self._delay_init_chan)
183 194 fid.flush()
184   - else:
185   - pass
  195 + elif self._channel_type=="TCP":
  196 + fid = self._fid_chan
  197 + port = self._port_chan
  198 + if self._port_chan == None:
  199 + err = self.ERR_CHAN_PORT_NOT_DEFINED
  200 + return (err, fid)
  201 + hostname = self._hostname_chan
  202 + if self._hostname_chan == None:
  203 + err = self.ERR_CHAN_HOSTNAME_NOT_DEFINED
  204 + return (err, fid)
  205 + if self._fid_chan != None:
  206 + return (err, fid)
  207 + # --- pass here only if the port was closed
  208 + try:
  209 + fid = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  210 + # Connect to server and send data
  211 + fid.connect((hostname, port))
  212 + except:
  213 + err = self.ERR_CHAN_UNKNOWN
  214 + # --- some inits
  215 + if err == self.NO_ERROR:
  216 + self._fid_chan = fid
186 217 err = self._my_open_chan()
187 218 return err, fid
188 219  
... ... @@ -218,12 +249,16 @@ class Mountchannel():
218 249 err, fid = self.check_chan()
219 250 if err != self.NO_ERROR:
220 251 raise Exception("Error {}".format(err))
221   - # --- serial port buffer reset for the next read
222   - fid.reset_input_buffer()
223   - # --- serial port is ready to put message
224   - cmd = cmd + self._end_of_command_to_send
225   - fid.write(cmd.encode())
226   - fid.flush()
  252 + if self._channel_type=="SERIAL":
  253 + # --- serial port buffer reset for the next read
  254 + fid.reset_input_buffer()
  255 + # --- serial port is ready to put message
  256 + cmd = cmd + self._end_of_command_to_send
  257 + fid.write(cmd.encode())
  258 + fid.flush()
  259 + elif self._channel_type=="TCP":
  260 + cmd = cmd + self._end_of_command_to_send
  261 + fid.sendall(cmd.encode())
227 262 return (err, cmd)
228 263  
229 264 def _read_chan_old(self):
... ... @@ -248,29 +283,39 @@ class Mountchannel():
248 283 err, fid = self.check_chan()
249 284 if err != self.NO_ERROR:
250 285 raise Exception("Error {}".format(err))
251   - # --- serial port is ready to put message
252 286 lignes = "" ; # fid.readlines()
253 287 t0 = time.time()
254 288 dt = 0
255 289 nn = 0
256   - while True:
257   - n = 0
258   - for car in fid.read():
259   - n += 1
260   - car = chr(car)
261   - lignes += car
262   - nn = 0
263   - t0 = time.time()
264   - #print("car={} n={} nn={}".format(car,n,nn))
265   - if n==0:
  290 + if self._channel_type=="SERIAL":
  291 + while True:
  292 + n = 0
  293 + for car in fid.read():
  294 + n += 1
  295 + car = chr(car)
  296 + lignes += car
  297 + nn = 0
  298 + t0 = time.time()
  299 + #print("car={} n={} nn={}".format(car,n,nn))
  300 + if n==0:
  301 + nn += 1
  302 + if nn==2:
  303 + #print("n={} nn={} Time out".format(n,nn))
  304 + break
  305 + time.sleep(0.01)
  306 + dt = time.time()-t0
  307 + if dt>10.0:
  308 + break
  309 + elif self._channel_type=="TCP":
  310 + while True:
  311 + lignes = fid.recv()
266 312 nn += 1
267 313 if nn==2:
268   - #print("n={} nn={} Time out".format(n,nn))
269 314 break
270 315 time.sleep(0.01)
271   - dt = time.time()-t0
272   - if dt>10.0:
273   - break
  316 + dt = time.time()-t0
  317 + if dt>10.0:
  318 + break
274 319 #print("dt={} lignes={}".format(dt,lignes))
275 320 # --- format la sortie
276 321 if self._end_of_command_to_receive!="":
... ... @@ -348,30 +393,34 @@ class Mountchannel():
348 393 self.set_channel_params(*args, **kwargs)
349 394 self.verbose_chan = False
350 395  
  396 +# #####################################################################
  397 +# #####################################################################
  398 +# #####################################################################
  399 +# Main
  400 +# #####################################################################
  401 +# #####################################################################
  402 +# #####################################################################
  403 +
351 404 if __name__ == "__main__":
352   - """
353   - try:
354   - chan.close_chan()
355   - except:
356   - pass
357   - """
358   - port_serial_ascom='//./com10'
  405 +
  406 + example = 2
  407 + print("Example = {}".format(example))
359 408  
360   - if False:
361   - chan = Mountchannel("SERIAL", port=port_serial_ascom, baud_rate=9600, delay_init_chan=0.1, end_of_command_to_send="".encode('utf8'), end_of_command_to_receive="#".encode('utf8'), delay_put_read=0.06)
362   - s = chan.putread_chan("READ_RA")
363   - print(s)
  409 + if example == 1:
  410 + port_serial_ascom='//./com8'
  411 +
  412 + chan = Mountchannel("SERIAL", port=port_serial_ascom, baud_rate=115200, delay_init_chan=0.1, end_of_command_to_send="#".encode('utf8'), end_of_command_to_receive="#".encode('utf8'), delay_put_read=0.06)
  413 + err, res = chan.putread_chan("READ_RA",0)
  414 + if err == chan.NO_ERROR:
  415 + print(res)
  416 + else:
  417 + print("Error code={}. {}".format(err,res))
  418 + chan.close_chan()
  419 +
  420 + if example == 2:
  421 + port = 80
  422 + hostname = "http://www.google.com"
  423 +
  424 + chan = Mountchannel("TCP", port=port, hostname=hostname, delay_init_chan=0.1, end_of_command_to_send="".encode('utf8'), end_of_command_to_receive="".encode('utf8'), delay_put_read=0.2)
  425 + err, fid = chan.open_chan()
364 426 chan.close_chan()
365   - else:
366   - fid = serial.Serial(
367   - port=port_serial_ascom,
368   - baudrate = 9600,
369   - parity = serial.PARITY_NONE,
370   - stopbits = serial.STOPBITS_ONE,
371   - bytesize = serial.EIGHTBITS,
372   - timeout = 0
373   - )
374   - cmd = "READ_RA"
375   - fid.write(cmd.encode())
376   - fid.flush()
377   - fid.close()
... ...