LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 09-12-2011, 11:12 AM   #1
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Rep: Reputation: 15
select function does not return immediately


I am writing a server and a client which communicate via USB-TTL devices. Here are the example codes of server and client.
server:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <memory.h>

int main()
{
    int fd_ttl;
    int read_len;
    fd_set readfds;
    char buf[64];

    fd_ttl = open("/dev/ttyUSB0", O_RDWR);

    if(fd_ttl < 0)
        return -1; 

    while(1)
    {   
        FD_ZERO(&readfds);
        FD_SET(fd_ttl, &readfds);

        if(select(fd_ttl + 1, &readfds, NULL, NULL, NULL) < 0)
            continue;

        if(FD_ISSET(fd_ttl, &readfds))
        {   
            bzero(buf, sizeof(buf));
            read_len = read(fd_ttl, buf, 1); 
            printf("%c", buf[0]);
            if(buf[0] == 0x00)
                printf("\n");
        }
    }

    close(fd_ttl);

    return 0;
}
client:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd_ttl;
    int wr_len;
    char buf[] = "Cricket";

    fd_ttl = open("/dev/ttyUSB0", O_RDWR);

    wr_len = write(fd_ttl, buf, sizeof(buf));
    printf("%d byte(s) written for %d\n", wr_len, fd_ttl);

    close(fd_ttl);

    return 0;
}
The server never print the content the client sends to it, instead, the data are buffered for the corresponding file descriptor. When I use the command cat "hallo" > /dev/ttyUSB0, all the data include that are buffered and sent with cat command are printed.

Does the redirect(>) do anything extra to signal select?
 
Old 09-12-2011, 11:56 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Does it work properly if you block on read and skip select? How do you know the block is on select and not on read? Redirection just opens the output file and uses dup2 to replace standard output before executing the program whose output is being redirected.
Kevin Barry
 
1 members found this post helpful.
Old 09-12-2011, 01:10 PM   #3
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
thanks, I am sure it blocks on select not on read. I use the code below to test:
Code:
......
if(select(fd_ttl + 1, &readfds, NULL, NULL, NULL) < 0)
     continue;

printf(FD_ISSET);//this line is for test, it is printed only when I use the command cat "hallo" > /dev/ttyUSB0
if(FD_ISSET(fd_ttl, &readfds))
{
......
}
 
Old 09-12-2011, 01:52 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Have you tried read without select?
Kevin Barry
 
Old 09-13-2011, 02:43 AM   #5
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
As your suggestion, I tried read without select. But another problem blocked me: the program blocks on open function. Then I tried to figure it out. First I entered cat /dev/ttyUSB0 in one terminal and entered echo "Cricket" > /dev/ttyUSB0 in another, they both hang, no any output and not move on. CTRL+C to any of them, the other ends with "/dev/ttyUSB0: Input/output error" prompted.

I know it is going to be more and more troublesome......
 
Old 09-15-2011, 01:52 AM   #6
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Thanks, ta0kira. My programs are working well now. When my server hang on open function, modprobe -r pl2303 then modprobe pl2303 makes it running smoothly. I still don't understand what is the problem about both problems, maybe they solved by time.
 
Old 09-16-2011, 05:57 PM   #7
TimothyEBaldwin
Member
 
Registered: Mar 2009
Posts: 249

Rep: Reputation: 27
By default tty devices use line buffering with echo, so the data will not be sent your server program until a newline is received, and deadlock will quickly result.

Read the Serial Programming Guide for POSIX Operating Systems - Michael R Sweet
 
Old 09-16-2011, 10:04 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by TimothyEBaldwin View Post
By default tty devices use line buffering with echo, so the data will not be sent your server program until a newline is received, and deadlock will quickly result.

Read the Serial Programming Guide for POSIX Operating Systems - Michael R Sweet
Are you talking about canonical input mode like terminals have?
Kevin Barry
 
Old 09-17-2011, 09:41 AM   #9
TimothyEBaldwin
Member
 
Registered: Mar 2009
Posts: 249

Rep: Reputation: 27
Yes
 
Old 09-19-2011, 11:00 PM   #10
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Danke schön, guys.
 
  


Reply



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
return value from a function jamesbon Programming 7 11-17-2010 07:39 AM
Get return value of function mortonmorton Linux - Newbie 4 10-09-2009 09:54 PM
return value of select() call sinu_nayak2001 Programming 9 10-09-2009 05:33 AM
How does a blocking select return (kernel)? iw05t Linux - Newbie 1 11-09-2006 03:52 PM
select function as timeout? frostmagic Programming 2 02-09-2004 11:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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