我有一个奇怪的错误,我似乎无法找到。情况是我有一个带温度的arduino板和一个光传感器:光传感器用于查看某个房间是否“打开”(如果房间内没有移动一段时间,灯会熄灭)。我正在使用串行端口将数据推送到运行Processing脚本的服务器。如果感测到的光线超过某个阈值,则arduino板会推动“OPEN”,如果不是,则“关闭”。它还将温度推到换行线上。它每两秒重复一次。
使用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 :(得分:0)
好吧,我想我明白了,实际上我是傻瓜。
对于最终未来的读者:致电
openClosedFile = createWriter(openClosedFileName);
writeOpenClosed()函数中的将打开文件,如果不需要写任何内容,它将永远不会被关闭。由于文件永远不会被关闭(因为下次调用该函数时会有一个新对象),它不会释放要读取的文件。