|
16 | 16 |
|
17 | 17 | max_bins = 8192 |
18 | 18 | logger = logging.getLogger(__name__) |
| 19 | + |
19 | 20 | stopflag = 0 |
20 | 21 | stopflag_lock = threading.Lock() |
| 22 | + |
21 | 23 | spec_stopflag = 0 |
22 | 24 | spec_stopflag_lock = threading.Lock() |
| 25 | + |
23 | 26 | histogram = [0] * max_bins |
24 | 27 | histogram_lock = threading.Lock() |
| 28 | + |
25 | 29 | command = "" |
26 | 30 | command_lock = threading.Lock() |
| 31 | + |
| 32 | +cps = 0 |
| 33 | +cps_lock = threading.Lock() |
| 34 | + |
| 35 | +calibration_updated = 0 |
| 36 | +calibration_lock = threading.Lock() |
| 37 | + |
27 | 38 | pkts03 = 0 |
28 | 39 | pkts04 = 0 |
29 | 40 | total_pkts = 0 |
30 | 41 | dropped = 0 |
31 | 42 | total_time = 0 |
32 | 43 | cpu_load = 0 |
33 | | -cps = 0 |
34 | | -cps_lock = threading.Lock() |
35 | 44 | lost_impulses = 0 |
36 | 45 | last_counts = 0 |
37 | | -data_directory = None |
38 | 46 | cps_list = [] |
39 | 47 | serial_number = "" |
40 | 48 | calibration = [0., 1., 0., 0., 0.] |
41 | | -calibration_updated = 0 |
42 | | -calibration_lock = threading.Lock() |
43 | 49 | inf_str = '' |
| 50 | +data_directory = '' |
| 51 | + |
44 | 52 |
|
45 | | -with global_vars.write_lock: |
46 | | - data_directory = global_vars.data_directory |
47 | 53 |
|
48 | 54 | # This function communicates with the device |
49 | 55 | def start(sn=None): |
| 56 | + |
50 | 57 | READ_BUFFER = 1 |
51 | 58 | pulse_file_opened = 0 |
52 | 59 | shproto.dispatcher.clear() |
@@ -152,35 +159,55 @@ def start(sn=None): |
152 | 159 | ((response.payload[i * 4 + 5]) << 24) |
153 | 160 | shproto.dispatcher.histogram[index] = value & 0x7FFFFFF |
154 | 161 | response.clear() |
| 162 | + |
| 163 | + # ----- cmd pulse mode ------------------------------------- |
| 164 | + |
| 165 | + |
| 166 | + # Track whether the CSV file has been initialized |
| 167 | + pulse_file_initialized = False |
| 168 | + |
155 | 169 | if response.cmd == shproto.MODE_PULSE: |
156 | 170 | pulse_data = [] |
| 171 | + |
| 172 | + # Extract pulse data from the payload (16-bit values) |
157 | 173 | for i in range(0, len(response.payload), 2): |
158 | 174 | if i + 1 < len(response.payload): |
159 | 175 | value = (response.payload[i + 1] << 8) | response.payload[i] |
160 | 176 | pulse_data.append(value) |
161 | 177 |
|
162 | 178 | if pulse_data: |
163 | | - pulse_data = pulse_data[:-1] # Remove the last item if needed |
| 179 | + pulse_data = pulse_data[:-1] # Remove last item if needed |
164 | 180 | logger.debug(f"Processed Pulse Data: {pulse_data}") |
| 181 | + |
165 | 182 | with global_vars.write_lock: |
166 | 183 | global_vars.max_pulse_shape = pulse_data |
167 | | - if pulse_file_opened != 1: |
168 | | - fd_pulses = open(os.path.join(data_directory,"max-pulse-check.csv"), "w+") |
169 | | - pulse_file_opened = 1 |
170 | 184 |
|
171 | | - shproto.dispatcher.pkts01 += 1 |
172 | | - offset = response.payload[0] & 0xFF | ((response.payload[1] & 0xFF) << 8) |
173 | | - count = int((response.len - 2) / 2) |
174 | | - pulse = [] |
175 | | - for i in range(0, count): |
176 | | - index = offset + i |
177 | | - if index < len(shproto.dispatcher.histogram): |
178 | | - value = (response.payload[i * 2 + 2]) | ((response.payload[i * 2 + 3]) << 8) |
179 | | - pulse.append(value & 0x7FFFFFF) |
180 | | - fd_pulses.writelines("{:d} ".format(value & 0x7FFFFFF)) |
181 | | - fd_pulses.writelines("\n") |
182 | | - fd_pulses.flush() |
| 185 | + # Open the CSV file only once per script execution |
| 186 | + if not pulse_file_initialized: |
| 187 | + with global_vars.write_lock: |
| 188 | + data_directory = global_vars.data_directory |
| 189 | + csv_file_path = os.path.join(data_directory, "_max-pulse-shape.csv") |
| 190 | + |
| 191 | + # Check if the file already exists (to avoid overwriting existing data) |
| 192 | + file_exists = os.path.isfile(csv_file_path) |
| 193 | + |
| 194 | + with open(csv_file_path, "a+") as fd_pulses: |
| 195 | + # Write header only if the file is newly created |
| 196 | + if not file_exists: |
| 197 | + header = ",".join(map(str, range(len(pulse_data)))) + "\n" |
| 198 | + fd_pulses.write(header) |
| 199 | + |
| 200 | + pulse_file_initialized = True # Ensure we don't write the header again |
| 201 | + |
| 202 | + # Append the pulse data as a new row |
| 203 | + with open(csv_file_path, "a") as fd_pulses: |
| 204 | + fd_pulses.write(",".join(map(str, pulse_data)) + "\n") |
| 205 | + |
183 | 206 | response.clear() |
| 207 | + |
| 208 | + |
| 209 | + # ------- cmd stat mode -------------------------- |
| 210 | + |
184 | 211 | elif response.cmd == shproto.MODE_STAT: |
185 | 212 | shproto.dispatcher.pkts04 += 1 |
186 | 213 | shproto.dispatcher.total_time = (response.payload[0] & 0xFF) | \ |
@@ -549,9 +576,14 @@ def load_json_data(file_path): |
549 | 576 | def process_03(_command): |
550 | 577 | with shproto.dispatcher.command_lock: |
551 | 578 | shproto.dispatcher.command = _command |
| 579 | + time.sleep(0.3) # Give it a little time to process |
552 | 580 | logger.info(f'Completed process_03({_command})\n') |
553 | 581 | return |
554 | 582 |
|
| 583 | + |
| 584 | + |
| 585 | + |
| 586 | + |
555 | 587 | def stop(): |
556 | 588 | logger.info('Command shproto.stop \n') |
557 | 589 | with shproto.dispatcher.stopflag_lock: |
|
0 commit comments