Skip to content

geiger_dose_measurement

Kamil Brylew edited this page Jun 19, 2026 · 3 revisions

Geiger-Mueller probe — 60-second dose measurement

Connects to a remote Geiger probe over a ser2net TCP bridge, prints a full device status snapshot, then polls the detector for one minute and displays the dose rate (counts per second, uncalibrated) as a matplotlib figure.

Prerequisites: pip install -e ".[dev]" from the repo root.

%config InlineBackend.figure_formats = ["png"]
import logging
import time

import matplotlib.pyplot as plt
import numpy as np

from nlab_modbus.core.enums import DeviceType
from nlab_modbus.maps.geiger_map import GEIGER_REGISTER_MAP
from nlab_modbus.services.manager import DeviceManager

logging.basicConfig(
    level=logging.WARNING,
    format="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
    force=True,
)
logging.getLogger("pymodbus").setLevel(logging.CRITICAL)

Configuration

Edit the variables below to match your setup.

HOST          = "192.168.10.134"   # IP address of the ser2net bridge
PORT          = 5002              # TCP port on the bridge
DEVICE_ID     = 2                 # Modbus device address (1-16)

DURATION_S    = 60                # measurement window in seconds
POLL_INTERVAL = 0.5               # seconds between reads (≥ 0.2 recommended)

Connect

mgr = DeviceManager()
device = mgr.connect_remote(HOST, PORT, DEVICE_ID, DeviceType.GEIGER)

print(f"Connected: {device.connection_info()}  |  type: {device.device_type.name}")
Connected: 192.168.10.134:5002:2  |  type: GEIGER

Device status snapshot

Reads all holding and input registers once and prints them with engineering units.

def _unit(name: str) -> str:
    spec = GEIGER_REGISTER_MAP.get(name)
    return spec.unit if (spec and spec.unit) else ""


def print_status(dev) -> None:
    holding = dev.get_all_holding_registers()
    inputs  = dev.get_all_input_registers()

    col_w = max(len(k) for k in {**holding, **inputs})

    print(f"{'─' * 55}")
    print(f"  {dev.device_type.name} — {dev.connection_info()}")
    print(f"{'─' * 55}")

    print("\n  [Holding registers — read/write]")
    for name, value in holding.items():
        unit = _unit(name)
        print(f"    {name:<{col_w}}  {value:>10.4g}  {unit}")

    print("\n  [Input registers — read-only]")
    for name, value in inputs.items():
        unit = _unit(name)
        print(f"    {name:<{col_w}}  {value:>10.4g}  {unit}")

    print(f"{'─' * 55}\n")


print_status(device)
───────────────────────────────────────────────────────
  GEIGER — 192.168.10.134:5002:2
───────────────────────────────────────────────────────

  [Holding registers — read/write]
    rs485_mb_addr                2  
    rs485_baud                9600  
    pwm_enable                   1  
    pwm_set_voltage            550  V
    dac_vout_mv                200  mV
    dac_vout_mv_2              200  mV
    pwm_freq_khz               100  kHz
    pwm_max_duty                40  %
    pwm_outvolt_comp             4  %
    dac_vout_mv_3              200  mV
    scale_coeff_p1_a2            0  
    scale_exp_p1_a2              0  
    scale_coeff_p1_a1            1  
    scale_exp_p1_a1              0  
    scale_coeff_p1_a0            0  
    scale_exp_p1_a0              0  
    scale_coeff_th               0  
    scale_exp_th                 0  
    scale_coeff_p2_a2            0  
    scale_exp_p2_a2              0  
    scale_coeff_p2_a1            1  
    scale_exp_p2_a1              0  
    scale_coeff_p2_a0            0  
    scale_exp_p2_a0              0  
    pid_p_vout                 202  
    pid_i_vout                 501  
    adc_freq_khz                11  kHz
    adc_filter_coeff            11  
    pass_static              -4345  

  [Input registers — read-only]
    hardware_version           513  
    firmware_version             2  
    pulses               1.979e+04  
    pulses_per_sec              43  cps
    dose_level_msvh             43  mSv/h
    dose_msv             2.089e+04  mSv
    usb_rx_frames_cnt           28  
    usb_rx_crc_err_cnt          17  
    usb_rx_bad_req_cnt           0  
    cpu_temp                    30  *C
    board_temp               27.57  *C
    eeprom_error                 0  
    supply_voltage            4.84  V
    hv_voltage                 550  V
    pulse_integer               12  mV
    gm_supply_fault              0  
    pwm_duty                   9.6  %
    vout_ripple               2868  mV
    pwm_cmpss_action             0  
    pid_saturation               0  
───────────────────────────────────────────────────────

Measurement

Polls pulses_per_sec (counts per second, uncalibrated dose rate) and dose_level_msvh (calibrated dose rate) for DURATION_S seconds at POLL_INTERVAL intervals. Progress is printed inline.

times_s        = []   # elapsed time in seconds
cps_series     = []   # raw counts per second (dose rate a.u.)
dose_rate_msvh = []   # calibrated dose rate in mSv/h (may be 0 if uncalibrated)
hv_series      = []   # HV output voltage for reference

t0        = time.perf_counter()
next_tick = t0 + POLL_INTERVAL

print(f"Measuring for {DURATION_S} s  (Ctrl-C to stop early)\n")
print(f"  {'Time':>6}   {'CPS':>6}   {'Dose rate':>10}   {'HV':>6}")
print(f"  {'[s]':>6}   {'[a.u.]':>6}   {'[mSv/h]':>10}   {'[V]':>6}")
print(f"  {'─'*6}   {'─'*6}   {'─'*10}   {'─'*6}")

try:
    while True:
        elapsed = time.perf_counter() - t0
        if elapsed >= DURATION_S:
            break

        # Single bus transaction: read snapshot registers 3-19 in one FC04 call
        snap = device.read_snapshot()

        cps      = int(snap.get("pulses_per_sec", 0))
        dose_r   = snap.get("dose_level_msvh", 0.0)
        hv       = snap.get("hv_voltage", 0.0)

        times_s.append(elapsed)
        cps_series.append(cps)
        dose_rate_msvh.append(dose_r)
        hv_series.append(hv)

        print(f"  {elapsed:6.1f}   {cps:6d}   {dose_r:10.4f}   {hv:6.1f}", end="\r")

        # Drift-compensating sleep: target absolute ticks, not relative delays
        sleep_dur = next_tick - time.perf_counter()
        if sleep_dur > 0:
            time.sleep(sleep_dur)
        next_tick += POLL_INTERVAL

except KeyboardInterrupt:
    print("\nInterrupted early.")

times_s        = np.array(times_s)
cps_series     = np.array(cps_series)
dose_rate_msvh = np.array(dose_rate_msvh)
hv_series      = np.array(hv_series)

# Cumulative dose: integrate CPS over time (trapezoidal rule)
cum_counts = np.concatenate(([0.0], np.cumsum(
    0.5 * (cps_series[:-1] + cps_series[1:]) * np.diff(times_s)
)))

print(f"\n\nDone.  {len(times_s)} samples over {times_s[-1]:.1f} s")
print(f"  Mean CPS        : {cps_series.mean():.1f}")
print(f"  Max CPS         : {cps_series.max()}")
print(f"  Accumulated dose: {cum_counts[-1]:.0f} counts  (a.u.)")
Measuring for 60 s  (Ctrl-C to stop early)

    Time      CPS    Dose rate       HV
     [s]   [a.u.]      [mSv/h]      [V]
  ──────   ──────   ──────────   ──────
     0.0       33      33.0000    550.0
     0.5       33      33.0000    550.0
     1.0       26      26.0000    550.0
     1.5       26      26.0000    550.0
     2.0       31      31.0000    550.0
     2.5       31      31.0000    550.0
     3.0       46      46.0000    550.0
     3.5       46      46.0000    550.0
     4.0       40      40.0000    550.0
     4.5       40      40.0000    550.0
     5.0       36      36.0000    550.0
     5.5       36      36.0000    550.0
     6.0       39      39.0000    550.0
     6.5       39      39.0000    550.0
     7.0       36      36.0000    550.0
     7.5       36      36.0000    550.0
     8.0       22      22.0000    550.0
     8.5       22      22.0000    550.0
     9.0       25      25.0000    550.0
     9.5       25      25.0000    550.0
    10.0       34      34.0000    550.0
    10.5       34      34.0000    550.0
    11.0       34      34.0000    550.0
    11.5       34      34.0000    550.0
    12.0       32      32.0000    550.0
    12.5       32      32.0000    549.0
    13.0       38      38.0000    550.0
    13.5       38      38.0000    550.0
    14.0       37      37.0000    550.0
    14.5       37      37.0000    550.0
    15.0       33      33.0000    550.0
    15.5       33      33.0000    550.0
    16.0       36      36.0000    549.0
    16.5       36      36.0000    550.0
    17.0       28      28.0000    550.0
    17.5       28      28.0000    550.0
    18.0       38      38.0000    550.0
    18.5       38      38.0000    550.0
    19.0       37      37.0000    550.0
    19.5       37      37.0000    550.0
    20.0       26      26.0000    550.0
    20.5       26      26.0000    550.0
    21.0       25      25.0000    550.0
    21.5       25      25.0000    550.0
    22.0       30      30.0000    550.0
    22.5       30      30.0000    550.0
    23.0       25      25.0000    550.0
    23.5       25      25.0000    549.0
    24.0       31      31.0000    549.0
    24.5       31      31.0000    550.0
    25.0       30      30.0000    550.0
    25.5       30      30.0000    550.0
    26.0       23      23.0000    550.0
    26.5       23      23.0000    550.0
    27.0       26      26.0000    550.0
    27.5       26      26.0000    550.0
    28.0       28      28.0000    550.0
    28.5       28      28.0000    550.0
    29.0       27      27.0000    549.0
    29.5       27      27.0000    550.0
    30.0       25      25.0000    550.0
    30.5       25      25.0000    550.0
    31.0       30      30.0000    550.0
    31.5       30      30.0000    550.0
    32.0       35      35.0000    550.0
    32.5       35      35.0000    550.0
    33.0       35      35.0000    550.0
    33.5       35      35.0000    550.0
    34.0       37      37.0000    550.0
    34.5       37      37.0000    550.0
    35.0       50      50.0000    550.0
    35.5       50      50.0000    550.0
    36.0       36      36.0000    550.0
    36.5       36      36.0000    550.0
    37.0       24      24.0000    549.0
    37.5       24      24.0000    550.0
    38.0       32      32.0000    550.0
    38.5       32      32.0000    550.0
    39.0       27      27.0000    550.0
    39.5       27      27.0000    550.0
    40.0       32      32.0000    550.0
    40.5       32      32.0000    550.0
    41.0       34      34.0000    550.0
    41.5       34      34.0000    549.0
    42.0       26      26.0000    550.0
    42.5       26      26.0000    550.0
    43.0       40      40.0000    550.0
    43.5       40      40.0000    550.0
    44.0       34      34.0000    550.0
    44.5       34      34.0000    550.0
    45.0       34      34.0000    550.0
    45.5       34      34.0000    550.0
    46.0       23      23.0000    550.0
    46.5       23      23.0000    550.0
    47.0       21      21.0000    550.0
    47.5       21      21.0000    550.0
    48.0       37      37.0000    550.0
    48.5       37      37.0000    550.0
    49.0       41      41.0000    550.0
    49.5       41      41.0000    550.0
    50.0       34      34.0000    550.0
    50.5       34      34.0000    550.0
    51.0       34      34.0000    550.0
    51.5       34      34.0000    550.0
    52.0       35      35.0000    550.0
    52.5       35      35.0000    550.0
    53.0       32      32.0000    550.0
    53.5       32      32.0000    550.0
    54.0       28      28.0000    550.0
    54.5       28      28.0000    550.0
    55.0       38      38.0000    550.0
    55.5       38      38.0000    550.0
    56.0       41      41.0000    550.0
    56.5       41      41.0000    550.0
    57.0       28      28.0000    550.0
    57.5       28      28.0000    550.0
    58.0       37      37.0000    550.0
    58.5       37      37.0000    550.0
    59.0       36      36.0000    550.0
    59.5       36      36.0000    550.0

Done.  120 samples over 59.5 s
  Mean CPS        : 32.5
  Max CPS         : 50
  Accumulated dose: 1931 counts  (a.u.)

Plot

fig, axes = plt.subplots(3, 1, figsize=(10, 8), sharex=True)
fig.suptitle(
    f"Geiger-Mueller dose measurement\n"
    f"{device.connection_info()}  |  duration: {times_s[-1]:.1f} s",
    fontsize=12,
)

# --- top panel: dose rate (raw CPS) ---
ax0 = axes[0]
ax0.plot(times_s, cps_series, color="steelblue", linewidth=1.2, label="CPS")
ax0.axhline(cps_series.mean(), color="steelblue", linestyle="--", linewidth=0.8,
            label=f"mean {cps_series.mean():.1f}")
ax0.set_ylabel("Dose rate [counts/s]\n(a.u.)")
ax0.legend(loc="upper right", fontsize=8)
ax0.grid(True, alpha=0.3)

# --- middle panel: cumulative dose ---
ax1 = axes[1]
ax1.plot(times_s, cum_counts, color="darkorange", linewidth=1.5)
ax1.set_ylabel("Accumulated dose\n[counts, a.u.]")
ax1.grid(True, alpha=0.3)

# --- bottom panel: HV output (diagnostic) ---
ax2 = axes[2]
ax2.plot(times_s, hv_series, color="seagreen", linewidth=1.0)
ax2.set_ylabel("HV output [V]")
ax2.set_xlabel("Time [s]")
ax2.grid(True, alpha=0.3)

fig.tight_layout()
plt.show()

Disconnect

mgr.close_all()
print("Connection closed.")
Connection closed.

Clone this wiki locally