Solved

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

Posted on 1997-08-20
6
358 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 96
ShiftLeft challenge 21 86
how to split multiple lines delimiter : 8 112
JQuery serialize and unserialize 8 138
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 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