繁体   English   中英

Python暂停或停止实时数据

[英]Python pause or stop realtime data

这次,我想知道针对以下情况存在哪些可能的解决方案:我让我的笔记本电脑使用我已经在此处发布的python脚本读取了原始鼠标数据(Ubuntu OS)。 它具有读取鼠标文件并从中提取x,y数据的方法。 一会儿true循环使用此方法将数据放入数组。 一段时间后,当我使用计时器停止读取时,脚本会将数据放入excel文件。 我现在需要的是一个选项来暂停数据流,即在不创建数据的情况下更改鼠标位置,然后恢复它。 我希望有一些东西可以停止阅读并将其写入excel。

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

workbook = xlsxwriter.Workbook('/path/test.xlsx')
worksheet = workbook.add_worksheet()
file = open( "/dev/input/mouse2", "rb" );
test = [(0,0,0)]
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:] ) 
  _zeit = time.time()-test[-1][-1]
  print ("x: %d, y: %d, Zeit: %d\n" % (x, y, _zeit) )  
  return x,y, _zeit

zeit = time.time()

warte = 0
while warte < 20:
 test.append(getMouseEvent())
 warte = time.time()-zeit     

row = 1
col = 0
worksheet.write(0,0, 'x-richtung')
worksheet.write('C1', 'Zeit')
for x, y , t in (test):
    worksheet.write(row, col,     x)
    worksheet.write(row, col + 1, y)
    worksheet.write(row, col + 2, t)
    row += 1
chart = workbook.add_chart({'type': 'line'})
chart.add_series({'values': '=Sheet1!$A$1:$A$'+str(len(test))})
worksheet.insert_chart('D2', chart)
workbook.close()
 #drawnow(makeFig)
 #plt.pause(.00001)
file.close();

拥有“击中暂停/取消暂停。q结束并保存”之类的功能真是太棒了,但是我不知道该怎么做。 任何想法都将是不错的:)哦,我尝试使用matplotlib绘制数据,此方法虽然有效,但仍可用于将来的改进;)

这是标准线程模块的示例-我实际上不知道它的响应速度。 另外,如果您要基于全局热键暂停或开始输入,而不是从脚本开始输入,则这取决于您的桌面环境-我只使用了xlib但应该有一个python包装器,用于在某个地方浮动。

import threading
import struct

data =[]
file = open("/dev/input/mouse0", "rb") 
e=threading.Event()

def getMouseEvent():
  buf = file.read(3);
  #python 2 & 3 compatibility 
  button = buf[0] if isinstance(buf[0], int) else ord(buf[0])
  bLeft = button & 0x1;
  bMiddle = ( button & 0x4 ) > 0;
  bRight = ( button & 0x2 ) > 0;
  x,y = struct.unpack( "bb", buf[1:] );
  return "L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, x, y) 


def mouseCollect():
    global e
    #this will wait while e is False (without breaking the loop)
    #and loop while e is True
    while e.wait(): 
        #do something with MouseEvent data, like append to an array, or redirect to pipe etc. 
        data.append(getMouseEvent()) 
mouseCollectThread = threading.Thread(target=mouseCollect)
mouseCollectThread.start()
#toggle mouseCollect with any keyboard input
#type "q" or "quit" to quit.
while True: 
    x = input() 
    if x.lower() in ['quit', 'q', 'exit']:
        mouseCollectThread._stop()
        file.close() 
        break
    elif x:
        e.clear() if e.isSet() else e.set()

编辑:我在e.isSet之后缺少()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM