LinuxQuestions.org
Help answer threads with 0 replies.
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 06-11-2009, 03:51 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
the get data from serial port function read() blocks when data not available


I am using read() in c++ to get data from a serial port. However, if no data is available on the serial port the function blocks until dta arrives.

Example code:

//------------------------------------------------------------
char m_readBuffer[255] = {0};

char* p_curChar = m_readBuffer;
ssize_t numberOfBytes = 0;
do {
const ssize_t BYTES_READ = read(m_fd, p_curChar, 1);
if (0 != BYTES_READ)
{
if (numberOfBytes < (SIZEOF_READBUFFER - 1))
{
numberOfBytes += BYTES_READ;
p_curChar = m_readBuffer + static_cast<int>(numberOfBytes);
}
else
{
p_curChar = 0;
}
}
} while ((sz_LF != p_curChar) ||
(sz_CR != p_curChar) ||
(0 != p_curChar));
//------------------------------------------------------------

Is there a way a either a) setting the read() funtion to timeout, b) stop the read() funtion from blocking so it returns if no data available or c) use of another funtion?
 
Old 06-11-2009, 04:21 AM   #2
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
OK it would seem my post was a little hasty!

Change the open() function to include the flag O_NONBLOCKING does the trick.
 
  


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
Minicom -- want to read data coming in a serial port ihopeto Linux - Newbie 2 04-12-2009 09:46 PM
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 08:35 PM
Bad data in socket read function nhydra Programming 2 05-08-2006 03:16 AM
accessing data from serial port PrerakParikh Linux - Hardware 2 08-24-2004 10:17 PM
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


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