Some help with Python and serial communication

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
tbp62
 
Posts: 4
Joined: Sun Jan 24, 2010 10:26 pm

Some help with Python and serial communication

Post by tbp62 »

Hello,

A little background... I've just started in the world of circuits and have made it through most of the Experimentation Kit sample circuits to get my feet wet. I have a background in programming software.

My first project is fairly simple and a little silly. Basically I want to turn on / off a vintage stereo receiver using Arduino by sending serial commands from my mac mini media center. So far I have everything working on the Arduino side but am having trouble now that I'm trying to send the serial data.

Here is the Arduino sketch:

Code: Select all

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 

// variables
int pos = 0;
int onOff = 0;

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  
  Serial.begin(9600);
} 
 
void loop() 
{ 
  
  	// send data only when you receive data:
	//if (Serial.available() > 0) {
		// read the incoming byte:
		onOff = Serial.read();

		// say what you got:
		//Serial.print("I received: ");
		//Serial.println(onOff, DEC);

                if (onOff == 48)
                {
                  turnOn();
                }
                else if (onOff == 49)
                {
                  turnOff();
                }
	//}
} 

void turnOn()
{ 
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 

void turnOff()
{ 

  for(pos = 90; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 
Here is my Python script:

Code: Select all

#!/usr/bin/env python
import time
import serial

ser = serial.Serial('/dev/tty.usbserial-A600af9G', 9600, timeout=1)

time.sleep(4.5)
ser.write('0')
I'm not sure about checking the onOff variable for 48 / 49, those are the values I saw when testing with the IDE. Regardless, from the mac if I use the Python environment (by typing "python" into Terminal) and hand type the script above in, everything works great. I can send a 1 or 0 with the ser.write and the little servo moves where it should. What I can't get to work is all of it together in a script as above. I need it in a script to automate the process... any ideas? I have some knowledge of the command line but not using modern serial devices and no experience with Python.

I may be asking in the wrong forum but hope this is a good place to start.

Thanks,
Blake

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: Some help with Python and serial communication

Post by adafruit »

well what isnt working? be more specific than "its not working"

tbp62
 
Posts: 4
Joined: Sun Jan 24, 2010 10:26 pm

Re: Some help with Python and serial communication

Post by tbp62 »

Sorry! ~

When run from the above script I get no movement from the servo. For the circuit itself I'm using the Circ-04 diagram from the experimentation kit. As mentioned before I can make the servo work from the command line when typing everything in by hand, so I must assume that the install of pyserial was successful. It's just running the python script that does not produce servo movement.

I hope that is more helpful!

Thanks,
Blake

mwr
 
Posts: 47
Joined: Sun Jun 07, 2009 3:08 pm

Re: Some help with Python and serial communication

Post by mwr »

First thing I'd check is whether or not the Python found via '/usr/bin/env python' is the same one you get when you run python directly. Try the following and see if it makes any difference (I doubt it will, but let's knock out the easy ones first):

At the command prompt:

Code: Select all

$ which python
That should return '/usr/bin/python' or something similar. Whatever that value is, replace '/usr/bin/env python' on line 1 of your script with it.

Rerun the script and see if that helps.

tbp62
 
Posts: 4
Joined: Sun Jan 24, 2010 10:26 pm

Re: Some help with Python and serial communication

Post by tbp62 »

Ok, some further (odd to me) information. I found that sending the python commands from the command line always works. I've also found that the python script above works ONLY with the Arduino IDE and Serial Monitor open...

For example:

IDE and Serial Monitor are open, I run the following command and the servo moves to position;

blake$ ./1.py

I close the IDE (or just the Serial Monitor) and run the same command and instead of moving to position the servo sounds like it wants to move but does not (I can hear the motor / gears start up but goes nowhere). Also, I can see the Rx LED alight briefly like normal.

Is the python interpreter doing something to 'open' / 'activate' the serial device that my script is not doing?

Blake

PS. I have modified the first line of my script after running 'which python' to /usr/bin/python. Thanks for the suggestion but unfortunately it's behaving the same.

Entropy
 
Posts: 472
Joined: Tue Jan 08, 2008 12:43 am

Re: Some help with Python and serial communication

Post by Entropy »

Hmm...

I don't have access to my files right now, but if you look at the Micropendous firmware distributions, they have an example test script written in Python as part of their USB Virtual Serial to I2C project. That Python script might provide a good starting point for you.

http://code.google.com/p/micropendous/w ... Serial_I2C - unfortunately you may need to download the complete firmware distro to get i2funcs.py, which is where the actual serial read/write calls are.

tbp62
 
Posts: 4
Joined: Sun Jan 24, 2010 10:26 pm

Re: Some help with Python and serial communication

Post by tbp62 »

I've done a lot more digging and it seems that the answer is as simple as when the serial connection is opened it is resetting Arduino (auto-reset "feature"). I found the following two commands that fix the problem for a time (while sleep is running) here: http://www.mydarkmaterials.co.uk/2008/1 ... e-arduino/

Code: Select all

nohup sleep 999 < /dev/cu.usbserial &
stty -f /dev/cu.usbserial -hupcl
I also found how to use a 47 ohm resistor b/w the 3.3V and Reset pins that will disable the auto-reset feature... Not really comfortable with this so currently looking for a software solution. I'll take a look at the micropendous solution, thanks for that.

Blake

7k6solutionman
 
Posts: 1
Joined: Mon Jan 25, 2010 10:19 pm

Re: Some help with Python and serial communication

Post by 7k6solutionman »

hopefully this might help.

I have a pretty large python program that works well with an arduino on linux. Hopefully your problem is not OS related.

Here's what I use to send my serial message to the arduino:

Code: Select all

  ser = serial.Serial()                 #serial port  
  ser.port = serialConfig['serialDevice']
  ser.baudrate = int(serialConfig['serialBaudRate'])
  ser.timeout = 2 
  ser.open()
msg = struct.pack('=cHcHc', HEADER, toMsg, msgType, thirdVal, '\n')        
ser.write(msg)                                   #send message

I also use this function to get messages from arduino:

Code: Select all

sender, msgType, thirdVal = struct.unpack('=HcH', m) # a cool python function that unpacks C data into python variables
Hopefully this might help a little. I've found that formatting the serial with headers and with a newline at the end helps reading the values as they come in.

I don't want to post the whole code because there's some IP involved, but if you have any targeted questions I'll do my best.

Entropy
 
Posts: 472
Joined: Tue Jan 08, 2008 12:43 am

Re: Some help with Python and serial communication

Post by Entropy »

Oh, I never populated the autoreset capacitor on my Boarduino so I don't know if the Micropendous examples will behave any differently.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”