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 |
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.
Thanks Tom |
---|---|
November 20, 2010 by tkopriva |
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):
|
November 21, 2010 by hevans (NerdKits Staff) |
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 |
Thanks Humberto, that helps a lot! Tom |
Please log in to post a reply.
Did you know that one NerdKits customer controlled a laser pointer with his computer using a microcontroller? Learn more...
|