-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_MCA.py
118 lines (93 loc) · 3.93 KB
/
read_MCA.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Read data coming from the REM 500 Neutron Survey Meter through a RS-232 serial cable
# Authors: Leo Borrel, Sophie Middleton
# Date: 2022-10-31
import serial
import binascii
import math
from time import sleep
# Variables that can be changed in the script
# Enable test mode to turn on the check source at the beginning of the run and turn it off at the end
test_mode = False
# total runtime (in seconds)
runtime = 600 #seconds
# split time: how often the data are saved in separate files (in seconds)
split_time = 60 #seconds
# Set up port
# ser = serial.Serial('/dev/tty.usbserial-AB0K01H7',9600,timeout=100) # Sophie's Macbook
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=100) # Leo's laptop (centOS)
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
# Create the main output text files
outputfile = open("data/count_data.txt", "w")
channelfile = open("data/channel_data.txt", "w")
# Create the multiple output files for the split time
n_split = math.ceil(runtime / split_time)
# Check if port is open
try:
ser.isOpen()
print("is open")
except:
print("error: port is close")
exit()
# Available commands to send
go = 'G'.encode('utf-8') # start the run
stop = 'S'.encode('utf-8') # stop the run
check = 'C'.encode('utf-8') # turn on and off the source in the detector
reset = 'R'.encode('utf-8') # reset data and timer
dump = 'DA'.encode('utf-8') # Dump the data for each channel
dump_read = 'D\r\n'.encode('utf-8') # printed output that separates the count readout from the data dump
# Read/Write
if(ser.isOpen()):
serial_string = ""
try:
# Reset data and start the run
ser.write(reset)
ser.write(go)
if test_mode == True:
ser.write(check)
# read data for the set runtime
t = 0
split = 1
while (t <= runtime):
split_filename = "data/count_data_split" + str(split) + ".txt"
split_file = open(split_filename, "w")
while(t <= split * split_time):
# read an entire line of data containing the timestamp and the number of counts in hex format
serial_string = ser.readline()
print(serial_string)
# save the data to the main output file and the split one
outputfile.write(serial_string.decode('utf-8'))
split_file.write(serial_string.decode('utf-8'))
# the REM 500 sends data every second so wait for the new line of data to come
sleep(1)
t = t + 1
split_file.close()
split = split + 1
# stop the run and turn off the source (if it has been turned on)
ser.write(stop)
if test_mode == True:
ser.write(check)
# dump the channel data
ser.write(dump)
# Because the commands sent to the REM 500 are part of the output, usually there is a couple of data lines missing that we need to read now
# read all the lines until the command to send the data dump is read
while (serial_string != dump_read):
serial_string = ser.readline()
print(serial_string)
outputfile.write(serial_string.decode('utf-8'))
# read the data for each one of the 256 channel and save them in the other output file
for i in range(256):
serial_string = ser.readline()
print(serial_string)
channelfile.write(serial_string.decode('utf-8'))
# The line right after the channel dump is supposed to be the number of counts with the timestamp
serial_string = ser.readline()
print('remaining: ', serial_string)
except Exception as err:
print("Error: cannot read/write:", err)
else:
print("Error: cannont open port")
# close text file
outputfile.close()
channelfile.close()