Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import *
- import struct
- import threading
- HOST = ''
- PORT = 12345
- s = socket(AF_INET, SOCK_DGRAM)
- s.settimeout(2)
- s.bind((HOST, PORT))
- buf_t = []
- buf_v = []
- lock = threading.Lock()
- flag_thread_stop = False
- def func_thread():
- while (flag_thread_stop == False):
- try:
- bv, addr = s.recvfrom(16*100)
- size = len(bv)
- lock.acquire()
- for i in range(size/16):
- vv = struct.unpack('dd', bv[i*16:(i+1)*16])
- buf_t.append(vv[0])
- buf_v.append(vv[1])
- lock.release()
- except timeout:
- pass
- print("loop")
- time.sleep(0.01)
- thread_udp = threading.Thread(target=func_thread)
- thread_udp.start()
- t = np.zeros(100)
- y = np.zeros(100)
- plt.ion()
- fig = plt.figure()
- ax = fig.add_subplot(111)
- li, = plt.plot(t, y)
- plt.ylim(0, 1)
- plt.show(block=False)
- fig.canvas.draw()
- background = fig.canvas.copy_from_bbox(ax.bbox)
- t0 = time.time()
- while True:
- try:
- time.sleep(0.01)
- flag_update = False
- lock.acquire()
- num = len(buf_t)
- for i in range(num):
- t = np.append(t, buf_t[i]-t0)
- t = np.delete(t, 0)
- y = np.append(y, buf_v[i])
- y = np.delete(y, 0)
- flag_update = True
- buf_t = []
- buf_v = []
- lock.release()
- if(flag_update == False):
- continue
- li.set_xdata(t)
- li.set_ydata(y)
- plt.xlim(min(t), max(t))
- plt.draw()
- fig.canvas.restore_region(background)
- ax.draw_artist(li)
- fig.canvas.blit(ax.bbox)
- except KeyboardInterrupt:
- print("interrupt")
- flag_thread_stop = True
- print("join")
- thread_udp.join()
- print("join end")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement