Share your knowledge at the LQ Wiki.
Go Back > Forums > Enterprise Linux Forums > Linux - Enterprise
User Name
Linux - Enterprise This forum is for all items relating to using Linux in the Enterprise.


  Search this Thread
Old 10-12-2011, 05:28 PM   #1
LQ Newbie
Registered: Oct 2011
Posts: 5

Rep: Reputation: Disabled
Unable to read data from Serial port

I am new to the linux.I have written a program in c/C++ to communicate with a device through serial port.This program is not able to read data from serial port.
I tried to connect the device using minicom and it seems to working fine.

Here is the program:-

#include <stdio.h>

#include <iostream>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
using namespace std;
int fd1;
int fd2;
string string_f;
//char *buff,*buffer,*bufptr;
char *buff;
int wr,rd,nbytes,tries;
const int MAXDATASIZE = 512;
// std::vector<char> m_vRequestBuf;
char buffer[MAXDATASIZE];
int totalBytes = 0;
//struct termios port_settings;
int configure_port(int fd);
int main()
fd1=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1 )
perror("open_port: Unable to open /dev/ttyS0 – ");
fcntl(fd1, F_SETFL,0);
printf("Port 1 has been successfully opened and %d is the file description\n",fd1);
fd1 = configure_port(fd1);
printf("Port Configured \n");// apply the settings to the port
printf("Start Writing... \n");
wr = write(fd1,"SFC\r",4);
if (wr < 0)
printf("no bytes written \n");
printf("bytes written %d \n" ,wr );
fcntl(fd1, F_SETFL, FNDELAY);
for(int i=0;i<10;i++)
if(rd > 0)
string_f+= buffer;
cout << "charsRead" << rd << endl;
//Sleep for some time and read again

return 0;
int configure_port(int fd) // configure the port
struct termios port_settings; // structure to store the port settings in
tcgetattr(fd, &port_settings);
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
port_settings.c_cflag &= ~CRTSCTS;
port_settings.c_cflag |= (CLOCAL | CREAD);
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &port_settings);
} //configure_port

Last edited by tanu1985; 10-12-2011 at 05:30 PM.
Old 10-12-2011, 05:44 PM   #2
harry edwards
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
What result do you get from running the program? Also, have you tried flushing the port after the write to ensure the data is sent, as I've have problems with data being held in local data buffers until the port if flushed?
Old 10-12-2011, 06:05 PM   #3
LQ Newbie
Registered: Oct 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
I increased the sleep time and now it's working fine.


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
the get data from serial port function read() blocks when data not available DEF. Programming 3 11-17-2014 08:11 AM
[SOLVED] Serial port : Read data problem, not reading complete data anujmehta Linux - Networking 5 09-06-2010 07:10 AM
unable to read from serial port vinst Programming 1 05-31-2009 01:57 AM
Minicom -- want to read data coming in a serial port ihopeto Linux - Newbie 2 04-12-2009 10:46 PM
Why can't I read in data from the serial port using a bash script? tjt Linux - Newbie 1 06-17-2004 01:21 AM > Forums > Enterprise Linux Forums > Linux - Enterprise

All times are GMT -5. The time now is 04:35 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