1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-22 23:51:08 +02:00

spool/: job spooler for CNC mill (migrated from Openmoko)

From
http://svn.openmoko.org/developers/werner/cncmap/spool/

Fixed a buffer termination bug that caused the mill to do strange
(yet almost always harmless) things at the end of a job.
This commit is contained in:
Werner Almesberger 2012-03-11 09:37:38 -03:00
parent 90254ef0bc
commit 6271d5f721
4 changed files with 211 additions and 0 deletions

25
spool/Makefile Normal file
View File

@ -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)

100
spool/serial.c Normal file
View File

@ -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 <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#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);
}
}

21
spool/serial.h Normal file
View File

@ -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 */

65
spool/spool.c Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#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;
}