is not very difficult to communicate our Arduino through serial port with C + + using the library LibSerial. This library has a class SerialStream
, we abstracted from the "complexity" of the structure termios. To use it you need to install it. Ubuntu can be found in the repositories. If not, here's the link: http://sourceforge.net/projects/libserial/files/ Installing the library (in Ubuntu)
$ sudo apt-get install libserial -dev LibSerial API The following URL, the API is well documented.
http://libserial.sourceforge.net/doxygen Here's how to do basic operations (from opening the port to read / write). Open port _________________________________________ # include \u0026lt;SerialStream.h>
SerialStream serial_port ("/ dev/ttyUSB0"); _________________________________________
baud Assignment _________________________________________ serial_port.SetBaudRate (SerialStreamBuf:: BAUD_9600)
_________________________________________
The example is for the case of 9600 baud (usual). But assign other (see this link ).
number of bits to transmit
is made with the method call SetCharSize ()
. _________________________________________
serial_port.SetCharSize (SerialStreamBuf:: CHAR_SIZE_8) _________________________________________ CHAR_SIZE_X, X may vary between 5 and 8 reads and writes / / Write (send a fact)
char c; cin>> c;
serial_port \u0026lt;\u0026lt;c; / / Reading: receive data char c ; serial_port>> c;
Complete Example
/ *
Author: Juan Jose Fumero Alfonso
Date: Fri May 19 July : 48:06 WEST 2010 Note: To compile install is necesary libserial: Serial port
programming in C++ under POSIX operating system $ sudo apt-get install libserial-dev
Doc : http://libserial.sourceforge.net/doxygen
Compile:
#include <string>
#include <unistd.h>
std;
using
int
main (
int
argc,
char **argv) {
oc;
string dev;
bool
dsp = false ;
while ((oc = getopt(argc, argv,
":d:"
)) != - 1
) {
switch (oc) { case
'd' :
//device
cout <<
"Dispositivo: "
; dev = optarg;
cout <<
" :: "
<< dev << endl; dsp =
true
; ;
case ':'
:
cerr << "Missing argument "
<< endl;
return - 1
; break
; }
}
if (!dsp) {
dev = "/dev/ttyUSB0"
; }
SerialStream serial_port (dev);
serial_port.SetBaudRate (SerialStreamBuf: BAUD_9600);
serial_port.SetCharSize (SerialStreamBuf: CHAR_SIZE_8);
char num;
while ( 1)
{court \u0026lt;\u0026lt; "Number of blinks?" ;
cin>> num; serial_port \u0026lt;\u0026lt;num; } return
0;}
To test, you can blink a typical example (by connecting an LED to pin 13) and send a number between 0 and 9 indicating the number of flashes to be made by LED on the Arduino. The corresponding pde code is as follows: (taken from
http://todbot.com/ , although it is no big deal). / *
* Read Serial Blink * ----------------- * Turns on and off to light emitting diode (LED) connected to digital
* pin 13. The LED will blink the number of times given by a
* single-digit ASCII number read from the serial port.
* * Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com>
* http://todbot.com/
*
* based on "serial_read_advanced" example
*/
int ledPin = 13 ;
// select the pin for the LED int val =
0 ; // variable to store the data from the serial port
void setup() {
pinMode(ledPin,OUTPUT);
// declare the LED's pin as output
Serial.begin( 9600 );
// connect to the serial port }
void loop () {
val = Serial.read();
// read the serial port
// if the stored value is a single-digit number, blink the LED that number
if (val > '0' && val <=
'9' ) { val = val -
'0'
;
// convert from character to number
for ( int i=
0 ; i<val; i++)
{digitalWrite (ledPin, HIGH);
delay (150
)
digitalWrite (ledPin, LOW); delay (150
);}
}}
This Class ( SerialStream ) has more methods to establish stop bits, parity check, etc.. You can see the class documentation.
More
http://libserial.sourceforge.net/
0 comments:
Post a Comment