Hello,
I am missing something, because while my serial communications between my arduino nano (clone) and ubuntu box works pretty well interactively, it fails with a pc-side script.
My arduino code for a dht11 is pretty simple, it reads serial input and ignores all but an "s", which it takes to mean "sample," returning one line of output:
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 2
void setup() {
Serial.begin(9600);
Serial.flush();
}
double fahrenheit(double celsius) {
return 1.8 * celsius + 32;
}
void loop() {
char s = Serial.read();
if (s != 's') return;
// if receive 's', do one sample pair
int chk = DHT11.read(DHT11PIN);
// send value or simple error codes
switch (chk) {
case DHTLIB_OK:
Serial.print((float)DHT11.humidity, 2);
Serial.print(' ');
Serial.println(fahrenheit(DHT11.temperature), 2);
break;
case DHTLIB_ERROR_CHECKSUM:
// let "CK" mean checksum error
Serial.println("CK CK");
break;
case DHTLIB_ERROR_TIMEOUT:
// let "TO" mean timeout error
Serial.println("TO TO");
break;
default:
// let "??" mean unknown error
Serial.println("?? ??");
break;
}
}
This simple script will hang on the readline:
import serial
#import time
ser = serial.Serial('/dev/ttyUSB0',9600)
ser.write('s\n')
ser.flush()
#time.sleep(1)
print("trying..\n")
result = ser.readline()
print(result)
The thing that kills me is that this readline hangs (with or without the sleep) but issuing the same commands in the python shell, one by one, always works.
Obviously something is wrong with my tty setup, but I don't know what it is. I have tried many recommended stty scripts, but most recently::
stty -F /dev/ttyUSB0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts -clocal
I can also use minicom, and it also works, but it does sometimes seem to eat a few "s" characters before it gets started, which is perhaps a clue. My command there is "minicom -b 9600 -o -D /dev/ttyUSB0"
Am I missing something simple?
Thanks!