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.
string dev = "/dev/ACM0";
#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 estado = 0;
if (serial.IsOpen() == false)
estado = -1;
serial.Read(buffer, 10, 1000);
This is the code of Arduino class.
using namespace std;
using namespace LibSerial;
int main(int argc, char **argv)
And finally this is the main class.
Step 2: Program in Arduino
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.
Participated in the
4 years ago
To facilitate the connection with a PC, I recommend you to use a communication library such as
7 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
8 years ago on Introduction
I was able to test scm which is an alternative library to rxtx/javaxcomm for serial port communication. The arduino sent and received data correctly.
Wiki : http://www.embeddedunveiled.com/
Repository : https://github.com/RishiGupta12/serial-communicat...
8 years ago on Introduction
Why would one release the code with structure names not in English? That's a bad practice.