Monday, August 4, 2008

Interfacing Arduino with C++ and libSerial.

I know that there is a section at the Arduino Playground on interfacing the Arduino with C. However, I found it is much easier, almost trivial, to use libSerial with C++.

(libSerial works on POSIX systems. I guess Windows users could use it through Cygwin but I'm not sure)
(Update: No, they can't)

First, you import your libraries and define the port you will be using:


#include < SerialStream.h >
#include < iostream >
#define PORT "/dev/ttyUSB0" //This is system-specific

SerialStream ardu;


using namespace std;
using namespace LibSerial;



The above code is assuming that the arduino is bound to /dev/ttyUSB0. Make sure to point the stream to the real device on you computer ;)

A new serial stream "ardu" is created.

Now let's define a simple function to setup the Arduino for communication.


void open()
{
ardu.Open(PORT);
/*The arduino must be setup to use the same baud rate*/
ardu.SetBaudRate(SerialStreamBuf::BAUD_9600);
ardu.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
}


At least in my case, this simple function is enough to have a working serial communication with the microcontroller.

Now I'll write another simple function that sends one byte through the serial port to the arduino and then reads a string, which gets interpreted as an integer..


int get(char out)
{
int res;
char str[SOME_BIG_SIZE];
ardu << out;
ardu >> str;
sscanf(str,"%d",&res);
return res;
}


See? It's easy! :)
If for some reason you must use standard POSIX libraries, here is a great article on POSIX serial programming.

If you have any doubts, post a comment.

4 comments:

LP said...

Hi, i´m trying to do some functions with libserial.

I tryed bufferinbg information:

serial_port >> input_buffer;

for (int i=0;i < BUFFER_SIZE;i++){
std::cout << input_buffer[i];
}

But with the information i have garbage? I think that the libserial have a function to read the data inside a vector and stop when data stops. Do you know something about how to use it?

information == > D V 1 6 4 0 0 5

D V 1 6 4 0 0 5 U � � ( L � � ( J � � � � � L . N = � � � "  � � @ U � � 8 � � � � � � � � � � � � � �  0 � � R � � � � � � � � X � �   � �   U � � � D �   � � �  � � � � � � � � r �  � �  H � � � � � � �  � � � � h � � W �  r �

Sergio said...

The extra garbage data is probably memory inside the char array which hasn't been initialized. The >> operator probably worked fine.
If you are always passing messages of 8 bytes, you could define BUFFER_SIZE to 8 to get rid of all those unused 0's and 1's.
If you want to see if the garbage is coming from the arduino, you could try filling the char array with '0's or whatever character you want, and then check if they are replaced.
You could also use the Read function of SerialPort. If you don't specify a timeout, I believe it will wait until BUFFER_SIZE bytes have been transfered.

LP said...

Hehe.. i´m not using a Arduino. It´s a barcode scanner. But i just found a few examples using libserial.

I´m initializing the array with 0 on all positions.

The garbage is from the barcode scanner because i configured a suffix(9 13 10) code that is sended after the garbage:

MK9590-61A47��������(��12 9 13 10

Dmeon said...

Hi! Exactly what does this script expect the arduino to send back? Because I can't get this to work. My program stops at "ardu >> input_buffer;", seemingly waiting for input.
The baudrate is set to 9600 on the device and in my code, so that's not it.

I have tried to do the same on Perl, and it works.

This is the line sending data back to the computer:
Serial.println(data, BYTE);
It's in a loop, so data is sent all the time.