IRNAS / ppk2-api-python

Power Profiling Kit 2 unofficial python api.
http://irnas.eu
GNU General Public License v2.0
145 stars 37 forks source link

Multiprocessing unable to stop measuring at times #13

Closed Solidedge closed 1 year ago

Solidedge commented 2 years ago

The Multiprocessing API will sometimes be blocked or is unable to stop measuring. The PPK2 seems to be remain in measure mode until the program is killed. I've traced the block to the following function:

def stop_measuring(self):
        PPK2_API.stop_measuring(self)
        PPK2_API.get_data(self) # flush the serial buffer (to prevent unicode error on next command)
        self._quit_evt.set()
        if self._fetcher is not None:
            print(self._fetcher)
            self._fetcher.join() # join() will block if the queue isn't empty
            self._fetcher = None

The code that produces the bug:

import time
from ppk2_api.ppk2_api import PPK2_MP

ppk2s_connected = PPK2_MP.list_devices()
if(len(ppk2s_connected) == 1):
    ppk2_port = ppk2s_connected[0]
    print(f'Found PPK2 at {ppk2_port}')
else:
    print(f'Too many connected PPK2\'s: {ppk2s_connected}')
    exit()

ppk2_test = PPK2_MP(ppk2_port)      # Serial port
ppk2_test.get_modifiers()
ppk2_test.use_source_meter()        # Set source meter mode
ppk2_test.set_source_voltage(3750)  # Set source voltage in mV
ppk2_test.toggle_DUT_power("ON")    # Enable DUT power

time.sleep(1) # For timing

ppk2_test.start_measuring()

time.sleep(2) # For timing

for i in range(0, 10):
    read_data = ppk2_test.get_data()
    if read_data != b'':
        samples = ppk2_test.get_samples(read_data)
        print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
    time.sleep(2) # Higher time seems to recreate the bug more often

ppk2_test.toggle_DUT_power("OFF")
ppk2_test.stop_measuring()
NejcKle commented 1 year ago

Fixed in #16.