How to Connect Arduino to a PC Through the Serial Port




Introduction: How to Connect Arduino to a PC Through the Serial Port

To do that I have used a computer with Ubuntu 12.04 and the program language C++, but if you want to used Windows the code works too, only have to change the port used to conect with arduino, but this is explain in the next step.
This program is only the first version so it must be improved.

Arduino use the usb port to simulate a serial port so we have to use a usb cable to connect the arduino usb port to computer usb port.

Step 1: Program in C++

This is the code of C++, I have created a main class and a Arduino class, so this is object oriented.

#ifndef ARDUINO_H
#define ARDUINO_H

#include <SerialStream.h>
#include <SerialStreamBuf.h>
#include <SerialPort.h>
#include <string>

class Arduino{
    int open);
    DataBuffer read();
    void close();
    string dev = "/dev/ACM0";
    SerialPort serial;

#endif // ARDUINO_H

This is the header of Arduino class.
There are three functions open, read and close.
Open: Open the conection bewteen arduino and the computer.
Read: Read the bufer where is all dates that arduino has send to the computer.
Close: Close the conection bewteen arduino and the computer.

To connect with arduino I have used the port of my computer "/dev/ACM0", if you use Windows instead of Linux you have to use the port "COM1" or "COM2". But to see what port is using arduino you have to use the JDK of arduino and select a port in "Tools -> Serial Port".

# include <Arduino.h>


int Arduino::abrir(){
    int estado = 0;


    if (serial.IsOpen() == false)
        estado = -1;

    return estado;

void Arduino::cerrar(){

DataBuffer Arduino::leer(){
    SerialPort::DataBuffer buffer;
    serial.Read(buffer, 10, 1000);

    return buffer;

This is the code of Arduino class.

#include <iostream>
#include <SerialStream.h>
#include <SerialStreamBuf.h>
#include <SerialPort.h>
#include <string>

using namespace std;
using namespace LibSerial;

int main(int argc, char **argv)
    Arduino arduino();
    return 0;

And finally this is the main class.

Step 2: Program in Arduino

void setup(){

void loop(){
  Serial.println("Hello world");

This is the code that you have to do in arduino.
This code is very simple.
"Serial.begin(9600)" : Sets the velocity of dates in bits per second (baud).
Serial.println("Hello world") : Send a message through the serial port.
delay(1000) : Stop by 1000 miliseconds.

Microcontroller Contest

Participated in the
Microcontroller Contest

Be the First to Share


    • Envision the Future of the Lift Truck Student Design Contest

      Envision the Future of the Lift Truck Student Design Contest
    • Sandwich Speed Challenge

      Sandwich Speed Challenge
    • Make It Modular: Student Design Challenge

      Make It Modular: Student Design Challenge


    Amir AliO
    Amir AliO

    6 years ago

    Hi, I'm trying to run your code on my mac to send my c++ robot commands to my Arduino Uno that controls my robot motor . So the only thing I changed was the port name, mine is "/dev/cu.usbmodem1421." But the problem is my xcode does not have any of these Serial libraries you used. I tried to add SerialLib but it didn't work. Since I'm new to Arduino and c++ can you show me how to add the Serial libraries. One more thing after I got it to work do I need to use serial monitor to see if the code's working.

    Thanks in advance


    7 years ago on Introduction

    Why would one release the code with structure names not in English? That's a bad practice.