-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpressure_gauge.py
More file actions
executable file
·97 lines (77 loc) · 2.86 KB
/
pressure_gauge.py
File metadata and controls
executable file
·97 lines (77 loc) · 2.86 KB
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
#!/usr/bin/env python
import time, serial
"""
This is the Pressure object which reads pressure
measurements from the 358 Micro-Ion Controller.
"""
class Pressure(object):
timeout = 5 # At most 5 second read wait necessary.
def __init__(self, port='/dev/tty.USA19H1463P1.1'):
self.port = port
self.start_serial(self.port)
#Open a port for serial communication.
def start_serial(self, port):
# Configure the serial connections.
# These are specific to the 358 Micro-Ion Controller
# This device is configurable, but these are default (except for BAUD).
# Currently set to send out a message every 5 seconds, non interactively.
# Could set to "interactive" mode with a switch inside, but no need.
try:
self.ser = serial.Serial(
port=port,
baudrate=9600,
parity=serial.PARITY_NONE,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE,
timeout=5
)
# Force these to true:
self.ser.setDsrDtr(True)
self.ser.setRtsCts(True)
except:
print("Something went wrong...")
# Unused
def write(self, command):
self.ser.write(command + '\r\n')
def close_serial(self):
self.ser.close()
def read(self):
out = []
terminated = False
# Flush input (constantly sending messages)
while self.ser.inWaiting() > 0: str(self.ser.read(1))
# Read newest message when buffer clear (5 sec delay)
start = time.time()
while (self.ser.inWaiting() > 0 or not terminated) and time.time() - start < self.timeout:
data = str(self.ser.read(1))
if data == '\r':
terminated = True
else:
out.append(data)
if out != '':
return ' '.join(out)[:-1] # Drop the \n
else:
return ''
# Unused
def write_and_read(self, command):
self.write(command)
return self.read()
# Checks if pressure unit sends anything. Try twice, just in case.
def check_ready(self):
test = self.read()
if len(test) != 0:
return True
else:
test = self.read()
if len(test) != 0:
return True
else:
return False
def getReadings(self):
readings = self.read().replace(' ', '').split(',')
return [float(r) for r in readings]
if __name__ == '__main__':
pressure = Pressure()
measurements = pressure.read().replace(' ', '').split(',')
for i in measurements:
print(i)