element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog Fast Track to Arduino Programming - Lesson 1, Reading from the Serial Port
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Robert Peter Oakes
  • Date Created: 9 Jun 2014 3:55 AM Date Created
  • Views 900 views
  • Likes 1 like
  • Comments 2 comments
  • fast-track
  • port
  • programming
  • fast_track_to_arduino_programming
  • tutorials
  • serial
  • arduino
Related
Recommended

Fast Track to Arduino Programming - Lesson 1, Reading from the Serial Port

Robert Peter Oakes
Robert Peter Oakes
9 Jun 2014

In this the first main lesson we look at how to simply read from the serial port and return what is read back out the port to the console, creating a buffer for the input data and correctly handling the input from the console

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

In the following code the CheckSerial() function has been corrected after the video was recorded. It will now be less dependent on the settings of your terminal program like Putty or the Serial Monitor of the IDE. this is due to the program now ignoring the '0D' or "\r" character often included in the data stream. All the sample code moving forward will be coded with this change

/*
Title: Lesson 1. Reading the Serial input a line at time

Description:  Reading a line of text from the Serial Port and sending to a command process function.
the processing function only echos back to the console for this lesson
This sketch also demonstrates the use of compiler directives to turn on and off blocks of functionality
Date created: 27 May 2014
Created By: Peter Oakes
Additional Comments:
Feel free to use and abuse as you wish, I imply NO Warranty
NOTE: defines take no program space untill used so use instead of ints etc when it will never change
Lessons Available
lesson 0. How big is an Empty Sketch anyway
Lesson 1. Reading the Serial input a line at time
Lesson 2. Parsing the line using  "if else if" statements
Lesson 3. Improved parsing and reduced SRAM consumption - "Case" Statements
lesson 4. Reading the Ethernet input a line at time (WEB Query)
lesson 5. Combining Serial and Ehernet into a common sketch
lesson 6. Advanced parsing of commands without "If" or "Case" statements
lesson 7. Adding input buttons to the command control
*/
// Bunch of constants in the form of definitions
// 1 = output debug to serial port, 0 = no debug
#define debug 1 
// define the buffer size... 
#define serialbufferSize 50 
// End of Constants
// Now the real varibles
char inputBuffer[serialbufferSize]   ; 
int serialIndex = 0; // keep track of where we are in the buffer
// End of real variables
void setup() 
{ 
  // initialize serial:
  Serial.begin(9600);
  // do other setup here as needed
}
void loop() 
{
  // Notice how the main loop is very simple and the functions 
  // seperate the logic into easily manageable parts
  if (CheckSerial()) DoCommand(inputBuffer); 
  // Do other stuff
}
boolean DoCommand(char * commandBuffer)
{
  Serial.println(commandBuffer);
#if debug
  Serial.print("Free Ram = "); Serial.println(freeRam(), DEC);
#endif  
return true;
}
/*
Checks the serial input for a string, returns true once a '\n' is seen
users can always look at the global variable "serialIndex" to see if characters have been received already
*/
boolean CheckSerial()
{
  boolean lineFound = false;
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    //Read a character as it comes in:
    //currently this will throw away anything after the buffer is full or the \n is detected
    char charBuffer = Serial.read(); 
      if (charBuffer == '\n') {
           inputBuffer[serialIndex] = 0; // terminate the string
           lineFound = (serialIndex > 0); // only good if we sent more than an empty line
           serialIndex=0; // reset for next line of data
         }
         else if(charBuffer == '\r') {
           // Just ignore the Carrage return, were only interested in new line
         }
         else if(serialIndex < serialbufferSize && lineFound == false) {
           /*Place the character in the string buffer:*/
           inputBuffer[serialIndex++] = charBuffer; // auto increment index
         }
  }// End of While
  return lineFound;
}// End of CheckSerial()
#if debug
// check free ram
int freeRam () 
{
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}
#endif

 

 

Back to Menu/Intro Page Fast Track to Arduino Programming

Attachments:
Arduino_Lesson_1___Reading_Strings_from_Serial_Port.ino.zip
  • Sign in to reply

Top Comments

  • Robert Peter Oakes
    Robert Peter Oakes over 9 years ago in reply to beacon_dave +1
    Setting the registers directly is not difficult and as the standard baud rates don't provide a setting for this speed, you can either change the library so they do or as you said, simply set the registers…
  • Robert Peter Oakes
    Robert Peter Oakes over 9 years ago in reply to beacon_dave

    Setting the registers directly is not difficult and as the standard baud rates don't provide a setting for this speed, you can either change the library so they do or as you said, simply set the registers directly...

     

    For the break, it is possible to simply set the port bit to an output temporarily and set it low for the required period, then return it to the serial module ?.  Changing the baud rate still requires you to send a whole character out the port + Start and stop bits so the whole character + control bits would need to be fabricated to achieve the 8uS. Still doable and on the face of it fairly simple. I would look into changing the port to I/O though as it does not require messing with the Serial Settings and may be simpler

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • beacon_dave
    beacon_dave over 9 years ago

    Perhaps one for a future lesson consideration, however have you ever done anything with the DMX 512 lighting protocol with the Arduino serial port ? There appear to be a number of workarounds for it on the net each claiming to be the right way but just wondering what your approach/opinion would be ?

     

    First issue is the 250000 baud, no parity and 2 stop bits which appears to require a workaround by setting up the USART registers directly.

     

    Second issue is the >88us long reset break prior to the start of data transmission which appears to require a workaround of temporarily changing the USART baud rate from 250000 baud down to 115200 baud, outputting a low, then changing the USART baud rate back to 250000 baud before sending the rest of the data >8us later.

    Arduino Playground - DMXSerial

    A Arduino Library for sending and receiving DMX

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube