I've always had trouble interpreting incoming serial messages on Arduino, so I'd like to get a good grasp of it with this project.
Here's an example line coming in over serial (python, eventually): th1 120.0@
th01 - identifier, to show which value it corresponds to
120.0 - example value (could be float, could be integer)
@ - end of line value (couldn't really figure out how to detect normal end of line characters)
Ideally I'd like to break the line up into it's respective components in order to compare them against my data model (eg. if (identifier == "th") { saveData(); })
How can I receive all the data into a proper buffer and be able to compare strings or integer/float values?
Interpreting an Arduino serial line
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Interpreting an Arduino serial line
You should be able to use the standard C library function sscanf. Here's a web page that popped up in google:
http://www.daniweb.com/software-develop ... ing-sscanf#
http://www.daniweb.com/software-develop ... ing-sscanf#
- wesg
- Posts: 118
- Joined: Thu Mar 18, 2010 10:00 pm
Re: Interpreting an Arduino serial line
Hey thanks, driver, that looks just like what I need. I'm already using printf, but had forgotten about scanf. So now my question is: how can I put all of the incoming characters into a single buffer that "looks" like a sentence?driverblock wrote:You should be able to use the standard C library function sscanf. Here's a web page that popped up in google:
http://www.daniweb.com/software-develop ... ing-sscanf#
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Interpreting an Arduino serial line
How are you reading it now? What you want to do is to write a loop that reads each character and adds it to a char* buffer. When you see the newline character, you parse the buffer with sscanf, reset the buffer pointer back to 0, and start over for the next sentence.
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: Interpreting an Arduino serial line
that would be a carriage return. you can compare each character in your incoming stream to the decimal value 13, to the hex value 0x0d, to the octal value 015 or to the character '\r'. the backslash is an escapement character and is necessary.wesg wrote:@ - end of line value (couldn't really figure out how to detect normal end of line characters)
Please be positive and constructive with your questions and comments.