Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-29-2009, 07:08 PM   #1
LQ Newbie
Registered: May 2009
Posts: 5

Rep: Reputation: 0
unable to read from serial port

I have been trying to communicate with device trhough /dev/ttyS0.
The device sends out data as soon as it powers up but the read just gets stuck and I cant proceed any further. After the initial startup data from the device, commands can be sent to it to control it. I can send one command to it successfully but after that I cant control the device.

Could the serial port be blocked by the kernel for some reason or is my code itself wrong. I have posted the code below and I would be grateful for any help regarding this.


int main()
int fd=-1,res,n,i;
char buff[100];
struct termios ol;
struct termios nw;

fd=open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);//opening port
fcntl(fd,F_SETFL,0);//returning control

printf("%s","error opening port\n\n");
tcgetattr(fd,&ol); //getting serial port attributes

nw.c_cflag |=B115200; //baud rate
nw.c_cflag |=(CLOCAL|CREAD);
nw.c_cflag &=~PARENB;
nw.c_cflag &=~CSTOPB;//8N1 settings
nw.c_cflag &=~CSIZE;
nw.c_cflag |=CS8;
nw.c_lflag |=~(ICANON | ECHO | ECHOE | ISIG);

if( tcsetattr(fd,TCSANOW,&nw)<0)
printf("%s","error in setting up device\n\n");


printf("%s","trying to read");

bytes=fcntl(fd,F_SETFL,FNDELAY); //to avoid serial port blocking
res=read(fd,buff,100); //trying to read initial setup data-not working
printf("%s","read failed\n");

n=write(fd,"debug d 35 5 0\r",1000); //writing command to WSS
printf("%s","write failed");
return 0;
Old 05-31-2009, 01:57 AM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The state of your new struct termios, nw, is indeterminate. Try zeroing it before setting bits and bytes.
    bzero(&nw, sizeof(nw));
You probably want to set VMIN & VTIME in the termios, since you are using raw mode. See the Serial Programming Howto, especially the section named 'Non-Canonical Input Processing'. It is hard to predict the right settings for your application without knowing more about the data. The essential problem, as I see it, is that you are going to have to read iteratively, and accumulate and/or parse data as it arrives, since raw mode input cannot know how to break up the input stream in a way that is suitable for your application.
--- rod.


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
read serial port fails willydlw Linux - Newbie 1 04-08-2009 02:46 PM
Difficult to read serial port omwoyo Programming 3 07-03-2007 07:47 AM
When to read from the serial port? kad_79 Programming 3 06-15-2006 03:39 PM
Problem with Non-blocking serial port read ZX_SA Programming 0 01-18-2005 09:50 AM
Serial port Read Write SeanatIL Programming 2 07-14-2004 04:42 PM > Forums > Non-*NIX Forums > Programming

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