// dnc.c // // Copy files between CNC control and network drive // // usage: // haas /dev/ttyUSB0 /path/haascode.txt /path/haas.log // mori /dev/ttyUSB1 /path/moricode.txt /path/mori.log // // /dev/ttyUSB0: serial port device // /path/haascode.txt: name of CNC g-code file to download on request from // Haas control. // /path/haas.log: name of log file for error messages // // notes: // - Assumes that input or output files reside at a specific location. // - haas is run as a "daemon" in background from /etc/init.d . // // compiling: // - must be compiled with a file containing setRaw with parameters // for specific CNC machine. Here are our two examples: // // gcc dnc.c haastty.c -o haas // gcc dnc.c moritty.c -o mori // // Ralph Stirling, 2009-01-16 modified 2012-09-20 // stirra@wallawalla.edu #include #include #include #include #include #include #include #include #include #include #include #include #define DC1 0x11 #define DC2 0x12 #define DC3 0x13 #define DC4 0x14 int setRaw(int fd, struct termios *ttystate_orig); int logit(FILE *f, const char *fmt, ...); int main(int ac, char *av[]) { FILE *logf; fd_set rfds; int rval; int rfd; int wfd; int ofd; int gfd; char ch; char b; int i; int sending = 0; int pause = 0; int receiving = 0; char *fname; char *gfile; char *lfile; char line[80]; int linelen; int len; struct termios istate, ostate; struct timeval timeout, *to; long bytesread = 0; timeout.tv_sec = 0; timeout.tv_usec = 0L; fname = "/dev/ttyUSB0"; gfile = "haascode.txt"; lfile = NULL; switch (ac) { default: case 4: lfile = av[3]; // log file name case 3: gfile = av[2]; // g-code input file case 2: fname = av[1]; // serial port name case 1: case 0: break; } if (lfile == NULL) logf = stdout; else logf = fopen(lfile, "a"); if (logf == NULL) { fprintf(stderr, "logf null\n"); logf = stdout; } else fprintf(stderr, "started logging\n"); logit(logf, "started %s\n", av[0]); wfd = open(fname, O_WRONLY); if (wfd < 0) { logit(logf, "open %s for write failed\n", fname); exit(1); } rfd = open(fname, O_RDONLY | O_NDELAY); if (rfd < 0) { logit(logf, "open %s for read failed\n", fname); exit(1); } setRaw(rfd, &istate); setRaw(wfd, &ostate); sending = 0; pause = 0; to = NULL; while (1) { FD_ZERO(&rfds); FD_SET(rfd, &rfds); rval = select(rfd+1, &rfds, NULL, NULL, to); if (rval < 0) { logit(logf, "select failed\n"); exit(1); } if (sending) { rval = read(gfd, &b, 1); if (rval == 1) { if (b != '\r') write(wfd, &b, 1); bytesread++; } else { logit(logf, "done sending %ld characters\n", bytesread); bytesread = 0; write(wfd, "%", 1); close(gfd); sending = 0; } } if (rval > 0) read(rfd, &ch, 1); else continue; switch (ch) { case DC1: if (!sending) { logit(logf, "Received DC1\n"); sprintf(line, "%s", gfile); logit(logf, "line=%s\n", line); gfd = open(line, O_RDONLY); if (gfd < 0) { write(wfd, "%%", 2); logit(logf, "open %s failed\n", line); break; } logit(logf, "sending\n"); sending = 1; to = &timeout; } else pause = 0; break; case DC2: if (!receiving) { logit(logf, "Received DC2\n"); sprintf(line, "%s.out", gfile); ofd = open(line, O_WRONLY | O_CREAT, 0644); if (ofd < 0) { logit(logf, "open %s failed\n", line); break; } logit(logf, "receiving\n"); receiving = 1; to = NULL; } break; case DC3: if (sending) { logit(logf, "pause\n"); pause = 1; } break; case DC4: if (receiving) { logit(logf, "done receiving\n"); close(ofd); receiving = 0; } default: if (!receiving) logit(logf, "unexpected: %c (%0x) sending=%d pause=%d\n", (unsigned char)ch, (unsigned char)ch, sending, pause); else write(ofd, &ch, 1); break; } } } int logit(FILE *f, const char *fmt, ...) { va_list argp; time_t t; char s[80]; t = time(NULL); strftime(s, 80, "%Y-%m-%d %H:%M:%S", localtime(&t)); fprintf(f, "%s => ", s); va_start(argp, fmt); vfprintf(f, fmt, argp); fflush(f); va_end(argp); }