ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#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?
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
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))
{
......
}
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......
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.