如何在不停止整张纸条的情况下停止数据测量

2024-04-24 08:09:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一些代码从我的linux笔记本电脑上的usb鼠标获取x,y delta。它是一个获取delta并用matplotlib绘制的脚本。但主要的问题是,我不能停止测量而不杀死整个脚本。我仍然是一个初学者在编程方面,所以任何帮助将是很好的。你知道吗

我的代码:

import struct
import matplotlib.pyplot as plt
import numpy as np
import time
from drawnow import * 

file = open( "/dev/input/mouse2", "rb" );
test = []
plt.ion()

def makeFig():
 plt.plot(test)
 #plt.show()

def getMouseEvent():
  buf = file.read(3);
  button = ord( buf[0] );
  bLeft = button & 0x1;
  x,y = struct.unpack( "bb", buf[1:] )  
  print ("x: %d, y: %d\n" % (x, y) )  
  return x,y


while True:
 test.append(getMouseEvent())
 drawnow(makeFig)

file.close();

Tags: 代码testimport脚本matplotlibdefasplt
1条回答
网友
1楼 · 发布于 2024-04-24 08:09:28

你必须决定在什么情况下停止脚本。例如,这将在5秒后停止:

start_time = time.time()
elapsed = 0
while elapsed < 5:
    elapsed = time.time() - start_time:
    test.append(getMouseEvent())

drawnow(makeFig)

如果要在100次测量后停止:

count = 0
while count < 100:
    count += 1
    test.append(getMouseEvent())
    time.sleep(1)  # <   optional

drawnow(makeFig)

相关问题 更多 >