我正在使用 Arduino Uno rev2设备作为永久连接的设备,有时会向 PC (Windows 7 x64) 发送信号。使用 arduino.cc 中的 Arduino 1.0 软件编译的代码
arduino.cc 上的话题,Arduino stops sending data to Serial after a long time period
Souce code
它运行良好,但有时,经过很长一段时间后,PC 停止从 Arduino 设备接收数据。这不是 PC 软件问题,因为所有软件(腻子、telnet 等)的行为都相同 - 我可以将数据发送到 Arduino(设备响应命令);我就是收不回来。
在 Serial communication stops after long periods. 中描述了类似的问题。 ,但没有提出解决方案。
断开/连接设备暂时解决了该问题,但这不能成为解决方案,因为该设备应该永久且全自动地使用。
使用电路板重置按钮将程序和所有值重置为它的启动无济于事。 PC 未开始接收数据。
笔记:
如果您认为问题出在 arduino 程序错误上,请考虑如何解释 TX 闪烁和重置没有帮助。
最佳答案
也许让软件重置您的问题会得到解决。
我运行以下代码来确定软件重置是否会重置时钟,从而重置 millis()
功能:
void setup()
{
Serial.begin(9600);
Serial.println("Will start sending millis values in 5 seconds...");
delay(5000);
}
void loop()
{
Serial.println(String(millis()));
if (Serial.available() > 0)
{
if (Serial.read() == '@')
{
Serial.println("Rebooting. . .");
delay(100); // Give the computer time to receive the "Rebooting. . ." message, or it won't show up
void (*reboot)(void) = 0; // Creating a function pointer to address 0 then calling it reboots the board.
reboot();
}
}
}
如代码中所述,要使软件重新启动,只需创建一个指向地址 0 的函数指针并调用它即可。
我确实得到了满意的结果:
Will start sending clock values in 5 seconds...
5000
5000
5000
5001
5001
5001
5002
5002
5002
. . .
6804
Rebooting...
Will start sending millis value in 5 seconds...
5000
5000
5000
5001
5001
5001
5002
5002
5002
5003
5003
. . .
我希望这能解决你的问题:)
关于serial-port - Arduino在很长一段时间后停止向串口发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389426/