NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Listen for serial input
August 20, 2010 by met_fredrik |
I am using code similar to the one in the iphone project to send some info with python through serial to the nerdkit. However the numbers I am sending isn't getting through! I think it's something in my microcontroller code that's wrong, but I don't know what! Here's the code:
I didn't paste the includes etc. The code compiles just fine, but something has to be wrong! And the python script runs without any errors and outputs the right numbers in terminal. But here's the python code aswell:
Would be really happy if someone could look through my code! Thanks! :D |
---|---|
August 20, 2010 by hevans (NerdKits Staff) |
Hi met_fredrik, I think I see one problem with your code which is causing your issue. On line 26 of your code you have
and then you correctly use scanf to read the number into your kode variable.
The thing to note is that you are using the %d string format operation, which is going to look for an integer and then dump it into an integer variable. If you sent 4 at this point the kode variable is the number 4. Then you try to compare against this variable in an if statement but you put the 4 in single quotes.
The if statement will only be true if kode is equal to the character 4, not the integer 4; so this if statement is not true with the number 4 (try sending the number 52 over the serial port, see what happens). Try comparing against the number 4, and let us know if it works. Can't wait to see your key code door system in action! Humberto |
August 21, 2010 by met_fredrik |
Thanks for your quick answer! You pointed out one of the problems with my code. The other however, making the mcu not receiving the serial commands I haven't solved. So I switched some code, now using this instead:
The uart_read captures everything I send. But still I am on the variable problem. I did as you told me and got rid of the ' around the number. And it worked, however I want to work with larger numbers. And the numbers are sent one by one it seems. So if I try to read the tc variable as something larger than 9 it won't work. So what am I doing wrong here? :D
|
August 24, 2010 by mrobbins (NerdKits Staff) |
Hi met_fredrik, In your original scanf_P based serial reading, I am curious as to whether you ever write a newline character (or other whitespace) that tells scanf_P that you're done with a particular number. For example, in your Python code from your very first message, change:
to:
That way, the newline character will get sent, so scanf_P knows to stop reading characters. As per the scanf documentation, "Processing is aborted as soon as the data and format string no longer match, or there is an error or end-of-file condition on stream." Without having a newline or space or similar, the scanf_P will just see a stream of ASCII digits coming in, and may never exit. Mike |
Please log in to post a reply.
Did you know that spinning a permanent magnet motor makes a voltage? Learn more...
|