1

I currently have an Arduino Mega 2560. This is hooked up to a DHT-11 sensor which works perfectly.

The code I use for the Arduino:

#include <dht.h>

#define dht_dpin A0 //no ; here. Set equal to channel sensor is on

dht DHT;

void setup(){
  Serial.begin(9600);
  delay(300);//Let system settle
  delay(700);//Wait rest of 1000ms recommended delay before
  //accessing sensor
}//end "setup()"

void loop(){
  //This is the "heart" of the program.
    DHT.read11(dht_dpin);

    Serial.print("Hum: ");
    Serial.print(DHT.humidity);

    Serial.print(" temperature: ");
    Serial.println(DHT.temperature); 
    delay(5000); //no need to access frequently
}// end loop()

Now when I connect my arduino to my pc and launch the Serial Monitor, everything works fine and the value is emitted every 5 seconds. If the problem was with the Arduino I wouldve changed stack exchange site

The problem is that I've now connected my Arduino to my Raspberry Pi 3. I'm reading the output of my USB device with the Python pySerial.

Python code:

import serial

ser = serial.Serial('/dev/ttyACM0', 9600)
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False

ser.timeout = 1 #turned off and on depeinding on output

s = [0]
while 1:
    try:
        s[0] = ser.readline()
        if(s[0]):
            print[s0]
            hum = s[0].split()[1]
            temp = s[0].split()[3]
            print "hum:" + hum
            print "temp" + temp
    except Exception, e1:
        print "error: " + str(e1)
        print ser.readline()
ser.close() 

Now when I comment out ser.timeout = 1 I get the following output:

�������������������������������������������������������������������������������������������������������Hum: 47.00 temperature: 24.00

hum:47.00
temp24.00
Hum: 47.00 temperature: 24.00

hum:47.00
temp24.00
Hum: 47.00 temperature: 24.00

hum:47.00
temp24.00

And then it doesn't output anything anymore. The Arduino is still sending data though (as seen by the LED light).

If I add the ser.timeout = 1 I do get output everytime the Arduino sends data but it seems corrupt:

�������������������
error: list index out of range
Hum: 47.00 temperature: 24.00

Hum: 47.00 temperature: 24.00

hum:47.00
temp24.00
Hum: 47.00 temperature: 24.00

hum:47.00
temp24.00
����������������������������������������������
error: list index out of range

����������������������������������������������
error: list index out of range

����������������������������������������������
error: list index out of range

(After the last hum and temp values are shown, the question marks are the only data I receive)

What is causing this and why? How can I resolve this?

0

I'm having a similar issue with python's serial module talking to an arduino (although the problem may not be limited to that). I'm finding that if you change the timeout parameter after the port is opened, strange things seem to happen. For example:

self.source = serial.Serial()
self.source.baudrate = self.baud
self.source.port = self.port
self.source.bytesize = serial.EIGHTBITS
self.source.parity = serial.PARITY_NONE
self.source.stopbits = serial.STOPBITS_ONE
self.source.timeout = 0
self.source.write_timeout = 0.5
self.source.xonxoff = False
self.source.rtscts = False
self.source.open()

The above code does just what you would expect. And whether timeout is set to 0 or 1 (or whatever) seems to work as described.

However, if after you open the port, you subsequently do:

(port is open as self.source)
self.source.timeout = 0

...I start to see corrupted data in the stream.

You might want to try setting the timeout value during your construction of the "ser" object to see if that changes your situation.

I'd be very interested in hearing about what happens as I've chased this for many hours myself.

1
  • For some reason if I added the ser.timeout = 1 in the while loop, I don't receive any corrupted data. Very weird behaviour
    – Ivaro18
    Nov 6 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.