GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Feb-2007, 07:40
fogled@mizzou fogled@mizzou is offline
New Member
 
Join Date: Feb 2007
Posts: 4
fogled@mizzou is on a distinguished road

Trouble with pyserial script


I have pyserial installed on a Mac Mini (10.4.8) with a Keyspan USB-Serial adapter. Everything is installed correctly, and I can send serial commands in python interactive mode just fine.

But... When I create a script with the same commands, the script does everything else (assigning the port, etc.), but it does NOT actually send the serial string.

The script that is NOT working is root:wheel, has been chmod'd up to 777, and is being run by root. Here it is:

Python Code:
!# /usr/local/bin/python
import serial
ser = serial.Serial('/dev/tty.KeySerial1')
print ser.portstr
ser.write("Hello World\r")
print "Did it Work?"
ser.close()

...but of course when I enter python interactive mode, and type the same commands, the ser.write() command sends out the serial like it's supposed to.

Can anyone help me get the script to send the serial?

PS: I'm a python newbie - this is the first thing I've tried with python, but I've done a lot of PHP stuff. I got pyserial installed and had it working in interactive mode in about 10 minutes, but I've spun my wheels on getting the script to work for a day now.

THANKS!
Last edited by LuciWiz : 21-Feb-2007 at 11:49. Reason: Please insert your Python code between [py] & [/py] tags
  #2  
Old 21-Feb-2007, 23:06
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Trouble with pyserial script


Try changing your first line to:
Python Code:
#! /usr/local/bin/python

The "she-bang" line needs to be marked as a comment, which it currently isn't. I'm surprised you aren't getting error messages, though the OS could be smart enough to figure it out.

The rest of your script looks like it should be working. What Python editor are you using? Many of them, such as SPE, let you run the code within the IDE and see if there are any errors.

Additionally, you may also want to look at Google's Python newsgroup. Most of the people on this forum are Python newbies (I've only been using it for a year and I imagine I'm one of the "senior Pythonistas" here).
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 22-Feb-2007, 06:15
fogled@mizzou fogled@mizzou is offline
New Member
 
Join Date: Feb 2007
Posts: 4
fogled@mizzou is on a distinguished road
Red face

Re: Trouble with pyserial script


Quote:
Originally Posted by crystalattice
Try changing your first line to:
Python Code:
#! /usr/local/bin/python

Oh, sorry, my shebang line is right in the script, I transposed the first two characters somehow when I was copying and pasting for the post.
  #4  
Old 22-Feb-2007, 07:52
fogled@mizzou fogled@mizzou is offline
New Member
 
Join Date: Feb 2007
Posts: 4
fogled@mizzou is on a distinguished road
Thumbs up

Re: Trouble with pyserial script


SOLVED. I needed to add a time.sleep(0.1) statement after the ser.write() to give the script enough time to do the work before closing.
  #5  
Old 22-Feb-2007, 09:49
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: Trouble with pyserial script


Does that point to a need to flush the output? Instead of sleep()ing, you could call flush() on it (i.e. ser.flush() )? I'm just guessing here. I've never used pyserial so I don't know it's limitations/features.
  #6  
Old 22-Feb-2007, 13:23
fogled@mizzou fogled@mizzou is offline
New Member
 
Join Date: Feb 2007
Posts: 4
fogled@mizzou is on a distinguished road
Thumbs up

Re: Trouble with pyserial script


Quote:
Originally Posted by darelf
Instead of sleep()ing, you could call flush() on it

Yes, you are correct: ser.flush() also causes the serial command to be sent. This, I'm sure, is a much more appropriate way to use pyserial than using time.sleep().

Thanks,
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Web stats fcolor Web Design Forum 10 12-Jul-2007 08:48
Need Free Website Templates sam_dezine Web Design Forum 8 06-Sep-2006 03:25
Who Is A Web Designer ? alicehopkins Web Design Forum 1 13-Jun-2006 02:56
HTML Code amgujral Web Design Forum 0 07-Apr-2006 09:46
Two virtual hosts, cgi script behaves differently on each blimbo Apache Web Server Forum 0 04-Aug-2004 09:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:10.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.