View Full Version : [Question(s)] Whats wrong with my python code?

02-21-2009, 12:33 AM
Here is some python code I am using to read a number from my arduino.

import serial
from time import sleep
var = 0
s = serial.Serial(3)
print s.portstr
print "Closing..."

For some reason the s.read() command wont work. My program just sits their like it is waiting for a response but it never gets on. I know it is not the arduino because this same command works fine in interactive mode but as soon as I try to create a actual python file it stops working. Why is this happening?

02-21-2009, 01:05 AM
According to your code you are using COM4. Is that correct? PYSerial starts numbering COM ports at 0.

02-21-2009, 07:49 AM
I've only just started writing Python so by no means am I an expert... but I don't see your code doing anything other than reading the serial input and then printing a fixed string?

Maybe if you change the code from s.read() to something like 'value = s.read()' then the code would actually read the data and put it in a variable, even if it's not being used?

Just stabbing in the dark here though..

02-21-2009, 10:05 AM
Are you sure the baud rate is correct? I always like to be more specific, rather than rely on system defaults:

s = serial.Serial("COM4",baudrate=115200)


02-21-2009, 01:32 PM
Hey Robotman,
I just started playing with Python yesterday, and I wanted to do exactly what you did. It worked for me. So I changed my original code to basically match yours, just to see if it broke it.

Heres what I ended up with..

import serial
from time import sleep
ser = serial.Serial(15, 19200) # open serial port 16 at 19200 baud
print ser.portstr # check which port was really used
ser.write("!") # write a string to robot
sleep(2) # wait a bit
print ser.read(10) #read a number 9 from robot 10 times, and print it
print "Closing..." #print the word closing
ser.close() # close port

I ran it with IDLE, and it worked fine. Here is what showed up in the IDLE shell.


My code was pretty much the same as yours, except I had the "print" attached to the "ser.read"

I ran it as a standalone too, it opened a window, printed the "Com16", then waited the aloted time, then printed the other stuff and dissapeared.. Thats next.. how to display stuff.

Good Luck
Python ultra noobie, beginner, and I don't know much either...

02-21-2009, 01:40 PM
Cool thanks for the code. It works great! Glad to see somebody new to python like me!:)

02-21-2009, 01:44 PM
Just out of curiosity what is your arduino code?

02-21-2009, 03:00 PM
I'm not using an Arduino. My controller is a Hitec MR-C3024 that came with a Robonova. It uses a stripped down version of basic.


02-21-2009, 08:30 PM
I asked that because it stopped working. I only ran the code in interactive mode as soon as I tried the code in a .py file it stopped working. It's weird because it works wonderful in interactive mode but when it tries to run the .py it just prints COM4 an then freezes. I know it is the read command because when I take it out the program works fine.