Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # python 3
  4.  
  5. from serial import Serial
  6. import time
  7. import argparse
  8. import re
  9. from threading import Thread
  10.  
  11. from timeit import default_timer as timer
  12.  
  13. SAMPLES_TO_AVERAGE = 8
  14. TIME_BETWEEN_COMMANDS = 0.1
  15.  
  16. class Command():
  17. """docstring for Command"""
  18. def __init__(self, cmd, delay, well=99):
  19. self.cmd = cmd
  20. self.delay = delay
  21. self.well = well
  22.  
  23. def has_callback(self):
  24. if b'sample' in self.cmd:
  25. return True
  26. else:
  27. return False
  28.  
  29. def is_bias(self):
  30. if b'well' in self.cmd:
  31. return True
  32. else:
  33. return False
  34.  
  35. def is_well(self):
  36. if b'bias' in self.cmd:
  37. return True
  38. else:
  39. return False
  40.  
  41.  
  42. sampleWellValue = [0] * 8
  43. sampleWellCode = [0] * 8
  44. sampleBiasValue = [0] * 8
  45. sampleBiasCode = [0] * 8
  46.  
  47. setup = [
  48. ]
  49.  
  50. command_set = [
  51. ]
  52.  
  53.  
  54. SAMPLES_TO_AVERAGE = 8
  55. def build_commands():
  56. for i in range(8):
  57. string = bytearray('sample well {0} {1}'.format(i, SAMPLES_TO_AVERAGE), "utf-8")
  58. c = Command(string, TIME_BETWEEN_COMMANDS, i)
  59. command_set.append(c)
  60.  
  61. string = bytearray('sample bias {0} {1}'.format(i, SAMPLES_TO_AVERAGE), "utf-8")
  62. c = Command(string, TIME_BETWEEN_COMMANDS, i)
  63. command_set.append(c)
  64.  
  65.  
  66.  
  67. def runner():
  68. com = Serial()
  69. com.port = 'COM4'
  70. com.baudrate = 115200
  71. com.timeout = 0.1
  72. com.open()
  73.  
  74. build_commands();
  75.  
  76. # setup
  77. for c in setup:
  78. handle_command(com, c)
  79.  
  80. # main commands
  81. for c in command_set:
  82. handle_command(com, c)
  83.  
  84. com.close()
  85.  
  86.  
  87.  
  88. def handle_command(com, command):
  89. com.write(command.cmd)
  90. # print(command.cmd.decode())
  91.  
  92. if command.has_callback():
  93. response = parse_sample_response(com)
  94. parse_response(response)
  95.  
  96.  
  97. time.sleep(command.delay)
  98. # clear out read buffer
  99. com.read()
  100.  
  101.  
  102. def parse_sample_response(command, line):
  103. nameValue = line.split(',')
  104.  
  105. resultValue = 0
  106. resultCode = 0
  107.  
  108. for x in nameValue:
  109. pair = x.split(':')
  110. name = pair[0].strip()
  111. value = pair[1].strip()
  112.  
  113. if 'value' in name:
  114. resultValue = float(value.strip())
  115.  
  116. if 'code' in name:
  117. resultCode = int(value.strip())
  118.  
  119. if command.is_well:
  120. sampleWellCode[command.well] = resultCode
  121. sampleWellValue[command.well] = resultValue
  122.  
  123. if command.is_bias:
  124. sampleWellCode[command.well] = resultCode
  125. sampleWellValue[command.well] = resultValue
  126.  
  127.  
  128.  
  129. def wait_for_callback(com):
  130. while True:
  131. line = readline(com)
  132. if b'response:sample' in line:
  133. return line
  134.  
  135.  
  136. def readline(com):
  137. eol = b'\r\n'
  138. leneol = len(eol)
  139. line = bytearray()
  140. while True:
  141. c = com.read(1)
  142. if c:
  143. line += c
  144. if line[-leneol:] == eol:
  145. break
  146. else:
  147. break
  148. if len(line) > 0:
  149. print(line.decode())
  150. return bytes(line)
  151.  
  152.  
  153. if __name__ == "__main__":
  154. parser = argparse.ArgumentParser(description='loop serial commands')
  155. runner()
  156.  
  157. print('WellValue,' + ','.join(str(x) for x in sampleWellValue))
  158. print('WellCode,' + ','.join(str(x) for x in sampleWellCode))
  159. print('BiasValue,' + ','.join(str(x) for x in sampleBiasValue))
  160. print('BiasCode,' + ','.join(str(x) for x in sampleBiasCode))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement