简体   繁体   中英

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. 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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