LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-29-2009, 06:08 PM   #1
vinst
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.

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<termios.h>

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

if(fd<0)
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");

tcgetattr(fd,&nw);


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
if(res<0)
printf("%s","read failed\n");
else
for(i=0;i<=99;i++)
printf(":%c:%d\n",buff[i],res);

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

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
The state of your new struct termios, nw, is indeterminate. Try zeroing it before setting bits and bytes.
Code:
    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.
 
  


Reply


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


All times are GMT -5. The time now is 02:58 AM.

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