Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. #!/bin/python
  2.  
  3. import time
  4. import datetime
  5. import serial
  6. from decimal import *
  7. import thread
  8. import os, sys
  9. import struct
  10.  
  11. TTY_IR = '/dev/ttyUSB0'
  12.  
  13. # Voltcraft IR thermometer serial communication initialization
  14. ser = serial.Serial()
  15. ser.baudrate = 9600
  16. ser.port = TTY_IR
  17.  
  18. try:
  19.     ser.open()
  20. except:
  21.     print "Thermometer is not connected, can't open /dev/ttyUSB0"
  22.     sys.exit()
  23.  
  24. # ### INITIALIZATION  ###
  25.  
  26. # read line to overcome possible corrupted data
  27. def detect_frame():
  28.     print "> Detecting frame"
  29.     while True:
  30.         data = ser.read(1)
  31.         if data[0].encode('hex') == "aa":
  32.             print "> Frame Detected"
  33.             break
  34.  
  35. detect_frame()
  36.  
  37. # Voltcraft IR 2200-50D USB data stream decoding (should be compatible with other Voltcraft IR USB thermometers)
  38. def temp_get_ir():
  39.     data = ""
  40.    
  41.     # read data from the serial port - 17 bytes
  42.     try:
  43.         data = ser.read(17)
  44.      
  45.         #for character in data:
  46.         #    print character.encode('hex'),
  47.    
  48.         if data[16].encode('hex') != "aa":
  49.             print "> Frame Lost"
  50.             detect_frame()
  51.             return temp
  52.     except:
  53.         print "> IR Data stream failed?"
  54.    
  55.     return (float (struct.unpack("<L", data[4]+""+data[3]+"\x00\x00")[0]) / 10, float (struct.unpack("<L", data[6]+""+data[5]+"\x00\x00")[0]) / 10)
  56.  
  57.  
  58. # get data from IR and TC
  59. data = temp_get_ir()
  60. temp_ir = data[0]
  61. temp_tc = data[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement