[Contents] [Prev. Chapter] [Next Chapter] [Index] [Help]

D    Differences Between DIGITAL UNIX and ULTRIX Terminal Modem Control

This appendix contains three sample programs showing terminal (tty) modem control: an ULTRIX program showing an outgoing phone call, a DIGITAL UNIX program showing an outgoing phone call, and a DIGITAL UNIX program showing an incoming phone call. The ULTRIX system uses TIOCCAR, TIOCNAR, and TIOCWONLINE requests to the ioctl() system call. These requests are not supported on a DIGITAL UNIX system. See Section 7.8 for more information.

Example D-1 demonstrates how an ULTRIX application interacts with a modem for outgoing calls. Error checking of the return values of the system calls is purposely omitted to simplify the example.

Example D-1:  Modem Control for Outgoing Calls (ULTRIX)

fd = open(dcname, O_RDWR|O_NDELAY);      [1]
ioctl(fd, TIOCMODEM, &temp);             [2]
ioctl(fd, TIOCNCAR);                     [3]

.
.
.
/* * Dial the phone number and negotiate with auto calling unit. */
.
.
.
ioctl(fd, TIOCCAR); [4] alarm(40); ioctl(fd, TIOCWONLINE); [5] alarm(0);

  1. Opens the line and does not wait for carrier. [Return to example]

  2. Monitors the modem signals. [Return to example]

  3. Allows read and write calls to succeed regardless of whether carrier is present. [Return to example]

  4. Allows read and write calls to succeed only if carrier is present. [Return to example]

  5. Waits for carrier. [Return to example]

Example D-2 demonstrates how a DIGITAL UNIX application interacts with a modem for outgoing calls. Error checking of the return values of the system calls is purposely omitted to simplify the example.

Example D-2:  Modem Control for Outgoing Calls (DIGITAL UNIX)

int fd, flags;
struct termios tty_termios;            [1]
fd = open(ttyname,O_RDWR | O_NONBLOCK);  [2]
tcgetattr(fd,&tty_termios);	             [3]
if ((tty_termios.c_cflag & CLOCAL) == 0) {
	tty_termios.c_cflag |= CLOCAL;        [4]
	tcsetattr(fd,TCSANOW,&tty_termios);
}
flags = fcntl(fd, F_GETFL)
       fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)   [5]

.
.
.
/* * dial phone number and negotiate with modem. */
.
.
.
tty_termios.c_cflag &= ~CLOCAL; [6] tcsetattr(fd,&tty_termios); [7] alarm(40); [8] read(fd,buffer,count); [9] alarm(0); [10]

  1. Contains information about the serial line that can be inspected and altered using the POSIX tcgetattr() and tcsetattr() library routines. [Return to example]

  2. Opens the terminal line. The CLOCAL flag is usually set by default, allowing you to ignore modem status lines. Use O_NONBLOCK in case CLOCAL is not set. [Return to example]

  3. Gets current line attributes. [Return to example]

  4. Sets CLOCAL, if it is not set. The line must be in local mode in order to talk to the modem. [Return to example]

  5. Turns off O_NONBLOCK; in local mode the application does not need it. [Return to example]

  6. Puts the line into modem mode by turning off CLOCAL. The next I/O operation to the line will block until carrier is present. [Return to example]

  7. Watches for modem signals. [Return to example]

  8. Sets a timer so the application does not wait forever. [Return to example]

  9. This read() call blocks, pending the appearance of modem signals. [Return to example]

  10. Turns off timer; the connection with the remote system has been established. [Return to example]

Example D-3 demonstrates how a DIGITAL UNIX application interacts with a modem for incoming calls. Error checking of the return values of the system calls is purposely omitted to simplify the example.

Example D-3:  Modem Control for Incoming Calls (DIGITAL UNIX)

int fd;
int lined;
struct termios tty_termios;         [1]
fd = open(ttyname,O_RDWR | O_NONBLOCK);  [2]
flags = fcntl(fd, F_GETFL)
       fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)  [3]
setsid();			[4]
ioctl(0, TIOCSCTTY, 0);		  [5]
lined = 0;
ioctl(0, TIOCSETD, &lined);	[6]
tcgetattr(fd,&tty_termios);	[7]
tty_termios.c_cflag &= ~CLOCAL;	 [8]
tty_termios.c_cflag &= ~CBAUD;
tty_termios.c_cflag |= B2400;	[9]
tcsetattr(fd,TCSANOW,&tty_termios); 	[10]
for (;;) {
	write(fd,"\r\nlogin: ",9);	[11]
	read(fd,buffer,count);		[12]
	if (valid_login_name(buffer))
		execl("/usr/bin/login",buffer);  [13]
	}

  1. Contains information about the serial line that can be inspected and altered using the POSIX tcgetattr() and tcsetattr() library routines. [Return to example]

  2. Opens the terminal line. The CLOCAL flag is usually set by default, allowing you to ignore modem status lines. Use O_NONBLOCK in case CLOCAL is not set. [Return to example]

  3. Turns off O_NONBLOCK; you do not need it. [Return to example]

  4. Creates a new session, becomes session leader for new session, and becomes process leader for new process group. [Return to example]

  5. Sets the controlling terminal. [Return to example]

  6. Sets the line discipline to 0 (POSIX). [Return to example]

  7. Gets current line attributes. [Return to example]

  8. Turns off CLOCAL for a modem. [Return to example]

  9. Clears baud rate bits and sets them to 2400. [Return to example]

  10. Sets the line attributes. [Return to example]

  11. This write call, containing the login message, blocks until someone dials in on the modem and all the signals are present. [Return to example]

  12. Gets the user's login name. [Return to example]

  13. Executes the login program, if the login name is valid. [Return to example]


[Contents] [Prev. Chapter] [Next Chapter] [Index] [Help]