Processing 处理:一个文件将写入,另一个不会写入
我有一个奇怪的错误,我似乎真的找不出来。情况是,我有一个arduino板,上面有温度和光传感器:光传感器用于查看某个房间是否“打开”,如果房间在某个时间内没有移动,灯就会熄灭。我使用串行端口将数据推送到运行处理脚本的服务器。如果感应到的光线高于某个固定点,arduino板将按下“打开”,如果不在固定点,则按下“关闭”。它还将温度推到一条新线上。它每两秒钟重复一次 用minicom监控串行端口,一切正常。即使在脚本中,我输出的数据也证实了一切正常。除此之外,当我尝试将数据写入“open.txt”时,它似乎不是这样,而“temp.txt”工作正常,尝试跟踪-f两个文件,temp.txt得到更新,而open.txt只是保持为空Processing 处理:一个文件将写入,另一个不会写入,processing,filewriter,Processing,Filewriter,我有一个奇怪的错误,我似乎真的找不出来。情况是,我有一个arduino板,上面有温度和光传感器:光传感器用于查看某个房间是否“打开”,如果房间在某个时间内没有移动,灯就会熄灭。我使用串行端口将数据推送到运行处理脚本的服务器。如果感应到的光线高于某个固定点,arduino板将按下“打开”,如果不在固定点,则按下“关闭”。它还将温度推到一条新线上。它每两秒钟重复一次 用minicom监控串行端口,一切正常。即使在脚本中,我输出的数据也证实了一切正常。除此之外,当我尝试将数据写入“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;
}
我是不是没看到什么,或者这里发生了什么事?好吧,我想我明白了,其实是我变傻了 给未来的读者:打电话
openClosedFile = createWriter(openClosedFileName);
在WRITEOP中,函数将打开文件,如果不需要写入任何内容,它将永远不会关闭。由于下次调用函数时将生成一个新对象,因此该文件将永远不会关闭,因此它不会释放要读取的文件
openClosedFile = createWriter(openClosedFileName);