python question with serial communication

Red Squirrel

No Lifer
May 24, 2003
71,304
14,081
126
www.anyf.ca
I've been trying to make a very simple program that when run, outputs the output of a command to a serial port. For some reason I can't get this to work. I gave up with C++ as serial communication in C++ is very complex then read it's easier in Python. Keep in mind I barely know this language, actually my first time using it, but here goes.

Why is it that this does not work:

Code:
#!/usr/bin/python

import time
import serial

ser = serial.Serial('/dev/arduino-0',9600,timeout=1)

ser.isOpen()

time.sleep(1)

input = 'allstates'

ser.write(input + '\r\n')

time.sleep(2)

out = ser.read(1000)

print out

But if I replace input = 'allstates' with input = raw_input(": ") and type the command manually, then it works?

Why is it not accepting a static string?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Should allstates be surrounded with a double quote instead of a single quote?
 

Red Squirrel

No Lifer
May 24, 2003
71,304
14,081
126
www.anyf.ca
Tried single and double quotes, same result. If I ommit \r it does not work in any case. (the serial app looks for \r\n for a command)
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
I was wondering if maybe \r was being evaluated, rather then sent as a CRLF.

Are you using Python 3.x, by any chance?
 

Red Squirrel

No Lifer
May 24, 2003
71,304
14,081
126
www.anyf.ca
Using 2.6 I believe.

I also tried to use a command line argument, and it does not work either. It seems the raw_input function must be formatting it a special way or something that makes it work.

Also if I type my program manually in the interpreter, then it does work.
 
Last edited:

Red Squirrel

No Lifer
May 24, 2003
71,304
14,081
126
www.anyf.ca
Got it!

Code:
#!/usr/bin/python

import time
import serial
import sys

ser = serial.Serial('/dev/arduino-0',9600,timeout=1)

time.sleep(2)

input = sys.argv[1]

ser.write(input)
ser.write('\r\n')

out = ser.read(1000)

print out

Seems that sleep(1) was not enough, had to sleep for 2 seconds. Does this seem right though? If I use something like picocom, I can issue a command right away and not wait 2 seconds.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
2 vs 1 millisecond could explain the issue. The system is unable in the shorter time to properly configure and get a response back from the hardware.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
2 vs 1 millisecond could explain the issue. The system is unable in the shorter time to properly configure and get a response back from the hardware.

time.sleep(2) sleeps for 2 seconds, not milliseconds.

Blindly sleeping for some arbitrary amount of time seems like a bad idea. Not really familiar with the pySerial API, but you may want to try something like:

Code:
while not ser.isOpen():
   sleep(0.1) # 100 ms
 

Red Squirrel

No Lifer
May 24, 2003
71,304
14,081
126
www.anyf.ca
I tried that and it did not work, but yeah it would be better if I could do something like that instead of sleeping and hoping it does not take longer to initialize at one point and muck up the app.