Go Down

Topic: Help with Python communication... (Read 484 times) previous topic - next topic

Robmotron

I am new to the Arduino but have several plans involving serial communication. I have been going through some of the tutorials and modifying them a bit to learn a little more specifics but am running into some problems with it.

I have the arduino just turning on and off an led when when it gets a 'y' or 'n' from the serial connection. When I start the python interpreter I can open the connection using  s = serial.Serial('/dev/ttyUSB', 9600) and then turn the light off and on using s.write() but this wont work at all when the commands are put into a python script run from the command line. However, if I open the connection in the interpreter in one terminal then open another terminal everything works, the script and even redirecting the standard output to the device. Anyone know why this is? Another thing is that when I run the script without the connection open in another terminal the TX and RX lights light up and the 'L' led also blinks twice but the led im trying to turn on and off does not do anything.
Code: [Select]

#! /usr/bin/env python

import sys
import serial

ser = serial.Serial('/dev/ttyACM0', 9600)

ser.write(sys.argv[1])



I run this with a command line argument of either 'y' or 'n' which works only if the connection is open in a second terminal.
Hopefully this isnt too stupid of a question.

Horacio

My Arduino resets itself every time I connect from Python. Wait for 2 seconds with time.sleep() after opening the serial connection, before sending any data. And feel free to checkout https://github.com/hgdeoro/py-arduino-proxy. :-D

Robmotron

That seems to have worked. Thank you so much!!  I can finally move on to more interesting things than blinking a light.

Go Up