-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommHH506RApython3.py
71 lines (58 loc) · 2.12 KB
/
CommHH506RApython3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# This program shows how to read Omega HH506RA thermocouple meter T1 and T2 with Python 3.x
#####################################################################################
# REQUIREMENTS
# python 3.x: http://www.python.org/ftp/python/
# pyserial for python 2.6: http://sourceforge.net/projects/pyserial/files/pyserial/2.6/
# javacomm: http://www.xpl4java.org/xPL4Java/javacomm.html
# Java JDK or JRE: http://java.sun.com/javase/downloads/index.jsp
# 2012-04-06: Updated to run on Python v3.x (ML)
import serial
import time
def main():
print("use CTRL + C to interrupt program\n")
delay = 1 # set interval of seconds between each reading
port = '/dev/tty.usbserial-A2001Epn'
i = HH506RAid(port)
while True:
t1,t2 = HH506RAtemperature(port,i)
print("T1 = " + str(t1) + " " + "T2 = " +str(t2) + "\n")
time.sleep(delay)
def HH506RAid(port):
try:
ser = serial.Serial(port, baudrate=2400, bytesize=7, parity='E', stopbits=1, timeout=1)
sync = None
while sync != b"Err\r\n":
ser.write(b"\r\n") # seems that on Python3/OSX standard LF is added (might by different on Windows!)
sync = ser.read(5)
time.sleep(1)
ser.write(b"%000R")
i = ser.read(5)[0:3]
ser.close()
return i
except serial.SerialException as e:
print(e)
return "000"
def hex2temp(h):
if h == '':
return 0
else:
t = int(h[1:],16)/10.0
if h[0:1] == '-':
t = -t
if t < -50 or t > 400:
return 0
else:
return t
def HH506RAtemperature(port,i):
try:
ser = serial.Serial(port, baudrate=2400, bytesize=7, parity='E', stopbits=1, timeout=1)
ser.write("#".encode('ascii') + i + "N\r".encode('ascii'))
ser.write(b"#" + i + b"N\r")
r = ser.read(14)
ser.close()
return hex2temp(r[0:5]), hex2temp(r[6:11])
except serial.SerialException as e:
print(e)
return 0,0
main()