Reading serial AMA0 of Arduino writes an empty data
Question: I am connected Arduino to the Raspberry via **/dev/ttyACM0**. Despite the
python script I want to retrieve data from the Arduino, which is doing, but if
you want the data to appear more frequently (1-4 seconds) and the data does
not list any. Data is written out irregularly me, which I do not understand :(

Arduino code:

    
    
    void setup() {
      Serial.begin(9600);
    }
    void loop() {
      Serial.print("25.78"); // temperature °C
      delay(1000);
      Serial.println();
    }
    

Raspberry, python code:

    
    
    import serial
    serialport = serial.Serial("/dev/ttyAMA0", 9600, timeout=0.7)
    response = serialport.readline()
    print response
    serialport.close()
    

Here is an excerpt:

    
    
    pi@raspberrypi:~ $ python test.py
    
    pi@raspberrypi:~ $ python test.py
    
    pi@raspberrypi:~ $ python test.py
    
    
    pi@raspberrypi:~ $ python test.py
    25.78
    pi@raspberrypi:~ $ python test.py
    25.78
    pi@raspberrypi:~ $ python test.py
    25.78
    pi@raspberrypi:~ $ python test.py
    
    pi@raspberrypi:~ $ python test.py
    

I do not know how I could fix it to be 100% functional?


Answer: It looks like your read timeout (0.7s) is smaller than the frequency that you
write the data (every 1000ms). So if you repeatedly execute the script that
reads it, you'll sometimes miss the data. I'd recommend to either increase
your read timeout, or decrease the interval that the Arduino sleeps.

