我有一个python (2.7)脚本,它以以下形式通过串行端口读取传入数据:
ID: 648 Data: 45 77 33 9C 26 9A 1F 42
ID: 363 Data: 35 74 36 BC 26 9D
...数据流包含几个不同的ID(大约30个),它们以2-8个数据字节周期性地重复。ID的频率范围为10-120 ms.有些人比其他人更早地重复。
无论如何,我有一个基本的python脚本,它将这个流读入两个变量(id和data):
import serial
import re
ser = serial.Serial("COM11", 115200)
while 1:
reading = ser.readline()
matchObj = re.match( r'ID:\s([0-9A-F]+)\sData:\s([^\n]+)', reading, re.M|re.I)
if matchObj:
id = matchObj.group(1)
data = matchObj.group(2)
else:
print "No match!!"我想要做的是以数据表格式实时显示这些数据,在数据表中添加新的ID条目,并更新重复的ID条目。这将导致一个表,该表最初将随着ID的发现而增长,然后随着ID数据的更改而更新。
我看到了一些表模块的例子,这些模块允许您向表中添加行,但我也需要能够修改现有的条目,因为这将是最常见的情况。
对于python,我还是很新的,我需要一个不需要过多开销的实现,以使数据更新尽可能快。
有什么想法吗?提前感谢!
发布于 2014-05-12 20:23:31
诅咒是终端显示器的首选.
#!/usr/bin/env python
import curses
import time
def updater():
fakedata = [[1, 78], [2, 97], [1, 45], [2, 2], [3, 89]]
codes_to_linenums_dict = {}
last_line = 0
win = curses.initscr()
for code, data in fakedata:
try:
# if we haven't seen this code before, give it the next line
if code not in codes_to_linenums_dict:
codes_to_linenums_dict[code] = last_line
last_line += 1
# use the line we set for this code.
line = codes_to_linenums_dict[code]
win.addstr(line, 0, '{}: {} '.format(code, data))
win.refresh()
# For display only
time.sleep(2)
except KeyboardInterrupt:
# if endwin isn't called the terminal becomes unuseable.
curses.endwin()
raise
curses.endwin()
if __name__ == '__main__':
updater()~
https://stackoverflow.com/questions/23617003
复制相似问题