Skip to content

Instantly share code, notes, and snippets.

@albedozero
Last active May 16, 2024 18:24
Show Gist options
  • Save albedozero/2e0d3cd1a135f6a04efc6878beda9ba5 to your computer and use it in GitHub Desktop.
Save albedozero/2e0d3cd1a135f6a04efc6878beda9ba5 to your computer and use it in GitHub Desktop.
MightyOhm geiger counter logging script
import time
import tkinter as tk
from tkinter import ttk
# connection settings
MIN_PORT = 1
MAX_PORT = 20
BAUDRATE = 9600
# initial number of samples to combine
num_samples = 5
def copy_text():
root.clipboard_clear()
root.clipboard_append(text.get(1.0, "end"))
status_bar.configure(text="Copied")
def restart_counter():
text.delete(1.0, "end")
text.insert(1.0, "Time (sec), Counts, Total Counts\n")
counter.total = 0
counter.samples = 0
counter.counts = 0
counter.t0 = time.time()
root = tk.Tk()
root.title("Geiger Logger")
frame = ttk.Frame(root)
frame.pack(fill="both", expand=True)
menubar = ttk.Frame(frame)
menubar.pack(side="top", fill="x")
copy_button = ttk.Button(menubar, text="Copy", command=copy_text)
copy_button.pack(side="left")
clear_button = ttk.Button(menubar, text="Clear", command=restart_counter)
clear_button.pack(side="left")
samptime_label = ttk.Label(menubar, text="Samples to Combine: ", anchor="w")
samptime_label.pack(side="left")
sample_entry = tk.StringVar()
samptime_entry = tk.Entry(menubar, width=10, textvariable=sample_entry)
samptime_entry.pack(side="left")
sample_entry.set(num_samples)
text = tk.Text(frame, wrap="word", height=10)
text.pack(side="left", fill="y", expand=True)
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=text.yview)
scrollbar.pack(side="right", fill="y")
text.configure(yscrollcommand=scrollbar.set)
status_bar = ttk.Label(root, anchor="w")
status_bar.pack(side="bottom", fill="x")
try:
import serial
except:
status_bar.configure(text="Downloading serial port module")
root.update()
import sys, subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pyserial'])
import serial
status_bar.configure(text="Waiting to connect")
isConnected = False
while not isConnected:
for port in ['COM' + str(p) for p in range(MIN_PORT, MAX_PORT + 1)]:
try:
geiger = serial.Serial(port, BAUDRATE, timeout=5)
if not geiger.readline().decode().startswith('CPS,'):
geiger.close()
continue
except serial.SerialException:
root.update()
continue
else:
isConnected = True
break
status_bar.configure(text=f"Connected to {port}")
counter = type('Counter', (), dict(total=0, samples=0, counts=0, t0=0))
restart_counter()
while True:
if geiger.in_waiting > 0:
a = geiger.readline().decode().split(',')
counter.counts += int(a[1])
counter.samples += 1
status_bar.configure(text=f"Received {counter.samples} samples from {port}")
try:
num_samples = int(sample_entry.get())
except:
pass
if counter.samples >= num_samples:
counter.total += counter.counts
text.insert("end", f"{time.time() - counter.t0:10.2f}, {counter.counts:6}, {counter.total:12}\n")
text.see("end")
counter.counts = 0
counter.samples = 0
root.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment