Results 1 to 9 of 9

Thread: Whats wrong with my python code?

  1. #1
    Join Date
    Dec 2008
    Location
    Washington
    Posts
    312
    Images
    35
    Rep Power
    41

    Whats wrong with my python code?

    Here is some python code I am using to read a number from my arduino.

    PHP Code:
    import serial
    from time import sleep
    var = 0
    serial.Serial(3)
    print 
    s.portstr
    s
    .write("5")
    sleep(2)
    s.read()
    print 
    "Closing..."
    s.close() 
    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?
    Dalton Caughell

  2. #2
    Join Date
    May 2008
    Location
    TN
    Posts
    853
    Images
    99
    Rep Power
    73

    Re: Whats wrong with my python code?

    According to your code you are using COM4. Is that correct? PYSerial starts numbering COM ports at 0.
    "If A is a success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
    -
    Einstein

    Don't be a HelpVampire

  3. #3
    Join Date
    Jul 2008
    Location
    Belgium
    Posts
    633
    Images
    2
    Rep Power
    62

    Re: Whats wrong with my python code?

    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..
    Artificial Intelligence is no match for Natural Stupidity

    "For a list of all the ways technology has failed to improve life, press three" - Alice Kahn

    Resistance is futile! (if < 1)

  4. #4
    Join Date
    May 2008
    Posts
    2,228
    Images
    155
    Rep Power
    127

    Re: Whats wrong with my python code?

    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)

    -Fergs

  5. #5
    Join Date
    Jul 2008
    Location
    South Sphincter Idaho
    Posts
    613
    Images
    104
    Rep Power
    68

    Re: Whats wrong with my python code?

    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.

    COM16
    9999999999
    Closing...

    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...
    Gary
    Team Maggot---Mechs. "Bheka" (retired), "Maggot Mk.3(A)"
    " Keep your stick on the ice ".... Red Green

  6. #6
    Join Date
    Dec 2008
    Location
    Washington
    Posts
    312
    Images
    35
    Rep Power
    41

    Re: Whats wrong with my python code?

    Cool thanks for the code. It works great! Glad to see somebody new to python like me!
    Dalton Caughell

  7. #7
    Join Date
    Dec 2008
    Location
    Washington
    Posts
    312
    Images
    35
    Rep Power
    41

    Re: Whats wrong with my python code?

    Just out of curiosity what is your arduino code?
    Dalton Caughell

  8. #8
    Join Date
    Jul 2008
    Location
    South Sphincter Idaho
    Posts
    613
    Images
    104
    Rep Power
    68

    Re: Whats wrong with my python code?

    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.

    Gary
    Team Maggot---Mechs. "Bheka" (retired), "Maggot Mk.3(A)"
    " Keep your stick on the ice ".... Red Green

  9. #9
    Join Date
    Dec 2008
    Location
    Washington
    Posts
    312
    Images
    35
    Rep Power
    41

    Re: Whats wrong with my python code?

    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.
    Dalton Caughell

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Just For Fun Phidgets Code Contest.
    By Droid Works in forum Software and Programming
    Replies: 28
    Last Post: 04-24-2012, 12:00 PM
  2. Discussion Python Project Sharing and help Thread
    By ROBOTMAN in forum Software and Programming
    Replies: 239
    Last Post: 12-04-2009, 08:08 AM
  3. Writing code on a Mac...reccomendations?
    By ninjadeathtoast in forum Software and Programming
    Replies: 9
    Last Post: 07-10-2008, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •