Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
#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;
//Initialize serial port
int initport(int fd)
int portstatus = 0;
struct termios options;
// Get the current options for the port...
// Set the baud rates to 115200...
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cc[VMIN] = 1; //min carachters to be read
options.c_cc[VTIME] = 10; //Time to wait for data (tenths of seconds)
// Set the new options for the port...
//tcsetattr(fd, TCSANOW, &options);
//Set the new options for the port...
if (tcsetattr(fd, TCSANOW, &options)==-1)
portstatus = -1;
portstatus = 1;
* 'open_port()' - Open serial port 1.
* Returns the file descriptor on success or -1 on error.
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (fd == -1)
* Could not open the port.
perror("open_port: Unable to open /dev/ttyS0 --- \n");
fcntl(fd, F_SETFL, 0);
int serial_fd = open_port();
if(serial_fd == -1)
printf("Error opening serial port /dev/ttyS0 \n");
printf("Serial Port /dev/ttyS0 is now open \n");
Post code within [code][/code] tags please. There's an option in Advanced mode to assist with that.
Check your ERRNO value after the call to open(). You likely need to be root. I usually only use O_RDWR and O_NONBLOCK. Also you might want to check and see what O_NDELAY is defined to be, it's possible that O_NONBLOCK equals O_NDELAY.