简体   繁体   English

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

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

I'm trying to plot pairs of data values coming in from an instrument over TCP.我正在尝试绘制通过 TCP 来自仪器的数据值对。 I can successfully receive these values and print them to my UI, but I can't seem to figure out how to plot these values dynamically as they are being received.我可以成功接收这些值并将它们打印到我的用户界面,但我似乎无法弄清楚如何在接收这些值时动态绘制它们。 I've played around with a couple graphing libraries and animate, but the main problem seems to be filtering out the string labels and unwanted characters so I can only plot the float values being received.我玩过几个图形库和动画,但主要问题似乎是过滤掉字符串标签和不需要的字符,所以我只能绘制接收到的浮点值。

Here is my code:这是我的代码:

    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()

This function is activated by a send message button press within my UI and receives the data successfully and prints and writes to a file.此功能由在我的 UI 中按下发送消息按钮激活,并成功接收数据并打印并写入文件。 How would I interpret the data and plot when I send a command that gives me a pair of values like this:当我发送一个命令给我一对这样的值时,我将如何解释数据和绘图:

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

Also it seems like there is a \\t char after Conc1:, the values, and Conc2:在 Conc1:、值和 Conc2 之后似乎有一个 \\t 字符:

Thanks for any input or help :)感谢您的任何输入或帮助:)

Plotting function using text file data has been written too, but strings are in that file as above and my animate function will not parse them correctly, but the main problems is doing this as I relieve data and not from the text file I write to.使用文本文件数据的绘图函数也已编写,但字符串在上述文件中,我的动画函数无法正确解析它们,但主要问题是在我释放数据而不是从我写入的文本文件中释放数据时这样做。 :

    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)

Since you won't show us a complete example, all I can do is show you an example I have lying around and hope you can adapt it.由于您不会向我们展示一个完整的示例,我所能做的就是向您展示我身边的一个示例,希望您能对其进行调整。

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