pyserial code not working as a saved script

Links to Python scripts.

Moderators: KDoiron, ChrJim, mawe, python

pyserial code not working as a saved script

Postby kishoreinme on Tue Mar 09, 2010 1:49 am

hello there

Iam using python 2.5.4
pyserial 2.4
pywin32-214

on windows 7

i hav a small test script written to query a serial device (arduino)
and get back reply appropriately

////file: test.py

import serial
print 'hi'
ser=serial.Serial(port='\\.\COM2', baudrate=9600)
ser.close()
ser.open()
ser.write('1')
ser.readline()
ser.readline()
ser.close()

the device waits for '1' through its serial interface and print two
lines if it gets '1'

"Some Data found" and "Header received"

the script works on IDLE well when given one line at a time

but when given in command line as python test.py it prints hi and wait
forever

can anyone help?
thanks in advance
kishoreinme
Python Fan
Python Fan
 
Posts: 1
Joined: Tue Mar 09, 2010 1:41 am

Re: pyserial code not working as a saved script

Postby Micseydel on Tue Mar 09, 2010 2:42 pm

Use code tags next time.

The issue is that when you're using Python interactively, a function's return value is echoed back, but this isn't the case in a script. You have to explicitly print it.

So, for example, here's an interactive session in IDLE

Code: Select all
>>> x = 7
>>> x
7
>>>


If you put

Code: Select all
x = 7
x


in a script, and run it, you'll get no output. So, to get output, you do this:

Code: Select all
x = 7
print x


(This will also work interactively.)

For your script, you need to change

Code: Select all
ser.readline()
ser.readline()


to

Code: Select all
print ser.readline()
print ser.readline()
Python 2.6.5 on Ubuntu Lucid Lynx, Mac OS X Snow Leopard, and Windows 7 triple boot.
User avatar
Micseydel
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1536
Joined: Sun Aug 26, 2007 2:25 pm
Location: Redondo Beach, California

Re: pyserial code not working as a saved script

Postby Sneekula on Wed Mar 10, 2010 8:48 am

Also read:
"*** THE "SCRIPTS" FORUM IS FOR FINISHED SCRIPTS ONLY"
Babes love a man who uses his Lisp!
User avatar
Sneekula
Python Guru
Python Guru
 
Posts: 745
Joined: Sun May 28, 2006 6:27 am
Location: Detroit


Return to Python Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Dreamlink Web hosting and Traduzioni Rumeno Italiano and ASSP Deluxe for cPanel.