* Trouble reading a character device. [not found] <2e0180190812152308j64b827a2r21da6ff56e139f7e@mail.gmail.com> @ 2008-12-16 7:11 ` saravanaraj v 2008-12-16 14:51 ` Glynn Clements 0 siblings, 1 reply; 3+ messages in thread From: saravanaraj v @ 2008-12-16 7:11 UTC (permalink / raw) To: linux-c-programming Hi, I am facing few issues in reading from a character device. I have created few character devices using 'mknod' and I am writing continuously to those devices using 'printk'. After some time I start another process to read from the device. The open/read is done using normal sys calls for file open/reading. I have pasted the code for your ref. When I run this program the read happens for some time and then there is no progress. I am suspecting that everything written to the device before this program was started is read. The flags I tried for opening the file were RD, RDWR with/without NONBLOCK. The output was same in all the cases. I tried "cu -l /dev/virt_uart8". It was able to read continuosuly without any problem. Can someone tell where the problem is ? int main(){ int fd,counter=0; fd = open("/dev/virt_uart8",O_RDONLY|O_NONBLOCK); if(fd<0) printf("\nError in opeining file"); while(1){ int nread = 0; nread = read(fd,buffer,MAX_BUFFER_SIZE-1); if(nread>0) { buffer[nread]='\0'; printf("%s",buffer); } } } Thanks, Saravanaraj ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Trouble reading a character device. 2008-12-16 7:11 ` Trouble reading a character device saravanaraj v @ 2008-12-16 14:51 ` Glynn Clements 2008-12-18 12:17 ` saravanaraj v 0 siblings, 1 reply; 3+ messages in thread From: Glynn Clements @ 2008-12-16 14:51 UTC (permalink / raw) To: saravanaraj v; +Cc: linux-c-programming saravanaraj v wrote: > I am facing few issues in reading from a character device. > I have created few character devices using 'mknod' and I am writing > continuously to those devices using 'printk'. After some time I start > another process to read from the device. The open/read is done using > normal sys calls for file open/reading. I have pasted the code for > your ref. When I run this program the read happens for some time and > then there is no progress. I am suspecting that everything written to > the device before this program was started is read. The flags I tried > for opening the file were RD, RDWR with/without NONBLOCK. The output > was same in all the cases. I tried "cu -l /dev/virt_uart8". It was > able to read continuosuly without any problem. Can someone tell where > the problem is ? > > int main(){ > > int fd,counter=0; > fd = open("/dev/virt_uart8",O_RDONLY|O_NONBLOCK); > if(fd<0) printf("\nError in opeining file"); > while(1){ > > int nread = 0; > nread = read(fd,buffer,MAX_BUFFER_SIZE-1); > if(nread>0) { > buffer[nread]='\0'; > printf("%s",buffer); > } If you want to print data which doesn't necessarily have a trailing newline, you need fflush(stdout) or setvbuf(stdout, NULL, _IONBF, 0). If you make that change and still have problems, it's likely to be an issue with the device. E.g. you may need to disable XON/XOFF processing with tcsetattr(), or put the device into raw mode. -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Trouble reading a character device. 2008-12-16 14:51 ` Glynn Clements @ 2008-12-18 12:17 ` saravanaraj v 0 siblings, 0 replies; 3+ messages in thread From: saravanaraj v @ 2008-12-18 12:17 UTC (permalink / raw) To: linux-c-programming Thanks Glynn On Tue, Dec 16, 2008 at 8:21 PM, Glynn Clements <glynn@gclements.plus.com> wrote: > > saravanaraj v wrote: > > > I am facing few issues in reading from a character device. > > I have created few character devices using 'mknod' and I am writing > > continuously to those devices using 'printk'. After some time I start > > another process to read from the device. The open/read is done using > > normal sys calls for file open/reading. I have pasted the code for > > your ref. When I run this program the read happens for some time and > > then there is no progress. I am suspecting that everything written to > > the device before this program was started is read. The flags I tried > > for opening the file were RD, RDWR with/without NONBLOCK. The output > > was same in all the cases. I tried "cu -l /dev/virt_uart8". It was > > able to read continuosuly without any problem. Can someone tell where > > the problem is ? > > > > int main(){ > > > > int fd,counter=0; > > fd = open("/dev/virt_uart8",O_RDONLY|O_NONBLOCK); > > if(fd<0) printf("\nError in opeining file"); > > while(1){ > > > > int nread = 0; > > nread = read(fd,buffer,MAX_BUFFER_SIZE-1); > > if(nread>0) { > > buffer[nread]='\0'; > > printf("%s",buffer); > > } > > If you want to print data which doesn't necessarily have a trailing > newline, you need fflush(stdout) or setvbuf(stdout, NULL, _IONBF, 0). > > If you make that change and still have problems, it's likely to be an > issue with the device. E.g. you may need to disable XON/XOFF > processing with tcsetattr(), or put the device into raw mode. > > -- > Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-12-18 12:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <2e0180190812152308j64b827a2r21da6ff56e139f7e@mail.gmail.com>
2008-12-16 7:11 ` Trouble reading a character device saravanaraj v
2008-12-16 14:51 ` Glynn Clements
2008-12-18 12:17 ` saravanaraj v
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).