2014-10-30 42 views
0

我有一個奇怪的錯誤,我真的似乎無法找到。情況是,我有一個溫度和光線傳感器的arduino板:光線傳感器用於查看某個房間是否「打開」(如果房間內沒有移動一段時間,燈會熄滅)。我正在使用串行端口將數據推送到運行處理腳本的服務器。如果檢測到的光線高於特定的閾值,arduino板推動「OPEN」,如果不是,則「CLOSED」關閉。它也推動換行的溫度。它每兩秒重複一次。處理:一個文件將寫入,其他不會

用minicom監測串行端口,似乎所有工作正常。即使在腳本中,我輸出的數據也證實一切都應該正常工作。除了當我嘗試寫入數據到'open.txt'似乎不是這樣,而'temp.txt'工作正常(嘗試尾-f兩個文件,temp.txt得到更新,而open.txt只是保持爲空

import processing.serial.*; 
Serial mySerial; 
PrintWriter openClosedFile; 
String openClosedFileName; 
String currentOpenClosed; 
PrintWriter temperatureFile; 
String temperatureFileName; 
int currentTemp; 

void setup() 
{ 
    mySerial = new Serial(this, Serial.list()[0], 9600); 
    openClosedFileName = "open.txt"; 
    openClosedFile = createWriter(openClosedFileName); 
    currentOpenClosed = "CLOSED"; 
    temperatureFileName = "temp.txt"; 
    temperatureFile = createWriter(temperatureFileName); 
    currentTemp = 0; 
} 

void draw() 
{ 
    if (mySerial.available() > 0) 
    { 
     String value = mySerial.readStringUntil('\n'); 
     if (value != null) 
     { 
      String timestamp = nf(day(),2) + "/" + nf(month(), 2) + "/" + year() + " " +   nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2); 
     println(timestamp); 
     value = trim(value); 
      if (isNumeral(value)) 
      writeTemperature(value); 
      else 
      writeOpenClosed(value); 
     } 
    } 
} 

void writeOpenClosed(String val) 
{ 
    print("OpenClosed: "); 
    println(val); 
    boolean writtenToFile = false; 
    openClosedFile = createWriter(openClosedFileName); 
    if (val.equals("OPEN") && !currentOpenClosed.equals("OPEN")) 
    { 
    println("val=OPEN and currentOpenClosed!=OPEN"); 
    openClosedFile.print("1"); 
    writtenToFile = true; 
    } 
    else if (val.equals("CLOSED") && !currentOpenClosed.equals("CLOSED")) 
    { 
    println("val=CLOSED and currentOpenClosed!=CLOSED"); 
    openClosedFile.print("0"); 
    writtenToFile = true; 
    } 

    if (writtenToFile) 
    { 
    currentOpenClosed = val; 
    openClosedFile.flush(); 
    openClosedFile.close(); 
    println("Written OpenClosed To File"); 
    } 
} 

void writeTemperature(String val) 
{ 
    print("temperature: "); 
    println(val); 
    int intTemp = Integer.parseInt(val); 
    if (intTemp != currentTemp) 
    { 
    currentTemp = intTemp; 
    temperatureFile = createWriter(temperatureFileName); 
    temperatureFile.print(val); 
    temperatureFile.flush(); 
    temperatureFile.close(); 
    println("Written Temperature To File"); 
    } 
} 

boolean isNumeral(String val) 
{ 
    for (int i = 0; i < val.length(); i++) 
    { 
    if (val.charAt(i) < 48 || val.charAt(i) > 57) 
     return false; 
    } 
    return true; 
} 

我認爲會有一些語法錯誤(我以前沒有使用過處理),但兩者的功能似乎是在做同樣的...

一些示例輸出:

Listening for transport dt_socket at address: 8212 

30/10/2014 12:14:57 
OpenClosed: CD 
30/10/2014 12:14:57 
temperature: 24 
Written Temperature To File 
30/10/2014 12:14:59 
OpenClosed: CLOSED 
30/10/2014 12:14:59 
temperature: 25 
Written Temperature To File 
30/10/2014 12:15:01 
OpenClosed: CLOSED 
30/10/2014 12:15:01 
temperature: 24 
Written Temperature To File 
30/10/2014 12:15:03 
OpenClosed: CLOSED 
30/10/2014 12:15:03 
temperature: 25 
Written Temperature To File 
30/10/2014 12:15:05 
OpenClosed: OPEN 
val=OPEN and currentOpenClosed!=OPEN 
Written OpenClosed To File 
30/10/2014 12:15:05 
temperature: 20 
Written Temperature To File 
30/10/2014 12:15:07 
OpenClosed: OPEN 
30/10/2014 12:15:07 
temperature: 20 
30/10/2014 12:15:09 
OpenClosed: OPEN 
30/10/2014 12:15:09 
temperature: 20 
^C 

我沒有看到什麼,或者可能會發生什麼嗎?

回答

0

好吧,我想我明白了,實際上我是愚蠢的。

對於最終未來的讀者:打電話

openClosedFile = createWriter(openClosedFileName); 
在writeOpenClosed()函數

將打開該文件,如果沒有已被寫入它永遠不會關閉。由於該文件永遠不會被關閉(因爲下次調用該函數時會有一個新對象),所以它不會釋放要讀取的文件。

相關問題