【发布时间】:2014-10-30 23:46:58
【问题描述】:
我有一个奇怪的错误,我似乎真的找不到。情况是我有一个带有温度和光传感器的 arduino 板:光传感器用于查看某个房间是否“打开”(如果房间在一段时间内没有任何动作,灯就会熄灭)。我正在使用串行端口将数据推送到运行处理脚本的服务器。如果感应到的光高于某个阈值,则 arduino 板会推动“打开”,如果不是,则推动“关闭”。它还将温度推到换行符上。它每两秒重复一次。
使用 minicom 监控串行端口似乎一切正常。即使在脚本中,我输出的数据也确认一切正常。除了当我尝试将数据写入“open.txt”时,它似乎并非如此,而“temp.txt”工作正常(尝试使用 tail -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
我没有看到什么,或者这里可能发生了什么?
【问题讨论】:
标签: processing filewriter