forked from memes/geiger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
90 lines (79 loc) · 3.23 KB
/
plot.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! python
#
# Simple script to read data from my geiger counter port, transform the chars
# (remove LCD control characters), and write to stdout.
#
# Designed to be run with feedgnuplot for live data streaming
#
import sys, serial, optparse
def main():
parser = optparse.OptionParser(
usage = "%prog [options] [port]",
description = "Simple script to read data from geiger counter and write to stdout"
)
parser.add_option("-p", "--port",
dest = "port",
help = "serial device to use, default %default%",
default = "/dev/ttyUSB0"
)
parser.add_option("-b", "--baud",
dest = "baudrate",
help = "baud rate for port, default %default",
default = 9600
)
parser.add_option("--parity",
dest = "parity",
help = "port parity, default %default",
default = serial.PARITY_NONE
)
parser.add_option("-s", "--stop",
dest = "stopbits",
help = "stop bits for port, default %default",
default = serial.STOPBITS_ONE
)
parser.add_option("--bits",
dest = "bytesize",
help = "bits per character for port, default %default",
default = serial.EIGHTBITS
)
parser.add_option("-c", "--chars",
dest = "chars",
help = "number of characters to read, default %default",
# Geiger LCD is 2 x (16 chars + 2 control chars)
default = 36
)
parser.add_option("-t", "--timeout",
dest = "timeout",
help = "timeout for port, default %default",
default = None
)
parser.add_option("-z", "--reset",
dest = "reset",
action = "store_true",
help = "reset geiger counter before starting, default %default",
default = False
)
(options, args) = parser.parse_args();
port = None
try:
port = serial.Serial(port = options.port,
baudrate = options.baudrate,
parity = options.parity,
stopbits = options.stopbits,
bytesize = options.bytesize,
timeout = options.timeout)
if options.reset:
port.write('z')
while True:
buf = port.read(options.chars).decode('ascii', 'ignore')
counts = [int(s) for s in buf.split() if s.isdigit()]
if counts:
sys.stdout.write("%d\n" % counts[0])
sys.stdout.flush()
except serial.SerialException as se:
sys.stderr.write("problem reading from port %s: %s\n" % (options.port, se))
finally:
if port:
port.close()
if __name__ == '__main__':
main()