C in 4GL
Posted in 1993
Hello people, I'm writing a 4gl prog that monitor input from a piece of kit on a tty line and sends commands to the same. Now I have written the code (it took a while as I'm a 'C' virgin 8-() and have reproduced part of it below. As you can see the code opens the device every call. This works 'cos currently the 4gl program runs, calls each routine once and exits. However, I want the routine to run continuously in the background. Now for the question -- do I leave the routines as they are opening the device on each call or should I just open it the once at the start of the program. If the latter, how do I arrange things so that the file pointer is remembered between calls? Here's some sample code: #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <termio.h> int sendcmd(n) int n; { FILE *f; int fd; struct termio buf; int stx, etx, eot, gs, enq, ack; int t; char cmd[60]; char data[40]; int datalen; if ((fd=open("/dev/ttyi23", O_RDWR))==-1) exit(1); ioctl(fd, TCGETA, &buf); buf.c_iflag |= ISTRIP; buf.c_cflag &= ~CSIZE; buf.c_cflag |= CS7; buf.c_cflag |= PARENB; buf.c_cflag |= CSTOPB; buf.c_cflag &= ~CBAUD; buf.c_cflag |= B9600; buf.c_cc[VEOF]=1; buf.c_cc[VEOL]=0; ioctl(fd, TCSETAW, &buf); eot=4; gs=29; enq=5; ack=6; stx=2; etx=3; f=fdopen(fd, "r+"); popint(&datalen); popvchar(&data[0], datalen); t=0; cmd[t++]=eot; cmd[t++]=gs; cmd[t++]=enq; cmd[t]=0; fputs(&cmd[0], f); sleep(1); t=0; cmd[t++]=stx; t+=sprintf(&cmd[t], "%s", &data[0]); cmd[t++]=etx; cmd[t++]=eot; cmd[t]=0; fputs(&cmd[0], f); return(0); } int getscan(n) int n; { FILE *f, *g; int fd; struct termio buf; int lf, cr, stx, etx, eot, fs, enq, ack, nak; int c; int t; char s[50]; if ((fd=open("/dev/ttyi23", O_RDWR))==-1) exit(1); ioctl(fd, TCGETA, &buf); buf.c_iflag |= ISTRIP; buf.c_cflag &= ~CSIZE; buf.c_cflag |= CS7; buf.c_cflag |= PARENB; buf.c_cflag |= CSTOPB; buf.c_cflag &= ~CBAUD; buf.c_cflag |= B9600; buf.c_cc[VEOF]=1; buf.c_cc[VEOL]=0; ioctl(fd, TCSETAW, &buf); eot=4; fs=28; enq=5; ack=6; nak=21; stx=2; etx=3; cr=13; lf=10; f=fdopen(fd, "r+"); fputc(eot, f); fputc(fs, f); fputc(enq, f); t=0; if((c=fgetc(f))!=eot) { while((c=fgetc(f))!=etx) s[t++]=c; s[t]=0; } else { t+=sprintf(&s[0], "No data."); s[t]=0; } retquote(&s[0]); return(1); } TIA | I work with some bloody strange people, and so do they! | |------------------------------------------------------------| | Stuart Hemming | shemminga@cix.compulink.co.uk | | Tudor Labels Ltd | uunet!cix.compulink.co.uk!shemminga | | Roman Bank Bourne | Tel : (+44) 778 426444 | | Lincs PE10 9LQ. UK. | Fax : (+44) 778 421862 |