2012-03-11 14:37:38 +02:00
|
|
|
/*
|
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "serial.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_PORT "/dev/ttyS0"
|
|
|
|
#define BUF_SIZE 8192
|
|
|
|
|
|
|
|
|
2012-03-21 21:57:05 +02:00
|
|
|
static void spool_file(FILE *file)
|
|
|
|
{
|
|
|
|
char buf[BUF_SIZE+1];
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = fread(buf, 1, BUF_SIZE, file);
|
|
|
|
if (!n)
|
|
|
|
break;
|
|
|
|
buf[n] = 0;
|
|
|
|
serial_printf("%s", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-11 14:37:38 +02:00
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
2012-03-21 21:57:05 +02:00
|
|
|
fprintf(stderr, "usage: %s [file ...]\n", name);
|
2012-03-11 14:37:38 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
2012-03-21 21:57:05 +02:00
|
|
|
FILE **files;
|
2012-03-11 14:37:38 +02:00
|
|
|
char *port;
|
2012-03-21 21:57:05 +02:00
|
|
|
int n_files, i;
|
2012-03-11 14:37:38 +02:00
|
|
|
|
2012-03-21 21:57:05 +02:00
|
|
|
/*
|
|
|
|
* Open all files before starting to spool, so that we don't risk
|
|
|
|
* failing in the middle of the job.
|
|
|
|
*/
|
|
|
|
n_files = argc == 1 ? 1 : argc-1;
|
|
|
|
files = malloc(sizeof(FILE *));
|
|
|
|
if (!files) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
*files = stdin;
|
|
|
|
} else {
|
|
|
|
if (*argv[1] == '-')
|
|
|
|
usage(*argv);
|
|
|
|
for (i = 0; i != n_files; i++) {
|
|
|
|
files[i] = fopen(argv[i+1], "r");
|
|
|
|
if (!files[i]) {
|
|
|
|
perror(argv[i+1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-03-11 14:37:38 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-21 21:57:05 +02:00
|
|
|
|
2012-03-11 14:37:38 +02:00
|
|
|
port = getenv("PORT");
|
|
|
|
serial_open(port ? port : DEFAULT_PORT);
|
2012-03-21 21:57:05 +02:00
|
|
|
for (i = 0; i != n_files; i++)
|
|
|
|
spool_file(files[i]);
|
2012-03-11 14:37:38 +02:00
|
|
|
sleep(3600);
|
|
|
|
serial_close();
|
|
|
|
return 0;
|
|
|
|
}
|