Browse Source

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
tags/ethstream-1.1
zacharyc 12 years ago
parent
commit
407a91c764
3 changed files with 85 additions and 23 deletions
  1. +42
    -11
      ethstream.c
  2. +8
    -0
      ethstream.h
  3. +35
    -12
      nerdjack.c

+ 42
- 11
ethstream.c View File

@@ -29,11 +29,13 @@
#include "opt.h"
#include "version.h"
#include "compat.h"
#include "ethstream.h"

#define DEFAULT_HOST "192.168.1.209"
#define UE9_COMMAND_PORT 52360
#define UE9_DATA_PORT 52361


struct callbackInfo {
struct ue9Calibration calib;
int convert;
@@ -50,7 +52,8 @@ struct options opt[] = {
{ 'r', "rate", "hz", "sample each channel at this rate (8000.0)" },
{ 'o', "oneshot", NULL, "don't retry in case of errors" },
{ '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" },
{ 'h', "help", NULL, "this help" },
{ 'v', "verbose", NULL, "be verbose" },
@@ -87,7 +90,7 @@ int main(int argc, char *argv[])
double actual_rate;
int oneshot = 0;
int forceretry = 0;
int convert = 0;
int convert = CONVERT_DEC;
uint8_t scanconfig;
uint16_t scaninterval;
#if UE9_CHANNELS > NERDJACK_CHANNELS
@@ -173,7 +176,18 @@ int main(int argc, char *argv[])
forceretry++;
break;
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;
case 'v':
verb_count++;
@@ -352,7 +366,7 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int
if (nerd_send_command(address,command) < 0) {
if (first_call)
retval = -ENOTCONN;
info("Failed to send command\n");
info("Failed to send GET command\n");
goto out;
}
first_call = 0;
@@ -464,17 +478,30 @@ int data_callback(int channels, uint16_t *data, void *context)

columns_left = channels;
for (i = 0; i < channels; i++) {
if (ci->convert)
printf("%lf", ue9_binary_to_analog(
switch (ci->convert) {
case CONVERT_VOLTS:
if (printf("%lf", ue9_binary_to_analog(
&ci->calib, UE9_BIPOLAR_GAIN1, 12,
data[i]));
else
printf("%d", data[i]);
data[i])) < 0)
goto bad;
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--;
if (i < (channels - 1)) {
putchar(' ');
if (ci->convert != CONVERT_HEX && putchar(' ') < 0)
goto bad;
} else {
putchar('\n');
if (putchar('\n') < 0)
goto bad;
lines++;
if (ci->maxlines && lines >= ci->maxlines)
return -1;
@@ -482,4 +509,8 @@ int data_callback(int channels, uint16_t *data, void *context)
}
return 0;

bad:
info("Output error (disk full?)\n");
return -1;
}

+ 8
- 0
ethstream.h View File

@@ -0,0 +1,8 @@
#ifndef ETHSTREAM_H
#define ETHSTREAM_H

#define CONVERT_DEC 0
#define CONVERT_VOLTS 1
#define CONVERT_HEX 2

#endif

+ 35
- 12
nerdjack.c View File

@@ -23,6 +23,7 @@
#include "nerdjack.h"
#include "util.h"
#include "netutil.h"
#include "ethstream.h"

#define NERDJACK_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */

@@ -73,7 +74,7 @@ int nerdjack_detect(char * ipAddress) {
if((-1 == sock) || (-1 == receivesock)) /* if socket failed to initialize, exit */
{
printf("Error Creating Socket\n");
verb("Error Creating Socket\n");
return -1;
}

@@ -93,7 +94,7 @@ int nerdjack_detect(char * ipAddress) {

bytes_sent = sendto(sock, buffer, buffer_length, 0,(struct sockaddr*) &sa, sizeof(struct sockaddr_in) );
if(bytes_sent < 0) {
printf("Error sending packet: %s\n", strerror(errno) );
info("Error sending packet: %s\n", strerror(errno) );
return -1;
}

@@ -240,14 +241,14 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis

//First check the header info
if(buf[0] != 0xF0 || buf[1] != 0xAA) {
printf("No Header info\n");
info("No Header info\n");
return -1;
}

//Check counter info to make sure not out of order
tempshort = (buf[2] << 8) | buf[3];
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;
}
@@ -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(charsread > index) {
datapoint = (buf[index] << 8 | buf[index+1]);
if(convert) {
switch(convert) {
case CONVERT_VOLTS:
if(alignment <= 5) {
volts = (long double) ( datapoint / 32767.0 ) * ((precision & 0x01) ? 5.0 : 10.0);
} else {
@@ -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++) {
voltline[destination[alignment].destlist[i]] = volts;
}
} else {
break;
default:
case CONVERT_HEX:
case CONVERT_DEC:
for(i = 0; i < destination[alignment].numCopies; i++) {
dataline[destination[alignment].destlist[i]] =
(unsigned short) (datapoint - INT16_MIN);
}
break;
}
//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
//Since channel data is packed, we need to know when to insert a newline
if(alignment == numChannelsSampled){
if(convert) {
switch(convert) {
case CONVERT_VOLTS:
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++) {
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;
numgroups++;
if(lines != 0) {
@@ -321,6 +340,10 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis

return 0;

bad:
info("Output error (disk full?)\n");
return -1;

}

int nerd_open(const char *address,int port) {
@@ -333,7 +356,7 @@ int nerd_open(const char *address,int port) {

if(-1 == i32SocketFD)
{
printf("cannot create socket");
verb("cannot create socket");
return -1;
}


Loading…
Cancel
Save