繁体   English   中英

实时绘制传入的 TCP 数据(过滤掉字符串?)

[英]Plotting incoming TCP data in real-time (filtering out strings?)

我正在尝试绘制通过 TCP 来自仪器的数据值对。 我可以成功接收这些值并将它们打印到我的用户界面,但我似乎无法弄清楚如何在接收这些值时动态绘制它们。 我玩过几个图形库和动画,但主要问题似乎是过滤掉字符串标签和不需要的字符,所以我只能绘制接收到的浮点值。

这是我的代码:

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #s.settimeout(10)
    s.connect((TCP_IP, TCP_PORT))
    s.sendall(MESSAGE)
    #s.shutdown(socket.SHUT_WR)

    s.setblocking(0) # set port to nonblocking

    begin=time.time()     


    while(1):
      if loop == 15:
        text1.insert(END, "Stopped Recieving Continious Data... Press SEND to continue recieving data \n")
        text1.insert(END, " To Stop Continious Transimisson, SEND *** 0")
        text1.see("end")
        break;
    if logData and time.time()-begin > timeout:
        #print("in if 1")
        break
    elif time.time()-begin > timeout*2:
        #print("in if 2")
        break
    try:    
        logData = s.recv(BUFFER_SIZE).strip('\t\r\n')
        f.write(logData)
        f.write('\n')
        if logData:
            udpData(logData) #prints to UI
            print(repr(logData))
            begin=time.time()
            loop = loop + 1
        else:
            time.sleep(0.1)
    except:
        pass

    # perform filtering of strings here or within while loop try?
    # x = logData

    #plt.plot(x, color = 'red', marker = 'o', linestyle = 'dashed', linewidth = 2)
    #plt.show()

此功能由在我的 UI 中按下发送消息按钮激活,并成功接收数据并打印并写入文件。 当我发送一个命令给我一对这样的值时,我将如何解释数据和绘图:

    CCT 1
    Conc1: 1004.5    Conc2: 3003.2
    Conc1: 567.4     Conc2: 4034.2
    ...              ...

在 Conc1:、值和 Conc2 之后似乎有一个 \\t 字符:

感谢您的任何输入或帮助:)

使用文本文件数据的绘图函数也已编写,但字符串在上述文件中,我的动画函数无法正确解析它们,但主要问题是在我释放数据而不是从我写入的文本文件中释放数据时这样做。

    def animate(i):
       xs = []
       ys = []
      with open("C:\\Users\\aeros\\Desktop\\output6.txt") as graph_data:
          for line in graph_data:
                x, y = line.split(" ")
                xs.append(x)
                ys.append(y)

 ax1.clear()
 ax1.plot(xs, ys)

由于您不会向我们展示一个完整的示例,我所能做的就是向您展示我身边的一个示例,希望您能对其进行调整。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import requests

URL = 'https://poloniex.com/public?command=returnTicker'

def poloapi():
    """get a single datapoint
    replace this with your code that returns a single datapoint"""
    data = requests.get(URL).json()
    return float(data['ETH_BCH']['highestBid'])

def animate(i):
    ydata.append(poloapi())
    line.set_data(range(len(ydata)), ydata)
    plt.ylim(min(ydata), max(ydata))
    plt.xlim(0, len(ydata))
    return line,

ydata = [poloapi()]
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
line, = ax1.plot(ydata, marker='o', markersize=10, markerfacecolor='green')
ani = animation.FuncAnimation(fig, animate, interval=30000)
plt.show()

暂无
暂无

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

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