diff --git a/spool/Makefile b/spool/Makefile new file mode 100644 index 0000000..f3a00af --- /dev/null +++ b/spool/Makefile @@ -0,0 +1,25 @@ +# +# spool/Makefile - Build the job spooler +# +# Written 2008, 2009, 2012 by Werner Almesberger +# Copyright 2008, 2009, 2012 Werner Almesberger +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + + +MAIN = spool +OBJS = spool.o serial.o + +CFLAGS = -Wall -Wshadow + +all: $(MAIN) + +$(MAIN): $(OBJS) + $(CC) $(LDFLAGS) -o $(MAIN) $(OBJS) + +clean: + rm -f $(OBJS) diff --git a/spool/serial.c b/spool/serial.c new file mode 100644 index 0000000..b3392b1 --- /dev/null +++ b/spool/serial.c @@ -0,0 +1,100 @@ +/* + * serial.c - Send data to a Modela MDX-15/20 + * + * Written 2009 by Werner Almesberger + * Copyright 2009 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" + + +static int tty; +static struct termios old; + + +static void reset_tty(void) +{ + if (tty >= 0) + if (tcsetattr(tty, TCSADRAIN, &old) < 0) + perror("tcsetattr"); +} + + +void serial_open(const char *name) +{ + struct termios new; + + + tty = open(name, O_WRONLY | O_NOCTTY); + if (tty < 0) { + perror(name); + exit(1); + } + + if (tcgetattr(tty, &old) < 0) { + perror("tcgetattr"); + exit(1); + } + atexit(reset_tty); + + new = old; + cfmakeraw(&new); + cfsetospeed(&new, B9600); + new.c_iflag |= IXON; + new.c_cflag |= CLOCAL | CRTSCTS; + new.c_lflag &= ~ECHO; + if (tcsetattr(tty, TCSAFLUSH, &new) < 0) { + perror("tcsetattr"); + exit(1); + } +} + + +void serial_close(void) +{ + reset_tty(); + (void) close(tty); + tty = -1; +} + + +void serial_printf(const char *fmt, ...) +{ + char buf[10000]; /* more than enough :) */ + va_list ap; + int len; + ssize_t wrote; + + va_start(ap, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + if (len >= sizeof(buf)-1) { + fprintf(stderr, "buffer too small\n"); + exit(1); + } + wrote = write(tty, buf, strlen(buf)); + if (wrote < 0) { + perror("write"); + exit(1); + } + if (wrote != len) { + fprintf(stderr, "short write: %d < %d\n", + (int) wrote, (int) len); + exit(1); + } +} diff --git a/spool/serial.h b/spool/serial.h new file mode 100644 index 0000000..b65e826 --- /dev/null +++ b/spool/serial.h @@ -0,0 +1,21 @@ +/* + * serial.h - Send data to a Modela MDX-15/20 + * + * Written 2009 by Werner Almesberger + * Copyright 2009 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#ifndef SERIAL_H +#define SERIAL_H + +void serial_open(const char *name); +void serial_close(void); +void serial_printf(const char *fmt, ...); + +#endif /* !SERIAL_H */ diff --git a/spool/spool.c b/spool/spool.c new file mode 100644 index 0000000..0887f74 --- /dev/null +++ b/spool/spool.c @@ -0,0 +1,65 @@ +/* + * spool.c - Send a file to a Roland MDX series CNC mill + * + * Written 2009, 2010, 2012 by Werner Almesberger + * Copyright 2009, 2010, 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include + +#include "serial.h" + + +#define DEFAULT_PORT "/dev/ttyS0" +#define BUF_SIZE 8192 + + +static void usage(const char *name) +{ + fprintf(stderr, "usage: %s [file]\n", name); + exit(1); +} + + +int main(int argc, const char **argv) +{ + FILE *file; + char buf[BUF_SIZE+1]; + size_t n; + char *port; + + switch (argc) { + case 1: + file = stdin; + break; + case 2: + file = fopen(argv[1], "r"); + if (!file) { + perror(argv[1]); + exit(1); + } + break; + default: + usage(*argv); + } + port = getenv("PORT"); + serial_open(port ? port : DEFAULT_PORT); + while (1) { + n = fread(buf, 1, BUF_SIZE, file); + if (!n) + break; + buf[n] = 0; + serial_printf("%s", buf); + } +sleep(3600); + serial_close(); + return 0; +}