A Handy Serial Library


Arduino hadn’t debugging function, maybe this is the first impression of some software engineers. In fact, often we just use the Arduino to do some simple application, even there’s no debugging, and it does not matter. If you want to use the Arduino to do some complex application, such as face recognition, then maybe you have to rethink if you had chosen the wrong platform.
As for some simple application, we can use serial print to debug, and Arduino had provided a very easy to use serial print function.

void setup()
{
    Serial.begin(115200);
    
    Serial.println("hello world");
}

void loop()
{
    // add code here
}

Besides, there is Serial.print, Serial.write and so on. When you are familiar with these functions, you will find that these function is not so friendly actually, just have a look at the following code:

void setup()
{
    Serial.begin(115200);
    
    Serial.print("a[");
    Serial.print(3);
    Serial.print("] = ");
    Serial.println(5);
    
}

void loop()
{
    // add code here
}

To print a[3]=5, it can take 4 lines of code, troublesom? Remember the C language lessons, it takes only one line of code:

printf("a[%d] = %d", 3, 5);

As for C++, one line is enough also:

cout << "a[" << 3 << "] = " << 5 << endl;

I will glad that if Arduino has the function such as printf or cout, it’s really convenient. It’s lucky that some guy had written such library, you can refer to http://arduiniana.org/libraries/streaming/ I made some small change to this library, you can download here: https://github.com/loovee/Streaming , why not have a try?



Resources

download pdf file

Copyright (c) 2008-2016 Seeed Development Limited (www.seeedstudio.com / www.seeed.cc)
This static html page was created from http://www.seeedstudio.com/wiki