LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices


Reply
  Search this Thread
Old 02-15-2010, 12:39 AM   #1
vijayp
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Rep: Reputation: 0
delay serial read function when there is no data reception


Hi,

I have an issue in reading serial port.
The read function takes very less execution time when the data is received, which is appr 80 us.
However, when there is no data on the bus the same function takes a long time which is around 220 ms before it returns.
I monitor the execution time on oscilloscope by toggling some IO line of my PC.
Can someone help me out please.

I have not set any timeouts or enabled blocking mode in my port initialization. Please find below the code for initialization and read function .
/********************************************/
int initport(int fd) {
struct termios options,oldtio;

fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd, F_SETFL, 0);
}
// save current port settings
tcgetattr(fd,&oldtio);

//Get the current options for the port...
tcgetattr(fd, &options);

// Set the baud rates to 9600...
long baudRate = B9600; //Please set baud rate as per you settings
cfsetispeed(&options, baudRate);
cfsetospeed(&options, baudRate);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
//options.c_oflag &= OPOST;

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; // No output processing
options.c_oflag &= ~ONLCR; // Don't convert linefeeds

options.c_lflag = 0; // no local flags
options.c_cflag |= HUPCL; // Drop DTR on close

// Clear line
tcflush(fd,TCIFLUSH);

// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
return 1;
}
/**********************************************************/
/**********************************************************/
int readport(int fd, unsigned char *result) {
int iIn = read(fd, result, 64);
result[iIn] = 0x00;
if (iIn < 0) {
if (errno == EAGAIN) {
printf("SERIAL EAGAIN ERROR\n");
printf("SERIAL read error %d %s\n", errno,
strerror(errno));
return 0;
} else {
printf("SERIAL read error %d %s\n", errno,
strerror(errno));
return 0;
}
}
return 1;
}
/**********************************************************/
/**********************************************************/
int serialcomm(unsigned char *result1, char sCmd[]) {

if (!writeport(fd, sCmd)) {
printf("write failed\n");
close(fd);
return 1;
}
usleep(SLPDLY);
fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
if (!readport(fd,result1)) {
printf("read failed\n");
return 1;
}
printf("readport=%s\n", result1);
return 0;
}
 
Old 02-21-2010, 06:05 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You have not explicitly set VMIN & VTIME. Try setting them both to zero. The granularity of VTIME is 100ms.
--- rod.
 
Old 02-22-2010, 05:53 AM   #3
vijayp
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks a lot.
This was consuming a lot of CPU load as I was doing serial read/write in 200 ms thread.
Now it works properly.
 
  


Reply

Tags
port, serial



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 07:11 AM
Minicom -- want to read data coming in a serial port ihopeto Linux - Newbie 2 04-12-2009 09:46 PM
Serial programming - binary data using open() and read() neutron001 Programming 5 04-07-2008 03:04 PM
Bad data in socket read function nhydra Programming 2 05-08-2006 03:16 AM
Why can't I read in data from the serial port using a bash script? tjt Linux - Newbie 1 06-17-2004 12:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration