?
Solved

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

Posted on 1997-08-20
6
Medium Priority
?
386 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 60 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month8 days, 9 hours left to enroll

765 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