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
我没有看到什么,或者可能会发生什么吗?