Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. from socket import *
  2. import struct
  3. import threading
  4.  
  5. HOST = ''
  6. PORT = 12345
  7.  
  8. s = socket(AF_INET, SOCK_DGRAM)
  9. s.settimeout(2)
  10. s.bind((HOST, PORT))
  11.  
  12. buf_t = []
  13. buf_v = []
  14. lock = threading.Lock()
  15. flag_thread_stop = False
  16.  
  17. def func_thread():
  18. while (flag_thread_stop == False):
  19. try:
  20. bv, addr = s.recvfrom(16*100)
  21. size = len(bv)
  22. lock.acquire()
  23. for i in range(size/16):
  24. vv = struct.unpack('dd', bv[i*16:(i+1)*16])
  25. buf_t.append(vv[0])
  26. buf_v.append(vv[1])
  27. lock.release()
  28. except timeout:
  29. pass
  30. print("loop")
  31. time.sleep(0.01)
  32.  
  33. thread_udp = threading.Thread(target=func_thread)
  34. thread_udp.start()
  35.  
  36.  
  37. t = np.zeros(100)
  38. y = np.zeros(100)
  39.  
  40. plt.ion()
  41. fig = plt.figure()
  42. ax = fig.add_subplot(111)
  43. li, = plt.plot(t, y)
  44. plt.ylim(0, 1)
  45. plt.show(block=False)
  46. fig.canvas.draw()
  47.  
  48. background = fig.canvas.copy_from_bbox(ax.bbox)
  49.  
  50. t0 = time.time()
  51.  
  52. while True:
  53. try:
  54. time.sleep(0.01)
  55. flag_update = False
  56. lock.acquire()
  57. num = len(buf_t)
  58. for i in range(num):
  59. t = np.append(t, buf_t[i]-t0)
  60. t = np.delete(t, 0)
  61. y = np.append(y, buf_v[i])
  62. y = np.delete(y, 0)
  63. flag_update = True
  64. buf_t = []
  65. buf_v = []
  66.  
  67. lock.release()
  68.  
  69. if(flag_update == False):
  70. continue
  71. li.set_xdata(t)
  72. li.set_ydata(y)
  73. plt.xlim(min(t), max(t))
  74. plt.draw()
  75. fig.canvas.restore_region(background)
  76. ax.draw_artist(li)
  77. fig.canvas.blit(ax.bbox)
  78. except KeyboardInterrupt:
  79. print("interrupt")
  80. flag_thread_stop = True
  81. print("join")
  82. thread_udp.join()
  83. print("join end")
  84. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement