0

So I'm continuously measuring temperature data with a National Instruments data acquisition system, and I've been able to plot the real time temperature measurements and store that into a dataframe that I can then save.

What I'm trying to do is set the "samplerate" range to be something really high (its at 301 right now) and then stop it when I no longer want to acquire temperature data. Sometimes I need to collect only 100 samples, sometimes over 10k.

When I use this code to break out the loop it prints into the console what I ask, but I cant actually type anything into the console. The console is completely unresponsive to keyboard interrupts. enter image description here

Is there anyway that I can break out of this loop early without out using the keyboard?

import nidaqmx
import matplotlib.pyplot as plt
import pandas as pd

plt.ion()

Samples = []
time = []
freezerTemp = []
rightSideTemp = []

def print_menu():
    print('Press "3" and hit "Enter" to exit ')
    print('3. Exit')

with nidaqmx.Task() as task:

    FreezerT = task.ai_channels.add_ai_thrmcpl_chan("cDAQ1Mod1/ai0")
    RightSideT = task.ai_channels.add_ai_thrmcpl_chan("cDAQ1Mod1/ai3")
    #This controls the sample/second. i.e. if 2 then 2 samples/second.
    samplerate = task.timing.samp_clk_rate = 1

    while True:
        print_menu()

        for samplerate in range(301):
            data = task.read()
            Samples.append(samplerate)
            freezerTemp.append(data[0])
            rightSideTemp.append(data[1])
            time.append(samplerate/1)

            plt.xlabel('Samples')
            plt.ylabel('Temperature (C)')
            plt.scatter(samplerate,freezerTemp[samplerate],c='r')
            plt.scatter(samplerate,rightSideTemp[samplerate],c='b')
            plt.pause(0.05)
            plt.show()

        choice = input('Enter a number: ')
        if choice == '3':
            break

df = pd.DataFrame({'Samples': Samples,
                    'Time(sec)': time,
                'Freezer Temperature': freezerTemp,
                'Right Side': rightSideTemp})

print(df)

dftag
  • 85
  • 2
  • 8
  • Does this answer your question? [What's the simplest way of detecting keyboard input in python from the terminal?](https://stackoverflow.com/questions/13207678/whats-the-simplest-way-of-detecting-keyboard-input-in-python-from-the-terminal) – mkrieger1 Aug 09 '20 at 19:11

2 Answers2

0

Your issue is plt.show(). This is a loop in itself that blocks the rest of your code.

However matplotlib has a feature to capture key commands after the plt.show(). Take a look at their demo here.

https://matplotlib.org/3.1.0/gallery/event_handling/keypress_demo.html

TeddyBearSuicide
  • 1,377
  • 1
  • 6
  • 11
0

What you need is to run the sampling and plotting in a different (background) thread so that your main thread stays responsive.

Have a look at the threading module https://docs.python.org/3/library/threading.html

Strategy:

  • Put your sampling and plotting function in different thread
  • Start that thread
  • Show your menu
  • Stop the other thread from the menu
Holger Waldmann
  • 101
  • 1
  • 3