Simple Send/Receive (loopback)

posted May 17, 2014, 12:04 PM by Javad Taghia   [ updated May 17, 2014, 12:20 PM ]
How to setup serial connection



//============================================================================
// Name : HelloC++.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h> // Standard input/output definitions
#include <string.h> // String function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions

using namespace std;

int main()
{
int fd; /* File descriptor for the port */
int n;
int bytes;
char c;
char buffer[10];
char *bufptr;
struct termios options;
fd = open("/dev/ttyS10", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttn0 - ");
}
else {
fcntl(fd, F_SETFL, FNDELAY);
}
tcgetattr( fd, &options );
// set baud rate
cfsetispeed( &options, B9600 );
cfsetospeed( &options, B9600 );
//I don't know what this is exactly
options.c_cflag |= ( CLOCAL | CREAD );
// Set the Character size
options.c_cflag &= ~CSIZE; // Mask the character size bits
options.c_cflag |= CS8; // Select 8 data bits
// Set parity - No Parity (8N1)
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Disable hardware flowcontrol
/* options.c_cflag &= ~CNEW_RTSCTS; */ // not supported
// Enable raw Input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// Disable software flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
// Chose raw (not processed) output
options.c_oflag &= ~OPOST;
if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
printf ("Error with tcsetattr = %s\n", strerror ( errno ) );
else
printf ( "%s\n", "tcsetattr succeed" );
fcntl (fd, F_SETFL, 0);
// Write to the port
n = write (fd, "1", 1);
/*n = write (fd, "2", 10);*/
if (n < 0) {
fputs ("write () of 1 bytes failed!\n", stderr);
}
// Read from the port
//fcntl (fd, F_SETFL, FNDELAY);
bytes = read (fd, &buffer, sizeof (buffer));
printf ("number of bytes read is %d\n", bytes);
printf ("%s\n", buffer, "%d\n", sizeof (buffer));
//* perror ("read error:");*/
close(fd);
return 0;
}
Comments