LinuxQuestions.org
Review your favorite Linux distribution.
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 08-01-2012, 06:00 PM   #1
dnewhous
LQ Newbie
 
Registered: Jul 2012
Location: Peoria, IL
Posts: 3

Rep: Reputation: 0
Question C/C++ blocking wait for character on USB


I'm thinking

int fd_usb = open("/dev/ttyS0", O_RDONLY);

fcntl(fd_usb, F_SETFL, 0);
char data_in;

while(read(fd_usb, &data_in, sizeof(char))>0)
{
printf("%c\n",data_in);
//do other stuff here
}
What I don't want is for printf to print the same character over and over when there is no new character to be read. There is only one character that is interesting, the letter 'p'. The data rate will be serial port rate.

I'm not sure that what I want is conceptually possible - to have the read function block until new data is ready.

Last edited by dnewhous; 08-02-2012 at 02:46 PM.
 
Old 08-01-2012, 10:38 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. 'read' returns the number of bytes, store it in a variable
2. if you don't want 'read' to block, use non-blocking mode
3. if you want to read the input per-character (and not per-line) use 'termios' to set non-canonical mode (Example: http://dtelnet.sf.net/shkeys.c)
 
Old 08-02-2012, 02:34 PM   #3
dnewhous
LQ Newbie
 
Registered: Jul 2012
Location: Peoria, IL
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
2. if you don't want 'read' to block, use non-blocking mode
I want read to block. But I need to restart my thinking. The command I am using in my OP for opening a file descriptor for a USB device is not working. How do I open a file descriptor for a USB device that connects on COM4? My platform is both Cygwin on Windows 7 and Ubuntu.

Last edited by dnewhous; 08-02-2012 at 05:12 PM.
 
Old 08-03-2012, 02:21 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
/dev/ttyUSB0 is a possible device name, but you have to found out it yourself
 
  


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
USB blocking in linux sachinm Linux - Software 2 07-03-2008 07:21 PM
Blocking external usb storage access on centos 5 pradeep.oops Linux - Security 2 05-23-2008 05:47 AM
long wait time for df after mounting usb hd beanerjo Linux - Hardware 7 11-13-2007 07:25 AM
USB Blocking PC on transfer MBH Linux - Hardware 2 05-27-2006 03:00 PM
2 questions about adsl usb modem and Susefirewall2, is that allowed? Wait, that's 3.. M-8 Linux - Networking 0 04-19-2004 05:27 AM

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

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