我正在嘗試通過TCP讀取數據,並將其保存並在同一時間繪製。到目前爲止,我正在讀取數據並保存到一個文本文件,但我有繪製它的問題。數據是以字符串形式出現的,我無法弄清楚如何將其轉換爲int或float。或者如何將這些值傳遞給一個數組來繪圖。如何繪製來自TCP的數據?
這裏是我的代碼:
服務器:
import socket
import mraa
import time
import numpy
host = '172.20.61.19'
port = 5000
x = mraa.Gpio(20)
x.dir(mraa.DIR_OUT)
s = socket.socket()
s.bind((host, port))
s.listen(1)
c, addr = s.accept()
print "Connection from: " + str(addr)
while True:
x.write(1)
time.sleep(2)
data = x.read()
print str(data)
c.send(str(data))
x.write(0)
time.sleep(0.5)
data = x.read()
print str(data)
c.send(str(data))
s.close()
客戶:
import socket
from collections import deque
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
host = '172.20.61.19'
port = 5000
s = socket.socket()
s.connect((host,port))
while True:
data = s.recv(1024)
print data
secPlot = ax.plot(int(data), 'b-')
fig.canvas.draw()
s.close()
誰能幫助我?
提前致謝!