I am a complete novice in Arduino and moderately experienced with the language C++. What if I want to use the output from the serial monitor in C++ code in real time?

Also how can I write to the serial monitor. Which libraries are available for this? Also please give me links to sites where I can learn more about this topic. I am using Arduino Uno.

  • Which Operating System are you using? Oct 8, 2014 at 11:02
  • @PeterR.Bloomfield Windows 7 32 bit Oct 8, 2014 at 11:20
  • This question appears to be off-topic because it is mainly about C++, not Arduino. Oct 8, 2014 at 15:26
  • 2
    @TheGuywithTheHat - unfortunately on any other SO site the question would likely be instantly flushed here without a read for merely mentioning "Arduino". I would agree that the question pertains to embedded systems in general, but the poster may not know that - part of what answers may need to do is explain what the Arduino Serial Monitor is doing and that the solution is likely not to claim data from that, but rather get data from the same source it does. Oct 8, 2014 at 15:36
  • @ChrisStratton I would love to learn more about what you are saying. The problem with the internet is there it's too much information and funding something useful is difficult unless you know what you ate looking for... So if you can suggest things I can read to increase my knowledge about arduino or embedded systems in general please do so. Also I would like to learn more about the hardware side of things (For instance what it means to connect a device serially to the computer) and a little more about how it works. Please suggest good sources if possible. Oct 12, 2014 at 19:07

3 Answers 3


Unfortunately, C++ doesn't have native support for serial I/O.

You could use the Windows API directly if you're feeling adventurous. It can be a little complicated to get to grips with, but not insurmountable. You can get basic information about it here. There's an example of using it with Arduino here.

As an alternative, you could use a 3rd party library. This one looks good, although I haven't actually used it myself.

#include <windows.h>
#include <string>
#include <stdio.h>
#define ARDUINO_WAIT_TIME 2000
using namespace std;

string portNumber;
HANDLE hSerial;
bool connected;
COMSTAT status;

bool connectPrinter(char *portName){

    //Connects to the port.
    hSerial = CreateFileA(portName,

            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
            return false;
            //If port is in use by another program or did not closed previous process.
            printf("ERROR!!! \n");
            return false;

        DCB dcbSerialParams = {0};

        if (!GetCommState(hSerial, &dcbSerialParams))
            printf("failed to get current serial parameters!");
            return false;
            // Set Serial Port specifications.

             if(!SetCommState(hSerial, &dcbSerialParams))
                printf("ALERT: Could not set Serial Port parameters");
                return false;
                 connected = true;
                 printf("Connection Successful for :%s !!! \n" ,portName);
                 //Wait 2s as the arduino board will be reseting
                 return true;


int readData(char *buffer,unsigned int nbChar){

    DWORD bytesRead;
    DWORD dwCommModemStatus;

        //Set the type of data to be caught.(RXCHAR -> Data available on RX pin.)
        SetCommMask (hSerial, EV_RXCHAR );

        while (hSerial != INVALID_HANDLE_VALUE)
          // Wait for an event to occur for the port.
          WaitCommEvent (hSerial, &dwCommModemStatus, 0);

          if (dwCommModemStatus & EV_RXCHAR )
                //unsigned int toRead;
                //ClearCommError(this->hSerial, &this->errors, &this->status);
                  // Read the data from the serial port.
                    ReadFile (hSerial, buffer, 1, &bytesRead, 0);
                  // Display the data read.
                      printf ("%s" ,buffer);

                } while (bytesRead > 0);
    //If nothing has been read, or that an error was detected return -1
    return -1;

    int main()

        char* buffer = new char[32768];
        int nbChar= 32768;
        string str="COMx"; // Use your port number to connect

        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';

        delete []writable;
        return 0;

try this one, it should work. Comment below if it does not work. You might reset Arduino to read data in method void setup() in Arduino code. I recommend you to use C#, Java or Python for efficiency.

  • What about writing data... Also please upload arduino sketch if possible! Your answer is really good , sorry i cant upvote as it requires 15 rep! Nov 3, 2014 at 2:04

@Peter has already posted library links.

I recommend that you use Node.js (node-serialport) or Python (serial), which have excellent native serial support.

Both of these use C++ internally to communicate over Serial (Serial.h). Try going through the code Node.js uses internally, and if you are willing to write that much, by all means use the library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.