2014-10-30 2 views
0

정말 이상하게 보이지 않는 이상한 오류가 있습니다. 상황은 내가 온도와 빛 센서가있는 arduino 보드를 가지고있다. 빛 센서는 특정 룸이 '열렸는지'확인하는 데 사용된다 (일정 시간 동안 방에 움직임이 없다면 빛이 꺼진다). 직렬 포트를 사용하여 처리 스크립트를 실행하는 서버에 데이터를 푸시합니다. Arduino 보드는 센싱 된 빛이 특정 경계선보다 높으면 'OPEN'을, 그렇지 않은 경우 'CLOSED'를 푸시합니다. 그것은 또한 개 글자에 온도를 푸시합니다. 2 초마다 반복됩니다.처리 중 : 하나의 파일이 쓰여지고, 다른 파일은 쓰여지지 않습니다.

모든 것이 정상적으로 작동하는 것처럼 보이는 미니 포트로 직렬 포트를 모니터링하십시오. 스크립트에서조차도, 제가 출력 한 데이터는 모든 것이 잘 작동해야한다는 것을 확인합니다. 그것을 제외하고 'open.txt'에 데이터를 쓰려고 할 때 'temp.txt'가 잘 작동하는 것처럼 보이지 않는 것 같습니다. (두 파일 모두 꼬리말로 시도했지만 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

좋아요, 알아 냈다고 생각합니다. 실제로는 바보가되었습니다. 파일을 열 것입니다 writeOpenClosed() 함수에서

openClosedFile = createWriter(openClosedFileName); 

를 호출하고, 아무것도 쓸 수없는 경우가 폐쇄 만나지됩니다

궁극적으로 미래의 독자

. 파일은 절대로 닫히지 않으므로 (다음에 함수가 호출 될 때 새 객체가 생성 될 것이므로), 읽힐 파일을 해제하지 않습니다.