Python script to drive an LED from the PC

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I am working on a simple project in which a python script will help the user interact with the MCU from the computer. The python script should drive an LED on and off depending on what the user writes and sends to the serial port.

However, I need the enter key to be automatic...
That means that the user would only have to write the word, and then he or she would just wait a few seconds until the program presses the enter key by itself.

I don't know very well this language, so how do I do that with python? :p

I am using the raw_input() function, so that's why I am asking about the enter key...
However, if you have any other suggestion, don't hesitate to share your idea!

Thanks in advance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You want to perform non-blocking input with a timeout. How you do that depends on what machine is hosting and running Python. I suspect it isn't an 8-bit AVR target :? which is the primary focus of this forum.

If you can tell me what machine and OS is running Python, I can point you in the right direction.

- John

Last Edited: Thu. Jul 8, 2010 - 10:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is, in fact it is an atmega168. The PC will send a character to the serial port (connected to the mcu) depending on what the user wrote (I've done this before). However, I don't want the user to press the enter key, but the program itself after a few seconds. How do I do this? :o

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the ATmega168 running Python or the PC? If the latter, which PC and operating system?

- John

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Atmega168 is of course, programmed in C. But it is connected to the PC (Windows 7) via RS232. So a python script I made sends some charactes to the MCU via the serial port, depending on what the user wrote in the python console API...

The problem is that the user has the press enter everytime the string is written, but I want the program to read the sentence after a few seconds without pressing the enter key.

Thanks for your time :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nvm, this module helped me a lot:

http://www.rutherfurd.net/python...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I believe I understand your problem, I just wanted to make sure I understood your question.

Under XP and Vista, you can call a function like this, say, every 100 ms to get each key:

import msvcrt

def get_char():
    """Return the next keyboard character.

    Return None if there's none available.
    """
    c = None
    if msvcrt.kbhit():
        c = msvcrt.getch()
    return c

You can then send on to the ATmega168 what you have collected and processed when the function hasn't returned anything for a few seconds. I'll leave the implementation of that as an exercise for the reader.

- John

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can read a single key from the keyboard in python using:

os.read(sys.stdin.fileno(), 1)

Perhaps that will work better than raw_input().

Bitlash (http://bitlash.net) comes with a Python serial to network proxy program named bitty.py which you can find here: http://bitlash.net/wiki/bitty.py -- it may be a useful reference for the Python part of your project.

Cheers,

-br
http://bitlash.net
http://entropymouse.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I like simple, one-liners, but would you have to run that as subprocess and later kill it to obtain the few second timeout (or set stdin not to block)?

The bitlash package looks handy!

- John

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[jflresto] Oops, yes, that is a blocking read from stdin. So it will wait for the keystroke. In bitty.py this happens in a separate thread.

It may not be the best solution for the OP because it waits for the key.

Thanks for your kind words about Bitlash!

-br
http://bitlash.net
http://entropymouse.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Something like this perhaps?

#Windows only
import serial, threading, time, SendKeys


def sendcr():
    time.sleep(10)
    SendKeys.SendKeys("~")

port = serial.Serial('COM11',9600)
t1 = threading.Thread(target=sendcr)
t1.start()

port.write(raw_input("write something: "))
port.close()
print 'done'