NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Basic Electronics » Python questions

November 20, 2010
by tkopriva
tkopriva's Avatar

I am using python to log measurements from the temperature sensor over the COM port (a addition to the first project). I want to take measurements for a undefined amount of time, the problem is that I am stuck in the while loop and unable to close the text file using my current code. Is there a way to use a break statement to exit the while loop (for example if I hit the escape key on the keyboard)? Or maybe a different way to do this altogether?

Also is there a serial function that takes a line of code versus a number of bits? I am also sending a time in seconds over the com line and as the number become 10 to 100 etc the number of bits I will no longer get a nicely formatted text file.

    #!/usr/bin/env python

    #import module
    import serial

    #communicate with serial port
    s = serial.Serial('COM3', 115200)

    #write to text file
    f = open('/Python27/temperature Readings/temperature.txt', 'w')
    x = 1
    while x == 1:
        f.write(s.read(13))
    f.close()

Thanks Tom

November 20, 2010
by tkopriva
tkopriva's Avatar

I found a work around for my second question, however I am still stuck on the infinite while loop dilemma....

In my C program I specified the number of the number of digits before the decimal place to compensate for the longest time I may need (about a day):

printf_P(PSTR("%.1f %7.1f\n"), temp_avg, (int32_t) the_time / 100.0);
November 21, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi tkopriva,

The serial module has a readline() method that will read characters until it encounters a newline character. That might be what you want to use instead of having a specific number of bytes read. You might need to specifically append another newline character to the line in your python code, because I think it would get stripped on the readline() call, but I'm not sure.

Your second problem is a little trickier. Any calls you make to get keyboard input from the command line are going to be blocking calls, so that isn't really an option. One solution is to just not worry about it, you can exit the Python program with Ctrl+D and any files it had opened will get closed. It just might close in a bad state with half a line written out. A second more elegant solution is to have a button on the MCU side that will cause your C code to send a special message to the python code. Python can check for this and break out of the while loop and properly close the file. If you really want to do this from the python side you can use a GUI library like PyGame. This will let you check for keypresses in a non-blocking manner and you can just exit out of the loop when a specific key is pressed. Hope that helps.

Humberto

November 21, 2010
by tkopriva
tkopriva's Avatar

Thanks Humberto, that helps a lot!

Tom

Post a Reply

Please log in to post a reply.

Did you know that you can aim and fire a servo-controlled water gun from your computer? Learn more...