Commit 4cccb783 by zacharyc

Fixed up "range" argument

Took Jim's suggestions about addressing and device options
Made autodetection a better fallback.


git-svn-id: https://bucket.mit.edu/svn/nilm/acquisition/ethstream@7450 ddd99763-3ecb-0310-9145-efcb8ce7c51f
parent 5e7f4ac9
Showing with 101 additions and 24 deletions
...@@ -44,12 +44,13 @@ struct callbackInfo ...@@ -44,12 +44,13 @@ struct callbackInfo
}; };
struct options opt[] = { struct options opt[] = {
{'a', "address", "string", "host/address of UE9 (192.168.1.209)"}, {'a', "address", "string", "host/address of device (192.168.1.209)"},
{'n', "numchannels", "n", "sample the first N ADC channels (2)"}, {'n', "numchannels", "n", "sample the first N ADC channels (2)"},
{'N', "nerdjack", NULL, "Use NerdJack device instead"}, {'N', "nerdjack", NULL, "Force NerdJack device"},
{'L', "labjack",NULL,"Force LabJack device"},
{'d', "detect", NULL, "Detect NerdJack IP address"}, {'d', "detect", NULL, "Detect NerdJack IP address"},
{'p', "precision", "0-3", {'R', "range", "a,b",
"Set precision on NerdJack (0 - max range, 1 - max precision)"}, "Set range on NerdJack for channels 0-5,6-11 to either 5 or 10 (10,10)"},
{'C', "channels", "a,b,c", "sample channels a, b, and c"}, {'C', "channels", "a,b,c", "sample channels a, b, and c"},
{'r', "rate", "hz", "sample each channel at this rate (8000.0)"}, {'r', "rate", "hz", "sample each channel at this rate (8000.0)"},
{'o', "oneshot", NULL, "don't retry in case of errors"}, {'o', "oneshot", NULL, "don't retry in case of errors"},
...@@ -109,8 +110,11 @@ main (int argc, char *argv[]) ...@@ -109,8 +110,11 @@ main (int argc, char *argv[])
#endif #endif
int channel_count = 0; int channel_count = 0;
int nerdjack = 0; int nerdjack = 0;
int labjack = 0;
int detect = 0; int detect = 0;
int precision = 0; int precision = 0;
int addressSpecified = 0;
int donerdjack = 0;
unsigned long period = NERDJACK_CLOCK_RATE / desired_rate; unsigned long period = NERDJACK_CLOCK_RATE / desired_rate;
/* Parse arguments */ /* Parse arguments */
...@@ -122,6 +126,7 @@ main (int argc, char *argv[]) ...@@ -122,6 +126,7 @@ main (int argc, char *argv[])
case 'a': case 'a':
free (address); free (address);
address = strdup (optarg); address = strdup (optarg);
addressSpecified = 1;
break; break;
case 'n': case 'n':
channel_count = 0; channel_count = 0;
...@@ -179,21 +184,41 @@ main (int argc, char *argv[]) ...@@ -179,21 +184,41 @@ main (int argc, char *argv[])
goto printhelp; goto printhelp;
} }
break; break;
case 'p': case 'R':
tmp = strtol (optarg, &endp, 0); tmp = strtol (optarg, &endp, 0);
if (tmp <= 3 && tmp >= 0) if (*endp != ',')
{ {
precision = tmp; info ("bad range number: %s\n", optarg);
goto printhelp;
} }
else if(tmp != 5 && tmp != 10) {
{ info("valid choices for range are 5 or 10\n");
info ("Bad argument to p: %s\n", optarg); goto printhelp;
}
if(tmp == 5) precision = precision + 1;
optarg = endp + 1;
if (*endp == '\0') {
info("Range needs two numbers, one for channels 0-5 and another for 6-11\n");
goto printhelp;
}
tmp = strtol (optarg, &endp, 0);
if (*endp != '\0') {
info("Range needs only two numbers, one for channels 0-5 and another for 6-11\n");
goto printhelp; goto printhelp;
} }
if(tmp != 5 && tmp != 10) {
info("valid choices for range are 5 or 10\n");
goto printhelp;
}
if(tmp == 5) precision = precision + 2;
break; break;
case 'N': case 'N':
nerdjack++; nerdjack++;
break; break;
case 'L':
labjack++;
break;
case 'd': case 'd':
detect++; detect++;
break; break;
...@@ -244,9 +269,40 @@ main (int argc, char *argv[]) ...@@ -244,9 +269,40 @@ main (int argc, char *argv[])
} }
} }
if (detect && labjack) {
info("The LabJack does not support autodetection\n");
goto printhelp;
}
if (detect && !nerdjack) {
info("Only the NerdJack supports autodetection - assuming -N option\n");
nerdjack = 1;
}
if (detect && addressSpecified) {
info("Autodetection and specifying address are mutually exclusive\n");
goto printhelp;
}
if (nerdjack && labjack) {
info("Nerdjack and Labjack options are mutually exclusive\n");
goto printhelp;
}
donerdjack = nerdjack;
//First if no options were supplied try the Nerdjack
//The second time through, donerdjack will be true and this will not fire
if (!nerdjack && !labjack) {
info("No device specified...Defaulting to Nerdjack\n");
donerdjack = 1;
}
doneparse: doneparse:
if (nerdjack)
if (donerdjack)
{ {
if (channel_count > NERDJACK_CHANNELS) if (channel_count > NERDJACK_CHANNELS)
{ {
...@@ -310,7 +366,7 @@ doneparse: ...@@ -310,7 +366,7 @@ doneparse:
} }
/* Figure out actual rate. */ /* Figure out actual rate. */
if (nerdjack) if (donerdjack)
{ {
if (nerdjack_choose_scan (desired_rate, &actual_rate, &period) < 0) if (nerdjack_choose_scan (desired_rate, &actual_rate, &period) < 0)
{ {
...@@ -351,6 +407,7 @@ doneparse: ...@@ -351,6 +407,7 @@ doneparse:
if (nerdjack_detect (address) < 0) if (nerdjack_detect (address) < 0)
{ {
info ("Error with autodetection\n"); info ("Error with autodetection\n");
goto printhelp;
} }
else else
{ {
...@@ -362,7 +419,7 @@ doneparse: ...@@ -362,7 +419,7 @@ doneparse:
for (;;) for (;;)
{ {
int ret; int ret;
if (nerdjack) if (donerdjack)
{ {
ret = ret =
nerdDoStream (address, channel_list, channel_count, precision, nerdDoStream (address, channel_list, channel_count, precision,
...@@ -382,10 +439,27 @@ doneparse: ...@@ -382,10 +439,27 @@ doneparse:
if (ret == 0) if (ret == 0)
break; break;
if (ret == -ENOTCONN && !nerdjack) //Neither options specified at command line and first time through.
//Try LabJack
if (ret == -ENOTCONN && donerdjack && !labjack && !nerdjack)
{ {
info ("Could not connect LabJack...Trying NerdJack\n"); info ("Could not connect NerdJack...Trying LabJack\n");
nerdjack = 1; donerdjack = 0;
goto doneparse;
}
//Neither option supplied, no address, and second time through.
//Try autodetection
if (ret == -ENOTCONN && !donerdjack && !labjack && !nerdjack && !addressSpecified) {
info ("Could not connect LabJack...Trying to autodetect Nerdjack\n");
detect = 1;
donerdjack = 1;
goto doneparse;
}
if (ret == -ENOTCONN && nerdjack && !detect && !addressSpecified) {
info ("Could not reach NerdJack...Trying to autodetect\n");
detect = 1;
goto doneparse; goto doneparse;
} }
...@@ -439,9 +513,12 @@ tryagain: ...@@ -439,9 +513,12 @@ tryagain:
if (nerd_send_command (address, "STOP", 4) < 0) if (nerd_send_command (address, "STOP", 4) < 0)
{ {
if (first_call) if (first_call) {
retval = -ENOTCONN; retval = -ENOTCONN;
if(verb_count) info("Failed to send STOP command\n");
} else {
info ("Failed to send STOP command\n"); info ("Failed to send STOP command\n");
}
goto out; goto out;
} }
......
...@@ -231,7 +231,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list, ...@@ -231,7 +231,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
} }
long double voltline[numChannels]; double voltline[numChannels];
unsigned short dataline[numChannels]; unsigned short dataline[numChannels];
...@@ -241,7 +241,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list, ...@@ -241,7 +241,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
int charsread = 0; int charsread = 0;
int numgroupsProcessed = 0; int numgroupsProcessed = 0;
long double volts; double volts;
//The timeout should be the expected time plus 60 seconds //The timeout should be the expected time plus 60 seconds
//This permits slower speeds to work properly //This permits slower speeds to work properly
...@@ -328,7 +328,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list, ...@@ -328,7 +328,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
switch (convert) switch (convert)
{ {
case CONVERT_VOLTS: case CONVERT_VOLTS:
memset (voltline, 0, numChannels * sizeof (long double)); memset (voltline, 0, numChannels * sizeof (double));
break; break;
default: default:
case CONVERT_HEX: case CONVERT_HEX:
...@@ -352,13 +352,13 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list, ...@@ -352,13 +352,13 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
if (channel_list[i] <= 5) if (channel_list[i] <= 5)
{ {
volts = volts =
(long double) (datapoint / 32767.0) * (double) (datapoint / 32767.0) *
((precision & 0x01) ? 5.0 : 10.0); ((precision & 0x01) ? 5.0 : 10.0);
} }
else else
{ {
volts = volts =
(long double) (datapoint / 32767.0) * (double) (datapoint / 32767.0) *
((precision & 0x02) ? 5.0 : 10.0); ((precision & 0x02) ? 5.0 : 10.0);
} }
voltline[i] = volts; voltline[i] = volts;
...@@ -379,7 +379,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list, ...@@ -379,7 +379,7 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
case CONVERT_VOLTS: case CONVERT_VOLTS:
for (i = 0; i < numChannels; i++) for (i = 0; i < numChannels; i++)
{ {
if (printf ("%Lf ", voltline[i]) < 0) if (printf ("%lf ", voltline[i]) < 0)
goto bad; goto bad;
} }
break; break;
...@@ -477,7 +477,7 @@ nerd_open (const char *address, int port) ...@@ -477,7 +477,7 @@ nerd_open (const char *address, int port)
(i32SocketFD, (struct sockaddr *) &stSockAddr, sizeof (stSockAddr), (i32SocketFD, (struct sockaddr *) &stSockAddr, sizeof (stSockAddr),
&(struct timeval) &(struct timeval)
{ {
.tv_sec = NERDJACK_TIMEOUT}) < 0) .tv_sec = 3}) < 0)
{ {
verb ("connection to %s:%d failed: %s\n", verb ("connection to %s:%d failed: %s\n",
inet_ntoa (stSockAddr.sin_addr), port, compat_strerror (errno)); inet_ntoa (stSockAddr.sin_addr), port, compat_strerror (errno));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment