Interfacing With A Cheap Geiger Counter
2024-11-22 08:0:0 Author: blog.didierstevens.com(查看原文) 阅读量:4 收藏

Interfacing With A Cheap Geiger Counter

I got a cheap Geiger counter from Aliexpress:

This picture was taken on an airplane: you have more radiation (cosmic rays) at high altitude.

I figured out how to interface with this counter in Python to log real time data:

#!/usr/bin/env python

from __future__ import print_function

__description__ = "Program for geiger meter"
__author__ = 'Didier Stevens'
__version__ = '0.0.1'
__date__ = '2024/05/11'

"""

Source code put in the public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk

History:
  2024/05/11: start

Todo:
"""

import optparse
import serial
import time

def FormatTime(epoch=None):
    if epoch == None:
        epoch = time.time()
    return '%04d%02d%02d-%02d%02d%02d' % time.localtime(epoch)[0:6]

def FindCOMPorts():
    ports = []
    for number in range(1, 10):
        try:
            comport = 'COM%d' % number
            with serial.Serial(comport) as oSerial:
                ports.append(comport)
        except serial.serialutil.SerialException as e:
            if 'PermissionError' in e.args[0]:
                ports.append(comport)
    return ports

def LogToCSV(comport):
    ser = serial.Serial(comport, 115200, timeout=0, write_timeout=0)
    ser.write(b'\xAA\x05\x0E\x01\xBE\x55\x00')
    alldata = b''
    fOut = open('geiger.csv', 'a')
    while True:
        data = ser.read(1000)
        if data != b'':
            alldata += data
            lines = alldata.split(b'\xaaU\x0e')
            alldata = lines[-1]
            lines = lines[:-1]
            for line in lines:
                if line != b'':
                    out = FormatTime() + ';' + line.decode('latin')
                    print(out)
                    fOut.write(out + '\n')
            if alldata.endswith(b'U') and not alldata.endswith(b'\xaaU'):
                out = FormatTime() + ';' + alldata.decode('latin')
                print(out)
                fOut.write(out + '\n')
                alldata = b''
            time.sleep(0.40)

def Main():
    oParser = optparse.OptionParser(usage='usage: %prog [options]\n' + __description__ , version='%prog ' + __version__)
    oParser.add_option('-l', '--listports', action='store_true', default=False, help='List ports')
    (options, args) = oParser.parse_args()

    comports = FindCOMPorts()
    if options.listports:
        print('Available ports:')
        for comport in comports:
            print(' %s' % comport)
        return

    if len(args) == 1:
        LogToCSV(args[0])
    elif len(comports) == 1:
        print('Using %s' % comports[0])
        LogToCSV(comports[0])
    else:
        print('Provide the COM port as argument')
        print('Available ports:')
        for comport in comports:
            print(' %s' % comport)

if __name__ == '__main__':
    Main()

No comments yet.


文章来源: https://blog.didierstevens.com/2024/11/22/interfacing-with-a-cheap-geiger-counter/
如有侵权请联系:admin#unsafe.sh