forked from TZlindra/ELEC291Project1Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultimeter_Temp.pyw
142 lines (129 loc) · 4.86 KB
/
Multimeter_Temp.pyw
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python
from tkinter import *
import time
import serial
import serial.tools.list_ports
import sys
import kconvert
top = Tk()
top.resizable(0,0)
top.title("Fluke_45/Tek_DMM4020 K-type Thermocouple")
#ATTENTION: Make sure the multimeter is configured at 9600 baud, 8-bits, parity none, 1 stop bit, echo Off
CJTemp = StringVar()
Temp = StringVar()
DMMout = StringVar()
portstatus = StringVar()
DMM_Name = StringVar()
connected=0
global ser
def Just_Exit():
top.destroy()
try:
ser.close()
except:
dummy=0
def update_temp():
global ser, connected
If connected==0:
top.after(5000, FindPort) # Not connected, try to reconnect again in 5 seconds
return
try:
strin = ser.readline() # Read the requested value, for example "+0.234E-3 VDC"
strin = strin.rstrip()
strin = strin.decode()
print(strin)
ser.readline() # Read and discard the prompt "=>"
If len(strin)>1:
If strin[1]=='>': # Out of sync?
strin = ser.readline() # Read the value again
ser.write(b"MEAS1?\r\n") # Request next value from multimeter
except:
connected=0
DMMout.set("----")
Temp.set("----");
portstatus.set("Communication Lost")
DMM_Name.set ("--------")
top.after(5000, FindPort) # Try to reconnect again in 5 seconds
return
strin_clean = strin.replace("VDC","") # get rid of the units as the 'float()' function doesn't like it
If len(strin_clean) > 0:
DMMout.set(strin.replace("\r", "").replace("\n", "")) # display the information received from the multimeter
try:
val=float(strin_clean)*1000.0 # Convert from volts to millivolts
valid_val=1;
except:
valid_val=0
try:
cj=float(CJTemp.get()) # Read the cold junction temperature in degrees centigrade
except:
cj=0.0 # If the input is blank, assume cold junction temperature is zero degrees centigrade
If valid_val == 1 :
ktemp=round(kconvert.mV_to_C(val, cj),1)
If ktemp < -200:
Temp.set("UNDER")
elIf ktemp > 1372:
Temp.set("OVER")
else:
Temp.set(ktemp)
else:
Temp.set("----");
else:
Temp.set("----");
connected=0;
top.after(500, update_temp) # The multimeter is slow and the baud rate is slow: two measurement per second tops!
def FindPort():
global ser, connected
try:
ser.close()
except:
dummy=0
connected=0
DMM_Name.set ("--------")
portlist=list(serial.tools.list_ports.comports())
for item in reversed(portlist):
portstatus.set("Trying port " + item[0])
top.update()
try:
ser = serial.Serial(item[0], 9600, timeout=0.5)
ser.write(b"\x03") # Request prompt from possible multimeter
pstring = ser.readline() # Read the prompt "=>"
pstring=pstring.rstrip()
pstring=pstring.decode()
# print(pstring)
If len(pstring) > 1:
If pstring[1]=='>':
ser.timeout=3 # Three seconds timeout to receive data should be enough
portstatus.set("Connected to " + item[0])
ser.write(b"VDC; RATE S; *IDN?\r\n") # Measure DC voltage, set scan rate to 'Slow' for max resolution, get multimeter ID
devicename=ser.readline()
devicename=devicename.rstrip()
devicename=devicename.decode()
DMM_Name.set(devicename.replace("\r", "").replace("\n", ""))
ser.readline() # Read and discard the prompt "=>"
ser.write(b"MEAS1?\r\n") # Request first value from multimeter
connected=1
top.after(1000, update_temp)
break
else:
ser.close()
else:
ser.close()
except:
connected=0
If connected==0:
portstatus.set("Multimeter not found")
top.after(5000, FindPort) # Try again in 5 seconds
Label(top, text="Cold Junction Temperature:").grid(row=1, column=0)
Entry(top, bd =1, width=7, textvariable=CJTemp).grid(row=2, column=0)
Label(top, text="Multimeter reading:").grid(row=3, column=0)
Label(top, text="xxxx", textvariable=DMMout, width=20, font=("Helvetica", 20), fg="red").grid(row=4, column=0)
Label(top, text="Thermocouple Temperature (C)").grid(row=5, column=0)
Label(top, textvariable=Temp, width=5, font=("Helvetica", 100), fg="blue").grid(row=6, column=0)
Label(top, text="xxxx", textvariable=portstatus, width=40, font=("Helvetica", 12)).grid(row=7, column=0)
Label(top, text="xxxx", textvariable=DMM_Name, width=40, font=("Helvetica", 12)).grid(row=8, column=0)
Button(top, width=11, text = "Exit", command = Just_Exit).grid(row=9, column=0)
CJTemp.set ("22")
DMMout.set ("NO DATA")
DMM_Name.set ("--------")
top.after(500, FindPort)
top.mainloop()