Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This 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!


  Search this Thread
Old 11-17-2014, 06:48 AM   #1
LQ Newbie
Registered: Jul 2014
Posts: 7

Rep: Reputation: Disabled
serail port read function blocked

#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 */
#include <string.h>
#include <string>
using namespace std;

//Initialize serial port
int initport(int fd)
int portstatus = 0;

struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 115200...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag = CS8;
//options.c_cflag |= SerialDataBitsInterp(8); /* CS8 - Selects 8 data bits */
options.c_cflag &= ~CRTSCTS; // disable hardware flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable XON XOFF (for transmit and receive)
//options.c_cflag |= CRTSCTS; /* enable hardware flow control */

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...
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &options)==-1)
perror("On tcsetattr:");
portstatus = -1;
portstatus = 1;

return portstatus;

* 'open_port()' - Open serial port 1.
* Returns the file descriptor on success or -1 on error.
int open_port(void)
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);

return (fd);

int main(void)

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");

if(initport(serial_fd) == -1)
printf("Error Initializing port");
return 0;

//printf("size of data being sent = %ld", sizeof("~ver~\n\r"));
unsigned char linkpack[2];
string str = "@R5-ON#";
char str1[8] = {'@','R','6','-','O','N','#'};
linkpack[0] = '@';
linkpack[1] = 'T';
linkpack[2] = '5';
linkpack[3] = '-';
linkpack[4] = 'O';
linkpack[5] = 'N';
linkpack[6] = '#';
//linkpack[7] = '@';
int n;
//write(serial_fd,str1, 8);
for(int i=0; i <8; i++)
n = write(serial_fd,&linkpack[i], 1);
printf("hell ");

if (n < 0)
fputs("write() of 8 bytes failed!\n", stderr);
printf("Successfully wrote 8 bytes\n");

char buffer[32];

int n = read(serial_fd, buffer, sizeof(buffer));
if (n < 0)
fputs("read failed!\n", stderr);
printf("Successfully read from serial port -- %s\n", buffer);




printf("\n\nNow closing Serial Port /dev/ttyS0 \n\n");


return 0;
Old 11-17-2014, 12:10 PM   #2
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,833
Blog Entries: 10

Rep: Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error copy const char to string when reading serail port amitbpl5 Programming 5 09-23-2014 03:49 AM
Read System Call is getting blocked when tried to read the data from CDC device sanju.lnt Linux - Embedded & Single-board computer 0 09-11-2011 11:48 PM
SFTP server port blocked on Uni network, need to change the listening port. sammyboy161 Linux - Newbie 2 10-21-2010 12:03 PM
USB to serail port adaptor COM port number areftaidi Linux - Software 2 09-25-2007 01:05 AM

All times are GMT -5. The time now is 08:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration