为什么Arduino的串行通信会在一段时间后停止

时间:2016-01-09 13:30:52

标签: python-2.7 arduino serial-port usb

我正在用arduino uno作为温度控制器建造一个种植箱/玻璃容器。 arduino的简单草图:如果DS18B20传感器小于25'C,则开启继电器,加热电缆连接到该继电器。循环30s,每次Serial.print(温度)到PC,我在那里收集数据并制作尕照片。 --->这是问题所在。

一段时间后(从15分钟到4小时)。与PC的串行通信停止。当我试图将新草图上传到arduino时,我收到了错误信息:  avrdude:ser_open():无法为“\。\ COM3”设置com-state

我需要重新拔下并插入USB电缆(或在Windows设备管理器中将其关闭并打开),同时重启正在收集数据的Python应用程序。 (非常不满意的修复)。

所以我的问题是:  1.为什么?  2.如何解决?  3.或者如何进行一些解决方法,例如从代码重置COM端口(最好是python2.7)

PS。我在做什么以及它如何工作(并且不起作用)的示例:Life of pepper

PS2。我的目标是为植物建立可控制的栖息地,在那里我可以看到行为因温度,日持续时间,光照强度,湿度而异。

请帮帮我:-)。

ARDUINO UNO SKETCH

    #include <OneWire.h>
    #include <DallasTemperature.h>

    // Data wire is plugged into port 2 on the A

rduino
    #define ONE_WIRE_BUS 2
    #define PIN_HEATING 6

    float temperatura = 30.0;

    // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);

    // Pass our oneWire reference to Dallas Temperature.
    DallasTemperature sensors(&oneWire);

    void setup(void)
    {
      // start serial port
      Serial.begin(9600);

      // Start up the library
      sensors.begin();

      pinMode(PIN_HEATING, OUTPUT);
    }

    void loop(void)
    {
      delay(30000);

      if(temperatura<25.0){
        digitalWrite(PIN_HEATING, HIGH);  
        }
      else{
        digitalWrite(PIN_HEATING, LOW);  
        }

      sensors.requestTemperatures(); // Send the command to get temperatures
      // After we got the temperatures, we can print them here.
      // We use the function ByIndex, and as an example get the temperature from the first sensor only.
      temperatura = sensors.getTempCByIndex(0);

      Serial.print("#");
      Serial.print(temperatura);
    }

PYTHON2.7接收部分

import serial #pySerial

ser = serial.Serial()
ser.port = "COM3"
ser.open()

data = ser.read_all().split("#")
datasize = len(data)
if datasize>1:
    temp = data[datasize-1]
    tempstr = " / " + str(temp) + "'C"
else:
    tempstr=" / ----- "

2 个答案:

答案 0 :(得分:1)

我过去遇到过类似的问题,串口无法响应。在我的案例中,解决方案并不是非常直观,但仍然有效。这也可能对你有所帮助:

  • 确保您用于连接arduino的USB端口不与其他设备共享(例如使用USB多点集线器)。如果您正在使用具有多个USB端口的桌面(通常位于桌面背面),请拔下所有其他USB设备(键盘,鼠标等)并将其重新连接到其他USB。使用计算机上可用的最快USB端口(USB 3.0,如果有),并专门用于与arduino进行通信。通信端口无响应的原因有时是因为如果连接了多个设备,电压和/或电流会瞬间波动。
  • 确保为您的计算机提供稳定的电源,由于负载过重导致的电压漂移有时会导致PC外围设备断开/重新连接。

如果这不起作用,请尝试使用此快速软件修复程序。在代码中的delay(30000)语句之前,添加以下内容:

 if(!Serial) {  //check if Serial is available... if not,
Serial.end();      // close serial port
delay(100);        //wait 100 millis
Serial.begin(9600); // reenable serial again
}

这可能至少可以确保您的应用程序继续运行,而无需您的干预。

答案 1 :(得分:0)

所以在我的情况下,问题是通过适当的浪涌抑制器连接我的计算机来解决的,以前我用一根简单的电缆将它连接到墙上,而我的公寓里有旧的2线,非接地的电气装置。 / p>