I've been working on this piece of code as a diagnostic too to help me
write some software that interfaces with DNC machines (Big industrial
machines with serial ports). The code works fine, it monitors handshaking
lines, monitors input, and allows you to type to the port (and even sends
it over the port, too!)... Except for one thing... When I hook it up to
my mode, the modem echos whatever I type, but will *NOT* respond. Quit
this & run a terminal, and the modem runs just fine. I've tried this with
two different modems, both did the same thing.
What I want from you (whomever *you* might be) is for you to take a peek
at the code, and see if you can see what I'm doing wrong. I don't profess
to know a lot about c, or a lot about working with serial ports (in c).
Heck, I don;t even think the code is that good, but it works (sort of)...
Besides, if you can get this working, you've got a nifty little diagnostics
tool! :)
Thanks for any & all help!
-Tod.
-------- Here begins port_monitor.c which almost works :) -----------------
/*
By: Todd D. Ihde
This program is in the Public Domain. If you find it usefull, great!
If you make changes to it, please give me a copy of the new code.
Compile with:
gcc -o port_monitor port_monitor.c -lncurses -I/usr/include/ncurses
...Which of course means that you need ncurses to compile. This
proggie was written on a 486 Linux box running RedHat commercial
Linux version 1.2.13 (ELF), ncurses version 1.9.6, gcc version
2.7.0
Use [CTRL]-A to exit.
Use [CTRL]-B to send "ATDT867-5309" to port. (Don't have modem plugged
in to phone jack, of course).
*/
#include <curses.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
struct termios tios;
char device[256];
const char *defaultdevice="/dev/cua2";
char character[2];
int status2;
int keyin;
int fd; /* for serial device */
int status; /* status of system calls */
unsigned int old_status = 0; /* value of previous call */
unsigned int arg; /* value returned by ioctl */
WINDOW *bits_win;
WINDOW *data_win;
WINDOW *input_win;
/* print command usage and exit */
void usage()
{
fprintf(stderr, "usage: Port_monitor [device]\n");
fprintf(stderr, " Default device is %s\n", device);
exit(1);
void parse_args(int argc, char **argv)Quote:}
{
/* check for a single argument */
if (argc > 2)
usage(); /* too many arguments */
if (argc == 2)
strcpy(device, argv[1]); /* one argument */
/* called before exiting */Quote:}
void cleanup()
{
endwin(); /* required by curses */
/* main program */Quote:}
int main(int argc, char *argv[])
{
/* make default the default */
strcpy(device, defaultdevice);
/* parse command line arguments */
parse_args(argc, argv);
/* open port */
fd = open(device, O_RDWR | O_NOCTTY );
if (fd == -1) {
char s[255];
sprintf(s, "port_monitor: can't open device `%s'", device);
perror(s);
exit(1);
}
tios.c_cflag = B9600 | CLOCAL | CRTSCTS | CREAD | CS8;
tios.c_iflag = IGNPAR;
tios.c_oflag=0;
tios.c_lflag=0;
tcsetattr(fd,TCSANOW,&tios);
/* init curses */
initscr();
cbreak();
noecho();
nodelay(stdscr,TRUE);
bits_win=newwin(7,79,0,0);
data_win=newwin(10,79,11,0);
input_win=newwin(2,79,22,0);
scrollok(data_win,TRUE);
scrollok(input_win,TRUE);
atexit(cleanup);
move(10,0);
hline(ACS_HLINE,80);
move(21,0);
hline(ACS_HLINE,80);
move(24,0);
hline(ACS_HLINE,80);
setscrreg(11,20);
move (20,0);
refresh();
/* loop forever */
while (keyin!=1) {
/* get modem status info */
status = ioctl(fd, TIOCMGET, &arg);
if (status != 0) {
perror("port_monitor: TIOCMGET failed");
exit(1);
}
/* home cursor */
mvwaddstr(bits_win,0,0,"RTS out ON ");
mvwaddstr(bits_win,1,0,"CTS in ON ");
mvwaddstr(bits_win,2,0,"DSR in ON ");
mvwaddstr(bits_win,3,0,"DCD in ON ");
mvwaddstr(bits_win,4,0,"DTR out ON ");
mvwaddstr(bits_win,5,0,"RI in ON ");
if ( !(arg & TIOCM_RTS) )
mvwaddstr(bits_win,0,14,"FF");
if ( !(arg & TIOCM_CTS) )
mvwaddstr(bits_win,1,14,"FF");
if ( !(arg & TIOCM_DSR) )
mvwaddstr(bits_win,2,14,"FF");
if ( !(arg & TIOCM_CAR) )
mvwaddstr(bits_win,3,14,"FF");
if ( !(arg & TIOCM_DTR) )
mvwaddstr(bits_win,4,14,"FF");
if ( !(arg & TIOCM_RNG) )
mvwaddstr(bits_win,5,14,"FF");
if (arg!=old_status)
{
wrefresh(bits_win);
old_status=arg;
};
/* Get chars & dump 'em to the screen & such... */
errno=0;
status2=read(fd,character,1);
if (status2>0 && errno!=EAGAIN)
{
if (strcmp(character,"\0"))
{
wprintw(data_win,"%s",character);
/*if (!strcmp(character,"\n"))*/
wrefresh(data_win);
};
};
keyin=getch();
if (keyin!=ERR)
{
if (keyin==2)
write(fd,"ATDT867-5309\n",13);
else
{
wprintw(input_win,"%c",keyin);
write(fd,&keyin,1);
wrefresh(input_win);
};
};
}
return 0;
Quote:}