import numpy
import argparse
import random
+import time
+import matplotlib
+matplotlib.use('GTKAgg')
+#matplotlib.interactive(True)
+from matplotlib import pyplot as plt
class rdevice(object):
def __init__(self, rcon = None):
if (sl == last_line):
return res
+ def get_multiline_vector(self, command = None, first_line = 'begin',
+ last_line = 'end', try_cycles = 10):
+ self.wait_stamp()
+ self.rcon.write(command);
+ s = self.get_multiline(last_line = last_line)
+ if s is None:
+ print("scan read timeout")
+ return None
+ si = s.find(first_line)
+ ei = s.find(last_line)
+ if (si == -1) or (ei == -1):
+ print("begin, end not found")
+ return None
+ si = s.find('\n', si, ei)
+ return numpy.fromstring(s[si:ei], dtype=float, count=-1, sep=' ')
+
def rdevice_com_test(rcon):
rcon.write("init\n");
rcon.write("run\n");
scan_cnt = 0
scan_all=numpy.ndarray(shape=(0,0), dtype=float)
while True:
- r.wait_stamp()
- r.rcon.write("run\n");
- s = r.get_multiline(last_line = 'end')
- if s is None:
- print("scan read timeout")
- continue
- si = s.find('bank')
- ei = s.find('end')
- if (si == -1) or (ei == -1):
- print("bank, end not found")
+ scan_act=r.get_multiline_vector(command = 'run\n',
+ first_line = 'bank', last_line = 'end')
+ if scan_act is None:
continue
- si = s.find('\n', si, ei)
- scan_act=numpy.fromstring(s[si:ei], dtype=float, count=-1, sep=' ')
- print(scan_act);
+ print(scan_act)
if scan_all.size == 0:
scan_all=numpy.ndarray(shape=(scan_count,scan_act.size), dtype=float)
scan_all[scan_cnt,:]=scan_act
F.write('\n')
F.close()
exit(0)
+
+ if action == 'plot':
+ plt_block = True
+
+ if not plt_block:
+ fig, ax = plt.subplots(1, 1)
+ plt.show(False)
+ plt.draw()
+ r.wait_stamp()
+ r.rcon.write("init\n");
+ scan_cnt = 0
+ scan_last = None;
+ plt_first = True
+
+ while True:
+ scan_act=r.get_multiline_vector(command = 'run\n',
+ first_line = 'bank', last_line = 'end')
+ if scan_act is None:
+ continue
+ print(scan_act)
+ if scan_last is None:
+ scan_last = scan_act
+ if plt_block or plt_first:
+ fig, ax = plt.subplots(1, 1)
+ last_line = ax.plot(range(len(scan_last)), scan_last, 'y')[0]
+ act_line = ax.plot(range(len(scan_act)), scan_act, 'r')[0]
+ plt_first = False
+ else:
+ last_line.set_data(range(len(scan_last)), scan_last)
+ act_line.set_data(range(len(scan_act)), scan_act)
+ #act_line.set_xdata(scan_act)
+ fig.canvas.draw()
+ plt.show(plt_block)
+
+ scan_last = scan_act
+ time.sleep(1)