Subject: Reading from a serial port/modem
To: None <netbsd-help@netbsd.org>
From: David Forbes <david@flossy.u-net.com>
List: netbsd-help
Date: 07/23/2000 15:10:42
I've been trying to read data from the serial port to which my modem is
connected.  I wish to be able to detect when the phone is ringing.

Originally, I tried using /dev/tty00 (which is what pppd uses).  However,
I found that I couldn't read anything and I wasn't convinced that I was
successfully writing anything either, even when I'd tried various things
to set the clocal flag.

I noticed a recent message on current-users that recommended using
/dev/dty00, so I've tried that without any luck.  I know it must be
possible because I can use minicom to read the "RING" string when the
phone rings.

Below is the program I've tried.  I did (sometime ago) attempt to read the
state of the RI line, but without much luck, either.

If it matters, this is on an arm32 running standard 1.4.  The modem works
(if you receive this mail!), and I can talk to it directly through
minicom, if I need to.

Any clues greatly appreciated.

Thanks,

David.


/* Configuration */
#define SECS_PER_RING 2
#define N_RINGS 2
#define DELAY 30
#define TTY_NAME "/dev/dty00"
#define BUF_SIZE 256

/* Includes */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/types.h>
#include <poll.h>
#include <termios.h>

int tty_open() {
    return open(TTY_NAME, O_RDWR | O_NONBLOCK | O_SYNC | O_RSYNC, 0x764);
}

void tty_close(int fd) {
    close(fd);
}

void buf_zero(char *buf, int buf_size) {
	int i;
	for (i = 0; i < buf_size; i++)
		buf[i]=0;
}

int main() {

    int tty_fd;
    int line_disc;
    int modem_state;
    int term_flags;
    int temp;
    char buf[BUF_SIZE];
    struct pollfd poll_data;
    struct termios term_info;


    term_flags = TIOCFLAG_CLOCAL | TIOCFLAG_SOFTCAR;

    tty_fd = tty_open();
    if (tty_fd < 0) puts("error occured\n");

    poll_data.fd = tty_fd;
    poll_data.events = POLLWRNORM;


    poll(&poll_data,1, INFTIM);
    printf("poll return %x %x\n", poll_data.revents, POLLWRNORM);
    printf("performing write %x %x\n", write(tty_fd, "+++\nATZ\n", 8), errno);
    
    poll_data.events = POLLRDNORM;
    poll(&poll_data, 1, INFTIM);

    buf_zero(buf, BUF_SIZE);
    printf("poll returned because %x %x\n", poll_data.revents, POLLRDNORM);
    printf("%x %x\n", read(tty_fd, buf, BUF_SIZE), errno);

    tty_close(tty_fd);

    return 0;
}