Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. import time
  2. import daqflex
  3. import sys
  4. import csv
  5. import os.path
  6.  
  7. d = daqflex.USB_2001_TC()
  8.  
  9.  
  10. def get_temperature(): # from thermocouple reader USB-2001-TC
  11. return float(d.send_message("?AI{0}:CJC/DEGC").encode("utf-8").split("=")[1])
  12.  
  13.  
  14. def create_file_name(param):
  15. number = 0
  16. file_name = "%s-%s-%s-%s-%d.csv" % (param, time.strftime("%m"), time.strftime("%d"), time.strftime("%y"), number)
  17. while True:
  18. if os.path.isfile(file_name):
  19. number += 1
  20. file_name = "%s-%s-%s-%s-%d.csv" % (param, time.strftime("%m"), time.strftime("%d"), time.strftime("%y"),
  21. number)
  22. else:
  23. break
  24. with open(file_name, 'w') as f:
  25. writer = csv.writer(f)
  26. writer.writerow(('Time', 'Temperature (Celsius)', 'Temperature (Kelvin)'))
  27. return file_name
  28.  
  29.  
  30. def temperature_kelvin():
  31. kelvin = get_temperature() + 273.15
  32. return kelvin
  33.  
  34.  
  35. def log_temperature(start, log_name):
  36. with open(log_name, 'w+') as f:
  37. writer = csv.writer(f)
  38. writer.writerow((time.time() - start, get_temperature(), temperature_kelvin()))
  39.  
  40.  
  41. def cleanup(state, start):
  42. if state:
  43. print "Time elapsed: %d" % (time.time() - start)
  44. print "Shutdown successful"
  45.  
  46.  
  47. def main():
  48. start_time = time.time()
  49. log_name = create_file_name("log")
  50. try:
  51. while True:
  52. log_temperature(start_time, log_name)
  53. time.sleep(.25)
  54. except KeyboardInterrupt:
  55. cleanup(True, start_time)
  56.  
  57.  
  58. def begin():
  59. response = ""
  60. while True:
  61. print "Begin?"
  62. response = raw_input("(y/n): ")
  63. if response.lower() in ("y", "n"):
  64. break
  65. else:
  66. print "Invalid response."
  67. if response == "y":
  68. print "Standby, process will begin in 5 seconds. Type Control+C at any time to end."
  69. time.sleep(5.0)
  70. main()
  71. else:
  72. sys.exit()
  73.  
  74.  
  75. if __name__ == "__main__":
  76. begin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement