diff cmu200/sercmd.c @ 0:bd62be88259d

initial import of rfcal code and docs from freecalypso-tools repository
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 May 2017 18:49:35 +0000
parents
children aeffe53e110d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmu200/sercmd.c	Sat May 20 18:49:35 2017 +0000
@@ -0,0 +1,58 @@
+/*
+ * This module contains the functions that send serial commands to the CMU200
+ * and collect the instrument's serial responses.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <strings.h>
+
+extern int target_fd;
+
+char instrument_response[4096];
+
+send_scpi_cmd(cmd)
+	char *cmd;
+{
+	printf("Command to CMU: %s", cmd);
+	write(target_fd, cmd, strlen(cmd));
+}
+
+collect_instr_response()
+{
+	char buf[BUFSIZ];
+	int cc, pos;
+
+	for (pos = 0; ; ) {
+		cc = read(target_fd, buf, sizeof buf);
+		if (cc <= 0) {
+			perror("error reading from serial port");
+			exit(1);
+		}
+		if (pos + cc > sizeof instrument_response) {
+			fprintf(stderr,
+		"error: response from CMU200 exceeds our buffer size\n");
+			exit(1);
+		}
+		bcopy(buf, instrument_response + pos, cc);
+		pos += cc;
+		if (instrument_response[pos-1] == '\n')
+			break;
+	}
+	instrument_response[pos-1] = '\0';
+	printf("Instrument response: %s\n", instrument_response);
+}
+
+collect_staropc_response()
+{
+	collect_instr_response();
+	if (instrument_response[0] != '1' ||
+	    instrument_response[1] && !isspace(instrument_response[1])) {
+		fprintf(stderr, "error: unexpected response to *OPC?\n");
+		exit(1);
+	}
+}