Solved

How do I know whether my device is attached to tty?

Posted on 1997-08-20
6
381 Views
Last Modified: 2013-12-26
I'm using AIX Ver 4.1 on IBM server.
In a C program, how do I know whether my device is attached to tty?  My device is asynchronous.

If my device is not attached, my program will hang on the following sentence : read(port, buf, 22);

I've tried using the following sentences but to no avail:
      port = open(PORT, O_RDWR | O_NODELAY);
and
     port = open(PORT, O_RDWR | O_NONBLOCK);


Thanks!


0
Comment
Question by:Rita060297
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Accepted Solution

by:
rembo earned 30 total points
ID: 1294491


What sort of device is it?  Take a look at the man page
for the specific device and there should be an ioctl()
that you can use to determine device readiness.  The open()
system call often just sets a flag and never accesses
the device.  
0
 

Author Comment

by:Rita060297
ID: 1294492
The device is a digital clock which will send me the current time when
I send it a certain character.

I've tried ioctl with FIONREAD but I receive 0 for the number of bytes available to be read on the file descriptor when device is attached and device not attached.

Are there any alternatives?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1294493
So it's a tty device?
Can you try an ioctl TIOCMGET?
Can you do a select with a timeout?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:aditya070797
ID: 1294494
I don't understand the question completely, but I think you can
try using:
----------------------------------
struct termios *termios_ptr;
...
rv = tcgetattr(port, termios_ptr);
----------------------------------
If you get an error (rv == -1) and errno is set to ENOTTY,
then the port is not a tty.
0
 

Author Comment

by:Rita060297
ID: 1294495
I tried ioctl(port, TIOCMGET, &nbyte) and received the following results :

nbyte = 70 when device attached to port
nbyte = 6 when device not attached to port

I've found out that TIOCMGET defines the following :
Gets all modem bits.  The argument to this command is a pointer to an
integer where the current state of the modem status lines is stored.
Which modem status and modem control lines are supported depends on the capabilities of the hardware and the hardware's device driver.

Question :
(1) How do I know what 70 or 6 means?
(2) Can I use 70 and 6 as a guide for deteching the attached or non-attached device?  It seems to be consistent during my numerous testing.


0
 
LVL 84

Expert Comment

by:ozo
ID: 1294496
You should be able to find the definition of the bits in /usr/include/sys/termios.h
It looks like 6 is DTR+RTS, and 0106 is DTR+RTS+CD (carrier detect)


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question