Sampling from serial port with different frequency than refresh rate


I am trying to read from a force sensor (in a grasp task) while I am showing the participant in real time how much they are pressing the force sensor. I will also have EEG neural recording so it is important for me to be able to record the readings from the force sensor with the highest frequency possible.

When I am using a “While” for 5 seconds (the trial duration), if there is no win.flip(), I can record from the sensor around 20000 samples during the 5 seconds trial. However, if I have win.flip() within the while (which I need to have for the experiment) then the number of samples becomes 300 samples (60 (refresh rate) * 5 (seconds)). Is there a way to record from the sensor with the highest frequency within the while loop?

t_start = time.time()
t_end = t_start + 5
while time.time() <= t_end:
    # show the blank screen
    # save time stamp of force
    # recording the force
    data = ser.readline()

When you create the window, is waitBlanking set to True? If so try running with it as False.

The other option if that doesn’t work is to use multi-threading, querying the serial port in a different thread so it isn’t tied to the flip call returning.

Hey Todd! Thanks for your reply! I tried setting waitBlanking set to False, but it did not help. I have started working on multi-threading, since I have never done multi-threading I was reading some tutorials on it. I have made a class for reading the force sensor as:

class ForceRead:
    def __init__(self): = np.array([])  # to store the data of the measurement
        self.timeStamp = np.array([])  # to store the timeStamp of the measurement

    def make_measurement(self, start, stop):
        while start <= stop:
            self.timeStamp = np.append(self.timeStamp, time.time())
            tmp_data = ser.readline()
                tmp_data = float(tmp_data.strip())
            except ValueError:
                tmp_data = np.nan
   = np.append(, tmp_data)
        return, self.timeStamp

The problems I have right now are: 1) It seems this approach slows down the sampling the force sensor a lot, maybe because of appending the numpy array? 2) While I am recording the force sensor in the thread, I need to have another while loop reading the real time of values of sensor during the screen refresh and flip it on the screen. This is the part I am not quite sure how to write the second while. Currently, I have something like this for the flip on the screen

            # find the time that starts the max pressure
            t_start = time.time()
            t_end = t_start + 5
            t = threading.Thread(target=force.make_measurement, args=(t_start, t_end))
            while time.time() <= t_end:
                # show the keep pushing screen
                # print the values of force sensor
                # ?