Commit 407a91c7 by zacharyc

Merged Jim's changes to ljstream. This now should respect disk full errors

and allow hex output


git-svn-id: https://bucket.mit.edu/svn/nilm/acquisition/ethstream@7214 ddd99763-3ecb-0310-9145-efcb8ce7c51f
parent 1d760e03
Showing with 85 additions and 23 deletions
...@@ -29,11 +29,13 @@ ...@@ -29,11 +29,13 @@
#include "opt.h" #include "opt.h"
#include "version.h" #include "version.h"
#include "compat.h" #include "compat.h"
#include "ethstream.h"
#define DEFAULT_HOST "192.168.1.209" #define DEFAULT_HOST "192.168.1.209"
#define UE9_COMMAND_PORT 52360 #define UE9_COMMAND_PORT 52360
#define UE9_DATA_PORT 52361 #define UE9_DATA_PORT 52361
struct callbackInfo { struct callbackInfo {
struct ue9Calibration calib; struct ue9Calibration calib;
int convert; int convert;
...@@ -50,7 +52,8 @@ struct options opt[] = { ...@@ -50,7 +52,8 @@ struct options opt[] = {
{ '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" },
{ 'f', "forceretry", NULL, "retry no matter what happens" }, { 'f', "forceretry", NULL, "retry no matter what happens" },
{ 'c', "convert", NULL, "display output in volts" }, { 'c', "convert", NULL, "convert output to volts" },
{ 'H', "converthex", NULL, "convert output to hex" },
{ 'l', "lines", "num", "if set, output this many lines and quit" }, { 'l', "lines", "num", "if set, output this many lines and quit" },
{ 'h', "help", NULL, "this help" }, { 'h', "help", NULL, "this help" },
{ 'v', "verbose", NULL, "be verbose" }, { 'v', "verbose", NULL, "be verbose" },
...@@ -87,7 +90,7 @@ int main(int argc, char *argv[]) ...@@ -87,7 +90,7 @@ int main(int argc, char *argv[])
double actual_rate; double actual_rate;
int oneshot = 0; int oneshot = 0;
int forceretry = 0; int forceretry = 0;
int convert = 0; int convert = CONVERT_DEC;
uint8_t scanconfig; uint8_t scanconfig;
uint16_t scaninterval; uint16_t scaninterval;
#if UE9_CHANNELS > NERDJACK_CHANNELS #if UE9_CHANNELS > NERDJACK_CHANNELS
...@@ -173,7 +176,18 @@ int main(int argc, char *argv[]) ...@@ -173,7 +176,18 @@ int main(int argc, char *argv[])
forceretry++; forceretry++;
break; break;
case 'c': case 'c':
convert++; if (convert != 0) {
info("specify only one conversion type\n");
goto printhelp;
}
convert = CONVERT_VOLTS;
break;
case 'H':
if (convert != 0) {
info("specify only one conversion type\n");
goto printhelp;
}
convert = CONVERT_HEX;
break; break;
case 'v': case 'v':
verb_count++; verb_count++;
...@@ -352,7 +366,7 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int ...@@ -352,7 +366,7 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int
if (nerd_send_command(address,command) < 0) { if (nerd_send_command(address,command) < 0) {
if (first_call) if (first_call)
retval = -ENOTCONN; retval = -ENOTCONN;
info("Failed to send command\n"); info("Failed to send GET command\n");
goto out; goto out;
} }
first_call = 0; first_call = 0;
...@@ -464,17 +478,30 @@ int data_callback(int channels, uint16_t *data, void *context) ...@@ -464,17 +478,30 @@ int data_callback(int channels, uint16_t *data, void *context)
columns_left = channels; columns_left = channels;
for (i = 0; i < channels; i++) { for (i = 0; i < channels; i++) {
if (ci->convert) switch (ci->convert) {
printf("%lf", ue9_binary_to_analog( case CONVERT_VOLTS:
if (printf("%lf", ue9_binary_to_analog(
&ci->calib, UE9_BIPOLAR_GAIN1, 12, &ci->calib, UE9_BIPOLAR_GAIN1, 12,
data[i])); data[i])) < 0)
else goto bad;
printf("%d", data[i]); break;
case CONVERT_HEX:
if (printf("%04X", data[i]) < 0)
goto bad;
break;
default:
case CONVERT_DEC:
if (printf("%d", data[i]) < 0)
goto bad;
break;
}
columns_left--; columns_left--;
if (i < (channels - 1)) { if (i < (channels - 1)) {
putchar(' '); if (ci->convert != CONVERT_HEX && putchar(' ') < 0)
goto bad;
} else { } else {
putchar('\n'); if (putchar('\n') < 0)
goto bad;
lines++; lines++;
if (ci->maxlines && lines >= ci->maxlines) if (ci->maxlines && lines >= ci->maxlines)
return -1; return -1;
...@@ -482,4 +509,8 @@ int data_callback(int channels, uint16_t *data, void *context) ...@@ -482,4 +509,8 @@ int data_callback(int channels, uint16_t *data, void *context)
} }
return 0; return 0;
bad:
info("Output error (disk full?)\n");
return -1;
} }
#ifndef ETHSTREAM_H
#define ETHSTREAM_H
#define CONVERT_DEC 0
#define CONVERT_VOLTS 1
#define CONVERT_HEX 2
#endif
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "nerdjack.h" #include "nerdjack.h"
#include "util.h" #include "util.h"
#include "netutil.h" #include "netutil.h"
#include "ethstream.h"
#define NERDJACK_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */ #define NERDJACK_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */
...@@ -73,7 +74,7 @@ int nerdjack_detect(char * ipAddress) { ...@@ -73,7 +74,7 @@ int nerdjack_detect(char * ipAddress) {
if((-1 == sock) || (-1 == receivesock)) /* if socket failed to initialize, exit */ if((-1 == sock) || (-1 == receivesock)) /* if socket failed to initialize, exit */
{ {
printf("Error Creating Socket\n"); verb("Error Creating Socket\n");
return -1; return -1;
} }
...@@ -93,7 +94,7 @@ int nerdjack_detect(char * ipAddress) { ...@@ -93,7 +94,7 @@ int nerdjack_detect(char * ipAddress) {
bytes_sent = sendto(sock, buffer, buffer_length, 0,(struct sockaddr*) &sa, sizeof(struct sockaddr_in) ); bytes_sent = sendto(sock, buffer, buffer_length, 0,(struct sockaddr*) &sa, sizeof(struct sockaddr_in) );
if(bytes_sent < 0) { if(bytes_sent < 0) {
printf("Error sending packet: %s\n", strerror(errno) ); info("Error sending packet: %s\n", strerror(errno) );
return -1; return -1;
} }
...@@ -240,14 +241,14 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -240,14 +241,14 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
//First check the header info //First check the header info
if(buf[0] != 0xF0 || buf[1] != 0xAA) { if(buf[0] != 0xF0 || buf[1] != 0xAA) {
printf("No Header info\n"); info("No Header info\n");
return -1; return -1;
} }
//Check counter info to make sure not out of order //Check counter info to make sure not out of order
tempshort = (buf[2] << 8) | buf[3]; tempshort = (buf[2] << 8) | buf[3];
if(tempshort != currentcount ){ if(tempshort != currentcount ){
printf("Count wrong. Expected %hd but got %hd\n", currentcount, tempshort); info("Count wrong. Expected %hd but got %hd\n", currentcount, tempshort);
return -1; return -1;
} }
...@@ -265,7 +266,8 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -265,7 +266,8 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
//While there is still more data in the packet, process it //While there is still more data in the packet, process it
while(charsread > index) { while(charsread > index) {
datapoint = (buf[index] << 8 | buf[index+1]); datapoint = (buf[index] << 8 | buf[index+1]);
if(convert) { switch(convert) {
case CONVERT_VOLTS:
if(alignment <= 5) { if(alignment <= 5) {
volts = (long double) ( datapoint / 32767.0 ) * ((precision & 0x01) ? 5.0 : 10.0); volts = (long double) ( datapoint / 32767.0 ) * ((precision & 0x01) ? 5.0 : 10.0);
} else { } else {
...@@ -274,11 +276,15 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -274,11 +276,15 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
for(i = 0; i < destination[alignment].numCopies; i++) { for(i = 0; i < destination[alignment].numCopies; i++) {
voltline[destination[alignment].destlist[i]] = volts; voltline[destination[alignment].destlist[i]] = volts;
} }
} else { break;
default:
case CONVERT_HEX:
case CONVERT_DEC:
for(i = 0; i < destination[alignment].numCopies; i++) { for(i = 0; i < destination[alignment].numCopies; i++) {
dataline[destination[alignment].destlist[i]] = dataline[destination[alignment].destlist[i]] =
(unsigned short) (datapoint - INT16_MIN); (unsigned short) (datapoint - INT16_MIN);
} }
break;
} }
//Each point is two bytes, so increment index and total bytes read //Each point is two bytes, so increment index and total bytes read
...@@ -292,16 +298,29 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -292,16 +298,29 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
//Since channel data is packed, we need to know when to insert a newline //Since channel data is packed, we need to know when to insert a newline
if(alignment == numChannelsSampled){ if(alignment == numChannelsSampled){
if(convert) { switch(convert) {
case CONVERT_VOLTS:
for(i = 0; i < numChannels; i++) { for(i = 0; i < numChannels; i++) {
printf("%Lf ",voltline[i]); if (printf("%Lf ",voltline[i]) < 0)
goto bad;
} }
} else { break;
case CONVERT_HEX:
for(i = 0; i < numChannels; i++) { for(i = 0; i < numChannels; i++) {
printf("%hu ",dataline[i]); if (printf("%04hX",dataline[i]) < 0)
goto bad;
} }
break;
default:
case CONVERT_DEC:
for(i = 0; i < numChannels; i++) {
if (printf("%hu ",dataline[i]) < 0)
goto bad;
}
break;
} }
printf("\n"); if(printf("\n") < 0)
goto bad;
alignment = 0; alignment = 0;
numgroups++; numgroups++;
if(lines != 0) { if(lines != 0) {
...@@ -321,6 +340,10 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -321,6 +340,10 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
return 0; return 0;
bad:
info("Output error (disk full?)\n");
return -1;
} }
int nerd_open(const char *address,int port) { int nerd_open(const char *address,int port) {
...@@ -333,7 +356,7 @@ int nerd_open(const char *address,int port) { ...@@ -333,7 +356,7 @@ int nerd_open(const char *address,int port) {
if(-1 == i32SocketFD) if(-1 == i32SocketFD)
{ {
printf("cannot create socket"); verb("cannot create socket");
return -1; return -1;
} }
......
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