element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Arduino | Tutorial 3 | Serial Communication
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: oksbwn
  • Date Created: 2 Jun 2016 6:00 AM Date Created
  • Views 2437 views
  • Likes 2 likes
  • Comments 12 comments
  • ardbeginner
  • arduino_tutorials
  • arduino_tutorial
  • arduino
  • serial communicat
  • arduino serial programming.
Related
Recommended

Arduino | Tutorial 3 | Serial Communication

oksbwn
oksbwn
2 Jun 2016

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

  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps +1
    Jan Cumps Apparently, a reference to Serial in an if or while argument context evaluates to true if the specified serial port is available else there is a permanent/temporary error. There must be something…
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps +1
    Ok, Jan Cumps earned the "King for the Day" title. I've been busy cleaning up my own mess (one of those "no good deed goes unpunished" stories). King of which country? How about the USA? Stay longer. The…
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps +1
    Kings don't need a freakin' nomination. Just show up with your 3 dragons and legions of unsullied.
Parents
  • Jan Cumps
    Jan Cumps over 9 years ago

    Are you sure that the code

     

    if (Serial) {
      // communicate
    }

     

    makes sense in the video? Isn't Serial always different from 0, because it's a static object that has a non-zero address? even if Serial.begin(9600) fails?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps

    Jan Cumps  Apparently, a reference to Serial in an if or while argument context evaluates to true if the specified serial port is available else there is a permanent/temporary error.  There must be something hidden in the compilation/interpreter that treats Serial special.  An if( Serial.isConnected() ) would be better.  Secrets are bad engineering practices but what can you do?  I cannot find any documentation of the static Serial object other than what's in the arduino.cc API.  Have you?

     

    To guard against a single spurious failure instance, I use this in setup() instead of the if-statement for Serial applications:

     

    int count_down = 3; 
    while( ! Serial ) {
          // Sleep 1 second if not yet ready.
          delay( 1000 );
          // Tried the maximum allowed times already?
          if( count_down-- < 1 ) {
               // Tried 3 seconds to get the serial connection ready - giving up.
               // Timeout error processing goes here
          }
    }
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 9 years ago in reply to Former Member

    The compiler is a standard c++ compiler and the linker is also a known one. So whatever the Serial library does must be a c++ construct.

     

    The source for the System class is open, so we can have a look ourselves. Let's do that.

    The first who finds it is king for a day...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 9 years ago in reply to Former Member

    I have been digging in the source code.

     

    The files are available on github

     

    The one we're talking about is:

    https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.h

     

    Relevant pieces of code

     

    class HardwareSerial : public Stream
    {
    // ...
      public:
        operator bool() { return true; }
    // ...
    }
    
    // ... 
      extern HardwareSerial Serial;

     

     

    So it looks like the bool operator is indeed overridden for the class HardwareSerial (also checked SoftwareSerial, it's the same mechanism there), but it always returns true, whatever the state of the connection

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps

    See the SerialEvent example at https://www.arduino.cc/en/Tutorial/SerialEvent

    Note that there are no include statements either for HardwareSerial.h nor SoftwareSerial.h.  In fact, there are no include statements at all.

    So, how is it that this "magical" Serial object is accounted for?

    There is not even an instantiation nor a constructor invoked by the user.

     

    It still looks to me that the Arduino IDE is adding something to the sketch before submitting it for C++ compilation.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 9 years ago in reply to Former Member

    I looked somewhat further:

     

    (this is true for boards where the value USBCON is defined, it doesn't work if  HardwareSerial is used - they always return true. For those boards the object is created in source file https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial0.cpp - in the end it's the board that defines if you're using HardwareSerial or Serial_)

     

    the object named Serial  is declared in CDC.cpp

    https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/CDC.cpp

     

    Serial_ Serial;

    You can see that the class is Serial_ not HardwareSerial

     

    And that class has the bool operator overloaded in a proper way

    Serial_::operator bool() {
      bool result = false;
      if (_usbLineInfo.lineState > 0) 
        result = true;
      delay(10);
      return result;
    }

     

    And that means that

    if (Serial) 

    returns true if the line state is active, and false if the line hasn't been established.

    So oksbwn is correct when he writes

     

    if (Serial) {
      // actions
    }

     

    No magic. c++ and lots of digging into source.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Jan Cumps
    Jan Cumps over 9 years ago in reply to Former Member

    I looked somewhat further:

     

    (this is true for boards where the value USBCON is defined, it doesn't work if  HardwareSerial is used - they always return true. For those boards the object is created in source file https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial0.cpp - in the end it's the board that defines if you're using HardwareSerial or Serial_)

     

    the object named Serial  is declared in CDC.cpp

    https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/CDC.cpp

     

    Serial_ Serial;

    You can see that the class is Serial_ not HardwareSerial

     

    And that class has the bool operator overloaded in a proper way

    Serial_::operator bool() {
      bool result = false;
      if (_usbLineInfo.lineState > 0) 
        result = true;
      delay(10);
      return result;
    }

     

    And that means that

    if (Serial) 

    returns true if the line state is active, and false if the line hasn't been established.

    So oksbwn is correct when he writes

     

    if (Serial) {
      // actions
    }

     

    No magic. c++ and lots of digging into source.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps

    Ok, Jan Cumps  earned the "King for the Day" title.  I've been busy cleaning up my own mess (one of those "no good deed goes unpunished" stories). image

     

    King of which country?  How about the USA?  Stay longer.  The American Congress is useless so we might as well have a King.  And, you'd probably do a lot better than the 2 individuals currently running President in any case!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 9 years ago in reply to Former Member

    image When do the nominations officially close?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps

    Also, as https://www.arduino.cc/en/Reference/Serial stated,

     

    "The Arduino Leonardo board uses Serial1 to communicate via TTL (5V) serial on pins 0 (RX) and 1 (TX). Serial is reserved for USB CDC communication. For more information, refer to the Leonardo getting started page and hardware page."

     

    I think that the same is true for all models.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps

    Kings don't need a freakin' nomination.  Just show up with your 3 dragons and legions of unsullied.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to Jan Cumps

    An interesting web page of details concerning Serial physical implementation and USB virtual serial devices (): https://www.arduino.cc/en/Guide/ArduinoLeonardoMicro

    That page covers the Arduino/Genuino Uno too.

    • Cancel
    • Vote Up +1 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