Interpreting an Arduino serial line

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Interpreting an Arduino serial line

Postby wesg » 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 / MONOCHRON clock
User avatar
wesg
 
Posts: 66
Joined: Thu Mar 18, 2010 9:00 pm
Location: Toronto, Ontario

Re: Interpreting an Arduino serial line

Postby adafruit_support_rick » 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: 2913
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Interpreting an Arduino serial line

Postby wesg » 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 / MONOCHRON clock
User avatar
wesg
 
Posts: 66
Joined: Thu Mar 18, 2010 9:00 pm
Location: Toronto, Ontario

Re: Interpreting an Arduino serial line

Postby adafruit_support_rick » 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: 2913
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Interpreting an Arduino serial line

Postby mtbf0 » 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: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca


Return to Microcontrollers

Who is online

Users browsing this forum: No registered users and 2 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [108]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[31]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[34]
LCDs & Displays[48]
Components & Parts[70]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[111]
 
Wireless[14]
Cables[62]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]