(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.
10 comments:
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 �
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.
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
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.
tienes algún programa ejemplo que comunique tanto arduino con un programa en c++ con libserial, es que he estado usando tu ejemplo y no me funciona, osea, el arduino esta enviando perfectamente la información porque si hago cat /dev/ttyUSB0 me aparece la info pero al parecer libserial no la esta leyendo y no me da errores ni nada parecido :s
también viceversa, si quiero enviarle algo al arduino no está funcionando pero si uso echo algo >> /dev/ttyUSB0 funciona perfectamente.
hi. ;) i trying to write a code that read arduino output in usb.
I work on mac os x and i not able to install libserial on my mac. can you help me?
thanks
i install succesfully libserial but when i compile my progrma obtain follow errors:
Undefined symbols:
"LibSerial::SerialStreamBuf::underflow()", referenced from:
vtable for LibSerial::SerialStreamBufin ccQlD1bA.o
"LibSerial::SerialStreamBuf::overflow(int)", referenced from:
vtable for LibSerial::SerialStreamBufin ccQlD1bA.o
"LibSerial::SerialStream::SetCharSize(LibSerial::SerialStreamBuf::CharSizeEnum)", referenced from:
_main in ccQlD1bA.o
"LibSerial::SerialStream::SetBaudRate(LibSerial::SerialStreamBuf::BaudRateEnum)", referenced from:
_main in ccQlD1bA.o
"LibSerial::SerialStreamBuf::xsgetn(char*, int)", referenced from:
vtable for LibSerial::SerialStreamBufin ccQlD1bA.o
"LibSerial::SerialStreamBuf::xsputn(char const*, int)", referenced from:
vtable for LibSerial::SerialStreamBufin ccQlD1bA.o
"LibSerial::SerialStream::Open(std::basic_string, std::allocator >, std::_Ios_Openmode)", referenced from:
_main in ccQlD1bA.o
"LibSerial::SerialStreamBuf::showmanyc()", referenced from:
vtable for LibSerial::SerialStreamBufin ccQlD1bA.o
"LibSerial::SerialStreamBuf::pbackfail(int)", referenced from:
vtable for LibSerial::SerialStreamBufin ccQlD1bA.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
for compile i use command : g++ file.cpp
file.cpp has follow code :
#include
#include
#define PORT "/dev/tty.usbserial-A8003LjH" //This is system-specific
using namespace std;
using namespace LibSerial;
int main(){
SerialStream ardu;
ardu.Open(PORT);
ardu.SetBaudRate(SerialStreamBuf::BAUD_9600);
ardu.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
int res;
char str[8];
ardu >> str;
sscanf(str,"%d",&res);
return 0;
}
Hey Marco. You need to link libserial to your program. In linux I would change your compile command to
g++ file.cpp -lserial
Maybe that will work in MacOS. If not, you'll have to figure out how to link it.
Oscar: Si quieres te mando el código por mail
estaría excelente mi correo es odavilar en gmail.com
Marco, aquí recordandote a ver si podrías enviarme el código que utilizaste, muchas gracias.
Post a Comment