Browse Source

Process ADC+DAC packets from the Zoom Nilm board and dump it out.

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@5935 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 16 years ago
parent
commit
9ecacf9ad9
1 changed files with 66 additions and 16 deletions
  1. +66
    -16
      pc/read.c

+ 66
- 16
pc/read.c View File

@@ -6,12 +6,18 @@
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <stdint.h>
#include <string.h>
#include <syslog.h>
#include <fcntl.h>
#include <err.h>
#include <linux/serial.h>

int hex = 0;
int screen = 0;

int process(const uint8_t *buf, int len);

int serial_open(const char *device, int rate)
{
struct termios options;
@@ -35,7 +41,8 @@ int serial_open(const char *device, int rate)
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
err(1, "TIOCGSERIAL 2");
if (serinfo.custom_divisor * rate != serinfo.baud_base) {
warnx("actual baudrate is %f",
warnx("actual baudrate is %d / %d = %f",
serinfo.baud_base, serinfo.custom_divisor,
(float)serinfo.baud_base / serinfo.custom_divisor);
}

@@ -54,24 +61,27 @@ int serial_open(const char *device, int rate)

int main(int argc, char *argv[])
{
char *device=strdup("/dev/ttyUSB3");
int rate=1000000;
char *device=strdup("/dev/ttyUSB0");
int rate=500000;
int fd;
int getopt_index;
int i;
char buf[1024];
int len;

static struct option long_opts[] = {
{ "device", required_argument, NULL, 'd' },
{ "rate", required_argument, NULL, 'r' },
{ "hex", no_argument, NULL, 'x' },
{ "screen", no_argument, NULL, 's' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
int help=0;
char c;

while((c=getopt_long(argc,argv,"d:r:h?",
long_opts,&getopt_index))!=-1) {
while ((c = getopt_long(argc, argv, "d:r:xsh?",
long_opts, &getopt_index)) != -1) {
switch(c)
{
case 'd':
@@ -81,7 +91,13 @@ int main(int argc, char *argv[])
case 'r':
rate = atoi(optarg);
if(rate == 0)
errx(1,"invalid rate: %s",optarg);
errx(1, "invalid rate: %s",optarg);
break;
case 'x':
hex = 1;
break;
case 's':
screen = 1;
break;
case 'h':
case '?':
@@ -91,25 +107,59 @@ int main(int argc, char *argv[])
}
}
if(help) {
fprintf(stderr,"Zoom Nilm Client\n");
fprintf(stderr,"usage: %s [options]\n\n",*argv);
fprintf(stderr," -d, --device /dev/ttyUSB3 serial port\n");
fprintf(stderr," -r, --rate 1000000 baud rate\n");
fprintf(stderr," -h, --help this cruft\n");
if (help) {
fprintf(stderr, "Zoom Nilm Client\n");
fprintf(stderr, "usage: %s [options]\n\n", *argv);
fprintf(stderr, " -d, --device /dev/ttyUSB3 serial port\n");
fprintf(stderr, " -r, --rate 1000000 baud rate\n");
fprintf(stderr, " -x, --hex hex out\n");
fprintf(stderr, " -s, --screen send \\r instead of \\n\n");
fprintf(stderr, " -h, --help this cruft\n");
return 1;
}

if ((fd = serial_open(device, rate)) == -1)
errx(1, "serial_open failed");

for (i = 0; i < sizeof(buf); i++)
buf[i] = 'U';
len = 0;
while (1) {
write(fd, buf, sizeof(buf));
int processed, n;
n = read(fd, buf + len, sizeof(buf) - len);
if (n <= 0)
err(1, "read");
len += n;
processed = process(buf, len);
memmove(buf, buf + processed, len - processed);
len -= processed;
}
///////////////////////
return 1;
}

int process(const uint8_t *buf, int len)
{
int n = 0;
uint16_t dac, adc;

/* Process blocks */
retry:
for (; (n + 4) <= len; buf += 4, n += 4) {
if ((!buf[0] & 0x80) ||
(buf[1] & 0x80) ||
(buf[2] & 0x80) ||
(buf[3] & 0x80)) {
/* badly formed data, eat one byte and retry */
buf++;
n++;
goto retry;
}

adc = ((buf[0] & 0x7f) << 5) |
((buf[1] & 0x7c) >> 2);
dac = ((buf[1] & 0x03) << 14) |
(buf[2] << 7) | buf[3];
printf(hex ? "%04x %04x%c" : "%d %d%c", dac, adc, screen ? '\r' : '\n');
}
return n;
}

Loading…
Cancel
Save