# -*- coding: utf-8 -*- """ Created on Sun Dec 12 18:41:34 2021 @author: toman """ import socket import sys import matplotlib.pyplot as plt import numpy as np #------- functions ----------- def reusePort(s): # https://docs.python.org/es/3/library/socket.html#functions info de setsocketopt if(s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) == -1): print("error in setsockopt,SO_REUSEPORT") sys.exit(-1) return #--------------------------- #----- parseado de argumentos ------- print ( len(sys.argv) ) if(len(sys.argv) != 4): #sys.argv deberia ser [nombre, host, port, delta] print("Debe ejecutarse como: python lector.py ") print("Un ejemplo es: python lector.py localhost 47205 1") sys.exit(-1) arg_host = sys.argv[1] arg_port = int(sys.argv[2]) delta = int(sys.argv[3])*1000 #en ms asumiendo argv en segundos #--------------------------------------- #-------------- config matplotlib ------------- # plt.style.use('_mpl-gallery') fig = plt.figure() #---------------------------------------------- #conexion del socket como cliente TCP sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a TCP/IP socket server_address = (arg_host, arg_port) reusePort(sock) print('connecting to {} port {}'.format(*server_address)) isFinished = False # xValues = np.zeros(20) # yValues = np.zeros(20) xval = 0 #current xval xValues = [0 for i in range(0,20)] #full of zeroes yValues = [0 for i in range(0,20)] #full of zeroes while isFinished == False: try: #envio el N sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a TCP/IP socket sock.connect(server_address) try: sock.send(str.encode("n\n")) except socket.error: print("sock.send err") #recibimos el numero try: rcv_msg = float(str(sock.recv(100), 'utf-8').strip()[:-2]) #decodes,removes junk, takes out '\n' from the end. yValues.pop(0) #put in the yValues considering a circular array yValues.append(rcv_msg) xValues.pop(0) xval = xval + delta/1000 xValues.append(xval) except socket.error: print("sock.recv err") fig.clear() ax = plt.subplot() ax.plot(np.asarray(xValues), np.asarray(yValues), linewidth=2.0) plt.pause(0.02) plt.show() print(rcv_msg) sock.close() except KeyboardInterrupt: isFinished = True print("Closing properly...") sock.close() print("Closed succesfully!...")