I am a bad programmer, so please excuse my simple question. I am trying to create a small program that reads data from a serial interface and displays it on the screen. I was able to do this in iPython laptop and matplotlib, and I was able to add buttons to the screen, which manages the data requests that go to the interface: Press the button → ser.write, ser.read, draw
Now I'm trying to develop a program in such a way that when the button is pressed, data will be re-collected with fixed time steps until the button is turned off again. Can someone please help me with a sketch for such a program?
Still:
%pylab
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Button
import serial
import binascii
import struct
from time import sleep
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.axis([0, 10, 0, 255])
ser = serial.Serial('COM5', 1000000, timeout=0)
ser.write("0".encode())
sleep(0.1)
if ser.read()== b'\xf1':
print ("Interface is responding")
else:
print ("Interface is NOT responding")
ser.flush()
ser.close()
exit(1)
t = np.linspace(0, 10, 2048)
line1, line2 = plt.plot(t,0*t, t,0*t, lw=2)
def ShowChannel(Channel):
if Channel==1:
ser.write("11".encode())
elif Channel==2:
ser.write("12".encode())
sleep(0.05)
Header = ser.read().decode("utf-8")
if Header == "1":
data = ser.read(2048)
y = struct.unpack('2048B', data)
if Channel==1:
line1.set_ydata(y)
elif Channel==2:
line2.set_ydata(y)
fig.canvas.draw()
def one(event):
ShowChannel(1)
def two(event):
ShowChannel(2)
axone = plt.axes([0.1, 0.05, 0.1, 0.075])
axtwo = plt.axes([0.21, 0.05, 0.1, 0.075])
axstart = plt.axes([0.40, 0.05, 0.1, 0.075])
axstop = plt.axes([0.51, 0.05, 0.1, 0.075])
bone = Button(axone, '1')
bone.on_clicked(one)
btwo = Button(axtwo, '2')
btwo.on_clicked(two)
Following the example in the comments, I added the following
rax = plt.axes([0.7, 0.05, 0.1, 0.1], aspect='equal')
labels = ('go!',)
check = CheckButtons(rax, labels, (False, ))
KeepShowing = False
def func(event):
global KeepShowing
KeepShowing = not KeepShowing
check.on_clicked(func)
while True:
if KeepShowing:
ShowChannel(1)
sleep(1)
- , . , , . ipython, .