LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-08-2017, 09:32 PM   #1
zvivered
Member
 
Registered: Sep 2007
Posts: 237

Rep: Reputation: 23
Blocked Read N bytes from UART


Hello,

I want to read from ttyS0 TBD bytes. I need that my thread will be blocked for TBD seconds till I receive all N bytes.

So I used the following code:

m_Handle = open( "ttyS0", O_RDWR);
fd_set set;

timeout.tv_sec = 5;
timeout.tv_usec = 0;

FD_ZERO(&set); // clear the set
FD_SET(m_Handle, &set); // add our file descriptor to the set

rc = select(m_Handle + 1, &set, NULL, NULL, &timeout);
rc = read(m_Handle, pData, Size);

Will this do the work ?

Thank you,
Zvika
 
Old 06-09-2017, 07:55 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,357
Blog Entries: 13

Rep: Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411
In the future, recommend you use [code][/code] tags around your code to make it more readable.

Without adding that to a test program and running it myself, my review of it tells me that it "should" work as intended.

However the select() call is specifically for checking file handles, as well as blocking entirely, forget timeouts, until you have new data.

I suspect that you may respond citing that you need to run the thread to do other things. If so, then you are doing too much in this thread and need a different thread to do other work.

You use select() to tell you when you have data available. You use the read() to read that data.

Next you may cite that you don't wish to read incomplete data from the UART.

If so then my opinion is that your receive parser should be message agnostic and should be capable of processing any number of bytes. It should process at the byte level first in a state machine and then dispatch full messages off to another logic level which deals with the message oriented data.

Those are just some guesses as to why you feel you need this architecture. Perhaps you can share a bit more to clarify why you feel this is needed.

And the other question I have is when you ask if it will work or not, ... sorry, but you haven't tried it already?
 
  


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
Where does uart console get its value of uart port membase? unifoxz Linux - Kernel 1 12-20-2011 02:28 PM
Read System Call is getting blocked when tried to read the data from CDC device sanju.lnt Linux - Embedded & Single-board computer 0 09-11-2011 11:48 PM
Cannot read beyond the first 2^63 bytes in /proc/<pid>/mem yeye_olive Programming 5 05-17-2009 04:06 AM
read API is blocked lucky6969b Programming 2 12-08-2005 07:27 PM
how to know how many bytes can read from a descriptor? (C/C++) Thinking Programming 6 10-28-2005 12:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:23 PM.

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