Solved

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

Posted on 1997-08-20
6
321 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now