Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import serial
  2. import time
  3.  
  4. from opentrons.drivers import connection, get_serial_ports_list
  5.  
  6.  
  7. port = get_serial_ports_list()[0]
  8. conn = connection.Connection(
  9. serial.Serial(), port=port, baudrate=115200)
  10. conn.open()
  11. conn.flush_input()
  12. print('Connected to', conn.name())
  13.  
  14.  
  15. def player_progress(c, timeout=5):
  16. c.flush_input()
  17. c.write_string('progress\r\n')
  18. c.wait_for_data(timeout=timeout)
  19. progress_data = c.readline_string()
  20. while 'play' not in progress_data.lower() and 'file' not in progress_data.lower():
  21. c.wait_for_data(timeout=timeout)
  22. progress_data = c.readline_string()
  23. c.wait_for_data(timeout=timeout)
  24. c.readline_string()
  25. c.flush_input()
  26.  
  27. c.write_string('M27\r\n')
  28. c.wait_for_data(timeout=timeout)
  29. progress_bytes = c.readline_string()
  30. c.wait_for_data(timeout=timeout)
  31. c.readline_string()
  32. c.wait_for_data(timeout=timeout)
  33. c.readline_string()
  34. c.flush_input()
  35. return _parse_progress_data(progress_data, progress_bytes)
  36.  
  37.  
  38. def _parse_progress_data(progress_a, progress_b):
  39. progress_info = {
  40. 'file': None,
  41. 'percentage': None,
  42. 'elapsed_time': None,
  43. 'estimated_time': None,
  44. 'current_byte': None,
  45. 'total_bytes': None
  46. }
  47. e = 'Not currently playing'
  48. if progress_a != e and progress_b != e:
  49. try:
  50. # file: /sd/protocol.gcode, 7 % complete, elapsed time: 00:00:08, est time: 00:02:06 # noqa
  51. split_data = progress_a.split(',')
  52.  
  53. file = split_data[0].strip().split(' ')[-1]
  54. progress_info['file'] = file.split('/')[-1]
  55. perc = split_data[1].split('%')[0].strip()
  56. progress_info['percentage'] = float(perc) / 100.0
  57.  
  58. elapsed_time = split_data[2].split(':')
  59. t = int(elapsed_time[-1].strip())
  60. t += (int(elapsed_time[-2].strip()) * 60)
  61. t += (int(elapsed_time[-3].strip()) * 60 * 60)
  62. progress_info['elapsed_time'] = t
  63.  
  64. # estimated time is not there in the beginning of a job
  65. estimated_time = None
  66. if len(split_data) > 3:
  67. estimated_time = split_data[3].split(':')
  68. est = int(estimated_time[-1].strip())
  69. est = int(estimated_time[-1].strip())
  70. est += (int(estimated_time[-2].strip()) * 60)
  71. est += (int(estimated_time[-3].strip()) * 60 * 60)
  72. progress_info['estimated_time'] = est
  73. except Exception:
  74. raise RuntimeError(
  75. 'Error parsing progress: {}'.format(progress_a))
  76.  
  77. try:
  78. # SD printing byte 3980/53182
  79. byte_data = progress_b.strip().split(' ')[-1].split('/')
  80. progress_info['current_byte'] = int(byte_data[0])
  81. progress_info['total_bytes'] = int(byte_data[1])
  82. except Exception:
  83. raise RuntimeError(
  84. 'Error parsing progress: {}'.format(progress_b))
  85.  
  86. return progress_info
  87.  
  88. if not player_progress(conn)['file']:
  89. conn.write_string('play /sd/test.gcode\r\n')
  90. conn.wait_for_data(timeout=3)
  91. conn.readline_string()
  92. conn.wait_for_data(timeout=3)
  93. conn.readline_string()
  94. conn.wait_for_data(timeout=3)
  95. conn.readline_string()
  96.  
  97. while True:
  98. progress = player_progress(conn)
  99. if not progress['file']:
  100. break
  101. print(progress['percentage'], progress['estimated_time'])
  102.  
  103. conn.flush_input()
  104. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement