Python图形/传感器数据

时间:2018-03-28 11:17:17

标签: python

我有一个arduino通过python套接字将文本文件发送到我的计算机,我不确定导致问题的原因是什么,我的python套接字脚本在循环中运行,在批处理脚本的帮助下使其保持运行。问题是有时我从arduino收到txt文件时会得到一个空白文本文件。我不确定它是否有损坏的传输/连接或arduinos代码。最后的问题最终是我的matplotlib图表在到达日志中的空白点时崩溃,因为它没有值只记录行号。继承了python上传服务器脚本,

with open('sensData.txt','wb') as f:
while True:
t = conn.recv(20)
print t
if t =='':
   s.close()
   break     

f.write(t)

这是上传文件的arduino代码的一部分,

void ftransfer() {
char rbuffer[2]; 
char c;
int n; // open test file
SdFile rdfile("sensData.txt", O_READ); // <--- Name of file to be transfered
uint32_t fsize; //Obtain file size
fsize = rdfile.fileSize() - rdfile.curPosition();
Serial.println(fsize);
if (!rdfile.isOpen()) error("File could not be opened, check 
configuration...");  // check for open error
 Ethernet.begin(mac, ip);
 Serial.println("Connecting 10.0.0.89 To Open Transfer...");
 Serial.println(Ethernet.localIP());
 if (client.connect(server1, localport)) {
 Serial.println("Connected");   // if you get a connection, report back via 
 serial:
    while ((n = rdfile.fgets(rbuffer, sizeof(rbuffer))) > 0) {
     client.write(rbuffer, sizeof(rbuffer) - 1);
   }
  client.stop();
 }
else {
Serial.println("No upload server available");     // No response from 
server1
 }
 }

继承了读取传感器的arduino部分,

void loop(void) {
tempvalue = analogRead(tempsensor);

mySensorData = sd.open("sensData.txt", FILE_WRITE);
if (mySensorData) {
Serial.print("Analyzing  Reading "); //Print Your results
delay(5000); //Pause between readings.
mySensorData.println (tempvalue);
mySensorData.close();
ftransfer();
delay(1000);
sd.remove("sensData.txt");
Serial.println("Done");//close the file
}
}

这是我的python脚本中更新图形日志文件的部分,

 def Log(self):
     with open('sensData.txt') as f: 
        FILE2 = f.read()

        now = datetime.datetime.now()
     if self.LogUpdate == 1:
        t = threading.Timer(1, self.Log) #every 45 minutes
        t.start()
        #print "disabled"
     if self.LogUpdate == 0:  
        p = open( 'sens-Data-Log.txt', 'a' ) #(w= write)(a= 
        p.write(str(self.count) + ' ' + FILE2 + '\n')
        print "itworks"
        self.count+=1
        #print self.count
        p.close()
        t = threading.Timer(5, self.Log) #every 45 minutes
        t.start()

我需要帮助搞清楚我能在这里做些什么来阻止上传空白的sensData.txt文件,如果python脚本收到损坏/空白传输怎么办?即使我必须让python输入一个0.0的假值,至少会让我的程序崩溃。如果sens-Data-txt文件为空,我更倾向于让python脚本不写入日志?

我认为通过这样做可以解决这个问题,

  def Log(self):
     with open('sensData.txt') as f: 
        FILE2 = f.read()
        now = datetime.datetime.now()
     if FILE2 =="":
        print "Text File Was Blank !!"
        t = threading.Timer(3, self.Log) #every 45 minutes
        t.start()
        return

0 个答案:

没有答案