usb serial setup for nano/ubuntu14

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!

So many options !
I use this:

stty -F /dev/ttyUSB0 speed 9600
echo "Hello World" > /dev/ttyUSB0

even with that simple stty command i get the same effect. the python hangs on readline. minicom works, though the first "s" has no result, the second, ..., all work.

and doing the same commands in the python shell works as well.

>>> import serial
>>> ser = serial.Serial('/dev/ttyUSB0',9600)
>>> ser.write('s\n')
2
>>> ser.flush()
>>> result = ser.readline()
>>> print(result)
44.00 73.40

but not as a script.py

not sure what to do, other than maybe try a different arduino board.

When you open the serial port it causes the nano to reset. Pause your python code to allow for that.

Have a look at how this Python-Arduino demo deals with the reset issue. It waits for the Arduino to say it is ready.

...R

thanks loads. i did try putting in delays, but not between the open and the send apparently.

i guess the flashing red light should have been a clue!