Interpreting an Arduino serial line
Moderators: adafruit_support_bill, adafruit

Interpreting an Arduino serial line

by wesg on Tue Jul 31, 2012 11:40 pm

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?
http://www.wesg.ca - Macs and more, including Arduinos!
MacBook Pro 13" - Mountain Lion
Ubuntu Server - Nimitz - 9.25TB
4.0 GHz i7-950 watercooled workstation
Arduino / Arduino MEGA / Arduino Pro MINI / Raspberry Pi
User avatar
wesg
 
Posts: 85
Joined: Thu Mar 18, 2010 9:00 pm
Location: Toronto, Ontario

Re: Interpreting an Arduino serial line

by adafruit_support_rick on Wed Aug 01, 2012 7:12 am

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 ... ng-sscanf#
User avatar
adafruit_support_rick
 
Posts: 12172
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Interpreting an Arduino serial line

by wesg on Wed Aug 01, 2012 9:22 am

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 ... ng-sscanf#


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?
http://www.wesg.ca - Macs and more, including Arduinos!
MacBook Pro 13" - Mountain Lion
Ubuntu Server - Nimitz - 9.25TB
4.0 GHz i7-950 watercooled workstation
Arduino / Arduino MEGA / Arduino Pro MINI / Raspberry Pi
User avatar
wesg
 
Posts: 85
Joined: Thu Mar 18, 2010 9:00 pm
Location: Toronto, Ontario

Re: Interpreting an Arduino serial line

by adafruit_support_rick on Wed Aug 01, 2012 10:16 am

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.
User avatar
adafruit_support_rick
 
Posts: 12172
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Interpreting an Arduino serial line

by mtbf0 on Wed Aug 01, 2012 7:33 pm

wesg wrote:@ - end of line value (couldn't really figure out how to detect normal end of line characters)


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.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca