view rfcal/cmu200/sertool.c @ 187:219ae678b955

fc-serscpi utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Apr 2017 00:51:01 +0000
parents
children
line wrap: on
line source

/*
 * This module contains the main() function for fc-serscpi, a manual tool
 * intended for learning how to control the CMU200 with SCPI commands
 * over RS-232.
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>

extern int errno;

int target_fd;

static void
safe_output(buf, cc)
	u_char *buf;
{
	int i, c;

	for (i = 0; i < cc; i++) {
		c = buf[i];
		if (c == '\r' || c == '\n' || c == '\t' || c == '\b') {
			putchar(c);
			continue;
		}
		if (c & 0x80) {
			putchar('M');
			putchar('-');
			c &= 0x7F;
		}
		if (c < 0x20) {
			putchar('^');
			putchar(c + '@');
		} else if (c == 0x7F) {
			putchar('^');
			putchar('?');
		} else
			putchar(c);
	}
	fflush(stdout);
}

main(argc, argv)
	char **argv;
{
	char buf[BUFSIZ];
	fd_set fds, fds1;
	register int i, cc, max;

	if (argc != 3) {
		fprintf(stderr, "usage: %s ttyname baudrate\n", argv[0]);
		exit(1);
	}
	open_target_serial(argv[1], argv[2]);
	set_serial_nonblock(0);
	FD_ZERO(&fds);
	FD_SET(0, &fds);
	FD_SET(target_fd, &fds);
	max = target_fd + 1;
	for (;;) {
		bcopy(&fds, &fds1, sizeof(fd_set));
		i = select(max, &fds1, NULL, NULL, NULL);
		if (i < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			exit(1);
		}
		if (FD_ISSET(0, &fds1)) {
			cc = read(0, buf, sizeof buf);
			if (cc <= 0)
				exit(0);
			write(target_fd, buf, cc);
		}
		if (FD_ISSET(target_fd, &fds1)) {
			cc = read(target_fd, buf, sizeof buf);
			if (cc <= 0) {
				fprintf(stderr, "EOF/error on target tty\n");
				exit(1);
			}
			safe_output(buf, cc);
		}
	}
}