Compare commits
33 Commits
ethstream-
...
master
Author | SHA1 | Date | |
---|---|---|---|
42d83649be | |||
df740e3e00 | |||
c2f43c474b | |||
893debb361 | |||
![]() |
0b1ad7415d | ||
![]() |
ba2565dc0e | ||
35238c57d0 | |||
e1cb6b6ee0 | |||
![]() |
7262486a36 | ||
![]() |
0f71b79b6f | ||
![]() |
d6f01a3497 | ||
![]() |
76143e7eb4 | ||
![]() |
3ba616841e | ||
![]() |
6bc683fb21 | ||
![]() |
640edf6aab | ||
![]() |
e2bf34ed0c | ||
![]() |
56a79c00f6 | ||
![]() |
bb4912a43c | ||
![]() |
16bf9f89ef | ||
![]() |
267e0e32a5 | ||
![]() |
588385bcb1 | ||
![]() |
ced565f372 | ||
![]() |
e6e9d32a54 | ||
![]() |
9cdb4ea77c | ||
![]() |
53ddf346d7 | ||
![]() |
b0be6473b8 | ||
![]() |
9353f2b3f2 | ||
![]() |
b697801e3e | ||
![]() |
5a14055029 | ||
![]() |
25bed4b644 | ||
![]() |
cd63e2fb11 | ||
![]() |
329871bf56 | ||
![]() |
f4f809eee3 |
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
ethstream
|
||||||
|
ethstream.1
|
||||||
|
ethstream.txt
|
||||||
|
ethstream.exe
|
||||||
|
*.obj
|
||||||
|
*.d
|
||||||
|
*.dobj
|
||||||
|
*.o
|
||||||
|
|
||||||
|
|
31
Makefile
31
Makefile
|
@ -14,12 +14,15 @@
|
||||||
# Build options
|
# Build options
|
||||||
|
|
||||||
CFLAGS += -Wall -g #-pg
|
CFLAGS += -Wall -g #-pg
|
||||||
LDFLAGS += -lm #-pg
|
LDFLAGS += #-pg
|
||||||
|
LDLIBS += -lm
|
||||||
|
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
MANPATH = ${PREFIX}/man/man1
|
MANPATH = ${PREFIX}/man/man1/
|
||||||
BINPATH = ${PREFIX}/bin
|
BINPATH = ${PREFIX}/bin
|
||||||
|
|
||||||
WINCC = i386-mingw32-gcc
|
#WINCC = i386-mingw32-gcc
|
||||||
|
WINCC = i586-mingw32msvc-gcc
|
||||||
WINCFLAGS += $(CFLAGS)
|
WINCFLAGS += $(CFLAGS)
|
||||||
WINLDFLAGS += $(LDFLAGS) -lws2_32 -liphlpapi -s
|
WINLDFLAGS += $(LDFLAGS) -lws2_32 -liphlpapi -s
|
||||||
|
|
||||||
|
@ -32,11 +35,10 @@ default: lin
|
||||||
all: lin win
|
all: lin win
|
||||||
|
|
||||||
.PHONY: lin
|
.PHONY: lin
|
||||||
lin: ljtest ethstream ljconfig \
|
lin: ethstream ethstream.1 ethstream.txt
|
||||||
ethstream.1 ljconfig.1
|
|
||||||
|
|
||||||
.PHONY: win
|
.PHONY: win
|
||||||
win: ljtest.exe ethstream.exe ljconfig.exe
|
win: ethstream.exe
|
||||||
|
|
||||||
|
|
||||||
version.h: VERSION
|
version.h: VERSION
|
||||||
|
@ -46,17 +48,12 @@ version.h: VERSION
|
||||||
# Object files for each executable
|
# Object files for each executable
|
||||||
|
|
||||||
obj-common = opt.o ue9.o ue9error.o netutil.o debug.o nerdjack.o
|
obj-common = opt.o ue9.o ue9error.o netutil.o debug.o nerdjack.o
|
||||||
obj-ljconfig = ljconfig.o $(obj-common)
|
|
||||||
obj-ethstream = ethstream.o $(obj-common)
|
obj-ethstream = ethstream.o $(obj-common)
|
||||||
obj-ljtest = ljtest.o $(obj-common)
|
|
||||||
|
|
||||||
ljconfig: $(obj-ljconfig)
|
|
||||||
ethstream: $(obj-ethstream)
|
ethstream: $(obj-ethstream)
|
||||||
ljtest: $(obj-ljtest)
|
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
|
||||||
|
|
||||||
ljconfig.exe: $(obj-ljconfig:.o=.obj) compat-win32.obj
|
|
||||||
ethstream.exe: $(obj-ethstream:.o=.obj) compat-win32.obj
|
ethstream.exe: $(obj-ethstream:.o=.obj) compat-win32.obj
|
||||||
ljtest.exe: $(obj-ljtest:.o=.obj) compat-win32.obj
|
|
||||||
|
|
||||||
# Manpages
|
# Manpages
|
||||||
|
|
||||||
|
@ -71,6 +68,7 @@ ljtest.exe: $(obj-ljtest:.o=.obj) compat-win32.obj
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install: ethstream.1 ethstream
|
install: ethstream.1 ethstream
|
||||||
|
mkdir -p ${BINPATH} ${MANPATH}
|
||||||
install -m 0755 ethstream ${BINPATH}
|
install -m 0755 ethstream ${BINPATH}
|
||||||
install -m 0644 ethstream.1 ${MANPATH}
|
install -m 0644 ethstream.1 ${MANPATH}
|
||||||
|
|
||||||
|
@ -92,16 +90,19 @@ dist: version.h
|
||||||
|
|
||||||
.PHONY: clean distclean
|
.PHONY: clean distclean
|
||||||
clean distclean:
|
clean distclean:
|
||||||
rm -f *.o *.obj *.exe ethstream ljtest ljconfig core *.d *.1 *.txt
|
rm -f *.o *.obj *.exe ethstream core *.d *.dobj *.1 *.txt
|
||||||
|
|
||||||
# Dependency tracking:
|
# Dependency tracking:
|
||||||
|
|
||||||
allsources = $(wildcard *.c)
|
allsources = $(wildcard *.c)
|
||||||
|
|
||||||
-include $(allsources:.c=.d)
|
-include $(allsources:.c=.d)
|
||||||
%.o : %.c
|
%.o : %.c
|
||||||
$(COMPILE.c) -MP -MMD -MT '$*.obj' -o $@ $<
|
$(COMPILE.c) -MP -MMD -MT '$*.o' -MF '$*.d' -o $@ $<
|
||||||
|
|
||||||
|
-include $(allsources:.c=.dobj)
|
||||||
%.obj : %.c
|
%.obj : %.c
|
||||||
$(WINCC) $(WINCFLAGS) -MP -MMD -MT '$*.o' -c -o $@ $<
|
$(WINCC) $(WINCFLAGS) -MP -MMD -MT '$*.obj' -MF '$*.dobj' -c -o $@ $<
|
||||||
|
|
||||||
# Win32 executable
|
# Win32 executable
|
||||||
|
|
||||||
|
|
11
README
11
README
|
@ -1,6 +1,13 @@
|
||||||
Labjack/Nerdjack Tools
|
Labjack/Nerdjack Tools
|
||||||
by Jim Paris <jim@jtan.com>
|
by Jim Paris <jim@jtan.com>
|
||||||
with modifications by Zach Clifford <zacharyc@mit.edu>
|
with modifications by
|
||||||
|
Zach Clifford <zacharyc@mit.edu>
|
||||||
|
John Donnal <jdonnal@mit.edu>
|
||||||
|
|
||||||
These tools are for interacting with the LabJack UE9 or the NerdJack over the Ethernet interface. More information about the UE9 device:
|
These tools are for interacting with the LabJack UE9 or the NerdJack
|
||||||
|
over the Ethernet interface. More information about the UE9 device:
|
||||||
http://www.labjack.com/labjack_ue9.php
|
http://www.labjack.com/labjack_ue9.php
|
||||||
|
|
||||||
|
The NerdJack device is a custom board made in LEES by Zach Clifford.
|
||||||
|
|
||||||
|
Use ethstream -h or ethstream -X for usage instructions and examples.
|
||||||
|
|
|
@ -2,71 +2,46 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "compat.h"
|
#include "compat.h"
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include "errno.h"
|
||||||
|
|
||||||
unsigned int
|
unsigned int sleep(unsigned int seconds)
|
||||||
sleep (unsigned int seconds)
|
|
||||||
{
|
{
|
||||||
Sleep (seconds * 1000);
|
Sleep(seconds * 1000);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct
|
static struct {
|
||||||
{
|
|
||||||
int num;
|
int num;
|
||||||
char *msg;
|
char *msg;
|
||||||
} win32_error[] =
|
} win32_error[] = {
|
||||||
{
|
|
||||||
/* Errors that we might vaguely expect to see */
|
/* Errors that we might vaguely expect to see */
|
||||||
{
|
{ WSAEINTR, "Winsock: Interrupted system call"},
|
||||||
WSAEINTR, "Winsock: Interrupted system call"},
|
{ WSAEBADF, "Winsock: Bad file number"},
|
||||||
{
|
{ WSAEFAULT, "Winsock: Bad address"},
|
||||||
WSAEBADF, "Winsock: Bad file number"},
|
{ WSAEINVAL, "Winsock: Invalid argument"},
|
||||||
{
|
{ WSAEMFILE, "Winsock: Too many open files"},
|
||||||
WSAEFAULT, "Winsock: Bad address"},
|
{ WSAEWOULDBLOCK, "Winsock: Operation would block"},
|
||||||
{
|
{ WSAEINPROGRESS, "Winsock: Operation now in progress"},
|
||||||
WSAEINVAL, "Winsock: Invalid argument"},
|
{ WSAEALREADY, "Winsock: Operation already in progress"},
|
||||||
{
|
{ WSAENOTSOCK, "Winsock: Socket operation on nonsocket"},
|
||||||
WSAEMFILE, "Winsock: Too many open files"},
|
{ WSAEADDRINUSE, "Winsock: Address already in use"},
|
||||||
{
|
{ WSAEADDRNOTAVAIL, "Winsock: Cannot assign requested address"},
|
||||||
WSAEWOULDBLOCK, "Winsock: Operation would block"},
|
{ WSAENETDOWN, "Winsock: Network is down"},
|
||||||
{
|
{ WSAENETUNREACH, "Winsock: Network is unreachable"},
|
||||||
WSAEINPROGRESS, "Winsock: Operation now in progress"},
|
{ WSAENETRESET, "Winsock: Network dropped connection on reset"},
|
||||||
{
|
{ WSAECONNABORTED, "Winsock: Software caused connection abort"},
|
||||||
WSAEALREADY, "Winsock: Operation already in progress"},
|
{ WSAECONNRESET, "Winsock: Connection reset by peer"},
|
||||||
{
|
{ WSAETIMEDOUT, "Winsock: Connection timed out"},
|
||||||
WSAENOTSOCK, "Winsock: Socket operation on nonsocket"},
|
{ WSAECONNREFUSED, "Winsock: Connection refused"},
|
||||||
{
|
{ WSAEHOSTDOWN, "Winsock: Host is down"},
|
||||||
WSAEADDRINUSE, "Winsock: Address already in use"},
|
{ WSAEHOSTUNREACH, "Winsock: No route to host"},
|
||||||
{
|
{ WSAVERNOTSUPPORTED, "Winsock: Unsupported Winsock version"},
|
||||||
WSAEADDRNOTAVAIL, "Winsock: Cannot assign requested address"},
|
{ ETIMEDOUT, "Connection timed out"},
|
||||||
{
|
{ ENOTCONN, "Not connected"},
|
||||||
WSAENETDOWN, "Winsock: Network is down"},
|
{ -1, NULL},
|
||||||
{
|
};
|
||||||
WSAENETUNREACH, "Winsock: Network is unreachable"},
|
|
||||||
{
|
char *compat_strerror(int errnum)
|
||||||
WSAENETRESET, "Winsock: Network dropped connection on reset"},
|
|
||||||
{
|
|
||||||
WSAECONNABORTED, "Winsock: Software caused connection abort"},
|
|
||||||
{
|
|
||||||
WSAECONNRESET, "Winsock: Connection reset by peer"},
|
|
||||||
{
|
|
||||||
WSAETIMEDOUT, "Winsock: Connection timed out"},
|
|
||||||
{
|
|
||||||
WSAECONNREFUSED, "Winsock: Connection refused"},
|
|
||||||
{
|
|
||||||
WSAEHOSTDOWN, "Winsock: Host is down"},
|
|
||||||
{
|
|
||||||
WSAEHOSTUNREACH, "Winsock: No route to host"},
|
|
||||||
{
|
|
||||||
WSAVERNOTSUPPORTED, "Winsock: Unsupported Winsock version"},
|
|
||||||
{
|
|
||||||
ETIMEDOUT, "Connection timed out"},
|
|
||||||
{
|
|
||||||
ENOTCONN, "Not connected"},
|
|
||||||
{
|
|
||||||
-1, NULL},};
|
|
||||||
char *
|
|
||||||
compat_strerror (int errnum)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
static char buf[128];
|
static char buf[128];
|
||||||
|
@ -74,12 +49,11 @@ compat_strerror (int errnum)
|
||||||
for (i = 0; win32_error[i].num != -1; i++)
|
for (i = 0; win32_error[i].num != -1; i++)
|
||||||
if (errnum == win32_error[i].num)
|
if (errnum == win32_error[i].num)
|
||||||
return win32_error[i].msg;
|
return win32_error[i].msg;
|
||||||
if (errnum >= 10000)
|
if (errnum >= 10000) {
|
||||||
{
|
sprintf(buf, "Winsock: unknown error %d\n", errnum);
|
||||||
sprintf (buf, "Winsock: unknown error %d\n", errnum);
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
return strerror (errnum);
|
return strerror(errnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
|
|
7
compat.h
7
compat.h
|
@ -2,12 +2,9 @@
|
||||||
#define COMPAT_H
|
#define COMPAT_H
|
||||||
|
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
unsigned int sleep (unsigned int seconds);
|
unsigned int sleep(unsigned int seconds);
|
||||||
char *compat_strerror (int errnum);
|
char *compat_strerror(int errnum);
|
||||||
//const char *inet_ntop(int af, void *src, const char *dst, socklen_t cnt);
|
//const char *inet_ntop(int af, void *src, const char *dst, socklen_t cnt);
|
||||||
#define INET_ADDRSTRLEN 16
|
|
||||||
#define ETIMEDOUT 110
|
|
||||||
#define ENOTCONN 107
|
|
||||||
#else
|
#else
|
||||||
#define compat_strerror strerror
|
#define compat_strerror strerror
|
||||||
#endif
|
#endif
|
||||||
|
|
29
debug.c
29
debug.c
|
@ -1,18 +1,35 @@
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
int verb_count = 0;
|
int verb_count = 0;
|
||||||
|
|
||||||
int
|
int func_fprintf(const char *func, FILE * stream, const char *format, ...)
|
||||||
func_fprintf (const char *func, FILE * stream, const char *format, ...)
|
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int ret;
|
int ret;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
fprintf (stream, "%s: ", func);
|
gettimeofday(&tv, NULL);
|
||||||
va_start (ap, format);
|
fprintf(stream, "%ld.%06ld: ", (unsigned long)tv.tv_sec, (unsigned long)tv.tv_usec);
|
||||||
ret = vfprintf (stream, format, ap);
|
fprintf(stream, "%s: ", func);
|
||||||
va_end (ap);
|
va_start(ap, format);
|
||||||
|
ret = vfprintf(stream, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int my_fprintf(FILE * stream, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int ret;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
fprintf(stream, "%ld.%06ld: ", (unsigned long)tv.tv_sec, (unsigned long)tv.tv_usec);
|
||||||
|
va_start(ap, format);
|
||||||
|
ret = vfprintf(stream, format, ap);
|
||||||
|
va_end(ap);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
11
debug.h
11
debug.h
|
@ -14,8 +14,10 @@ extern int verb_count;
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int func_fprintf (const char *func, FILE * stream, const char *format,
|
int func_fprintf(const char *func, FILE * stream, const char *format,
|
||||||
...) __attribute__ ((format (printf, 3, 4)));
|
...) __attribute__ ((format(printf, 3, 4)));
|
||||||
|
int my_fprintf(FILE * stream, const char *format,
|
||||||
|
...) __attribute__ ((format(printf, 2, 3)));
|
||||||
|
|
||||||
#define debug(x...) ({ \
|
#define debug(x...) ({ \
|
||||||
if(verb_count >= 2) \
|
if(verb_count >= 2) \
|
||||||
|
@ -28,6 +30,11 @@ int func_fprintf (const char *func, FILE * stream, const char *format,
|
||||||
})
|
})
|
||||||
|
|
||||||
#define info(x...) ({ \
|
#define info(x...) ({ \
|
||||||
|
if(verb_count >= 0) \
|
||||||
|
my_fprintf(stderr,x); \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define info_no_timestamp(x...) ({ \
|
||||||
if(verb_count >= 0) \
|
if(verb_count >= 0) \
|
||||||
fprintf(stderr,x); \
|
fprintf(stderr,x); \
|
||||||
})
|
})
|
||||||
|
|
724
ethstream.c
724
ethstream.c
File diff suppressed because it is too large
Load Diff
|
@ -5,4 +5,6 @@
|
||||||
#define CONVERT_VOLTS 1
|
#define CONVERT_VOLTS 1
|
||||||
#define CONVERT_HEX 2
|
#define CONVERT_HEX 2
|
||||||
|
|
||||||
|
#define TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
106
example.inc
Normal file
106
example.inc
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
|
||||||
|
char examplestring[] = "\n\
|
||||||
|
\n\
|
||||||
|
Welcome to the NILM Ethstream examples.\n\
|
||||||
|
\n\
|
||||||
|
For the most part, typing \"ethstream\" by itself will sample the first\n\
|
||||||
|
two channels at 8 kHz on 10V range. Press CTRL-C to terminate sampling.\n\
|
||||||
|
\n\
|
||||||
|
If you want current measurements on the first two phases of NILM\n\
|
||||||
|
with default sample rate of 8 kHz and 10V range:\n\
|
||||||
|
\n\
|
||||||
|
ethstream -C 0,3\n\
|
||||||
|
\n\
|
||||||
|
The device is configured so that channels 0 through 2 are currents for\n\
|
||||||
|
the three phases and channels 3-5 are for voltages of the three phases.\n\
|
||||||
|
The current channels sample voltages that will depend on the DIP switch\n\
|
||||||
|
settings in the NILM box. The DIP switch positions allow you to convert\n\
|
||||||
|
ethstream's readings to true current readings.\n\
|
||||||
|
\n\
|
||||||
|
If you want only currents at 16 kHz and 10V range:\n\
|
||||||
|
\n\
|
||||||
|
ethstream -n 3 -r 16000\n\
|
||||||
|
\n\
|
||||||
|
The -n option samples a number of channels starting at 0. The rate can be\n\
|
||||||
|
at least 16000 if 12 channels are sampled , but it can do more if\n\
|
||||||
|
fewer channels are sampled. The limiting factor is the highest channel\n\
|
||||||
|
sampled. Sampling just the top channel (11) is as bad as sampling\n\
|
||||||
|
all 12 at once.\n\
|
||||||
|
Ethstream will warn if you approach the limits of the NerdJack with the\n\
|
||||||
|
given sampled channels. Sampling outside the range of the NerdJack might\n\
|
||||||
|
result in corrupt data or crashing of the device. There will be no\n\
|
||||||
|
permanent damage to NILM or NerdJack, but be aware of the possibility of\n\
|
||||||
|
data corruption.\n\
|
||||||
|
\n\
|
||||||
|
If you need a higher accuracy but lower range measurement on the voltages:\n\
|
||||||
|
\n\
|
||||||
|
ethstream -R 5,10 -C 3,4,5\n\
|
||||||
|
\n\
|
||||||
|
The two numbers to the R command set the range to either 5V or 10V. Above,\n\
|
||||||
|
we are setting channels 0-5 to 5 V range and channels 6-11 to 10 V range.\n\
|
||||||
|
Channels 6-11 are unconnected, but they can have range set independently.\n\
|
||||||
|
\n\
|
||||||
|
All of the above examples output a digital number from 0 to 65535 with\n\
|
||||||
|
65535 representing the highest range (5V or 10V). 0 represents the most\n\
|
||||||
|
negative range (-5V or -10V). If you want conversion\n\
|
||||||
|
to volts for all six voltages and currents:\n\
|
||||||
|
\n\
|
||||||
|
ethstream -c -C 0,3,1,4,2,5\n\
|
||||||
|
\n\
|
||||||
|
The channels will be output in the order given in the C command. This\n\
|
||||||
|
command will group the current and voltage data by phase.\n\
|
||||||
|
\n\
|
||||||
|
If you are supplying data from ethstream to another program, you might\n\
|
||||||
|
want to dump its output to a file and terminate after a certain number of\n\
|
||||||
|
samples:\n\
|
||||||
|
\n\
|
||||||
|
ethstream -n 6 -r 8000 - l 16000 > outfile.dat\n\
|
||||||
|
\n\
|
||||||
|
This will take 16000 samples at 8 kHz (2 seconds of acquisition) of all\n\
|
||||||
|
channels and write the data to outfile.dat. This can be directly read\n\
|
||||||
|
by a package like MATLAB.\n\
|
||||||
|
\n\
|
||||||
|
If there are multiple NerdJacks or you have changed the TCP/IP settings\n\
|
||||||
|
from default, you might have to specify which one you want to talk to:\n\
|
||||||
|
\n\
|
||||||
|
ethstream -a 192.168.1.210\n\
|
||||||
|
\n\
|
||||||
|
This will sample two channels at 8 kHz from the NerdJack at 192.168.1.210.\n\
|
||||||
|
This is the default \"1\" setting on the NerdJack. If no address is\n\
|
||||||
|
specified, ethstream connects first to 192.168.1.209. It then tries\n\
|
||||||
|
to autodetect the NerdJack. This should find the device if you are on\n\
|
||||||
|
the same network, but it will get confused if there are multiple NerdJacks\n\
|
||||||
|
on the network.\n\
|
||||||
|
\n\
|
||||||
|
Labjack only Timer modes are also avaliable. Read the Labjack UE9 Users Guide\n\
|
||||||
|
for more information. Upto 6 timers of various modes can be specified,\n\
|
||||||
|
they occur on FIO0-FIO5 which are on channels 200-205 respectively in order\n\
|
||||||
|
of specification. For 32 bit timer modes, the MSW should be read from\n\
|
||||||
|
channel 224 imeadiately after the LSW is read from one the timer channel.\n\
|
||||||
|
A clock frequency divisor is specified on a per device basis. For example:\n\
|
||||||
|
\n\
|
||||||
|
ethstream -t 4,12 -T 1 -C 200,224,201\n\
|
||||||
|
\n\
|
||||||
|
This will enable two timers with the fastest system clock divisor (48 MhZ/1)\n\
|
||||||
|
and read the two 16 bit words for timer mode 4 and the single 16 bit word of\n\
|
||||||
|
timer mode 12. These three words will occupy their own columns in the output\n\
|
||||||
|
stream. Digital timer mode channels can be interspersed with analog inouts.\n\
|
||||||
|
\n\
|
||||||
|
Labjack only individual analog input channel gain set is also avaliable.\n\
|
||||||
|
Gain 0 is default on labjack and corresponds to -5.18v to +5.07v. Gain 1 is\n\
|
||||||
|
is -0.01 to +5.07v. Gain 2 is -0.01 to +2.53v. Gain 4 is -0.01 to +1.26v.\n\
|
||||||
|
Gain 8 is -0.01 to +0.62v. Gains on the -g flag should be put in the desired\n\
|
||||||
|
order corresponding to the channels as specified by the -C flag. If there are\n\
|
||||||
|
less gains specified than channels the remainder default to gain 0. Extra gains\n\
|
||||||
|
are ignored. Gains can be specified for digital inputs or timer modes but they\n\
|
||||||
|
are irrelevant. A case where one should do this is if there are dital input\n\
|
||||||
|
channels intersperced within analog input channels; this keeps the order matched\n\
|
||||||
|
up so later analog input channels have the expected gain.\n\
|
||||||
|
\n\
|
||||||
|
ethstream -t 4 -T 1 -C 0,1,200,224,2,3 -g 2,2,0,0,4,4 -c\n\
|
||||||
|
\n\
|
||||||
|
This will set channles 0,1 and 2,3 to gain 2,2 and 4,4, respectively and convert\n\
|
||||||
|
the data to volts using the firmware stored factory calibrated data on the\n\
|
||||||
|
labjack. The digital channels 200 and 224 will remain undisturbed as integers.\n\
|
||||||
|
\n\
|
||||||
|
";
|
102
ljconfig.c
102
ljconfig.c
|
@ -1,102 +0,0 @@
|
||||||
/*
|
|
||||||
* Labjack Tools
|
|
||||||
* Copyright (c) 2003-2007 Jim Paris <jim@jtan.com>
|
|
||||||
*
|
|
||||||
* This is free software; you can redistribute it and/or modify it and
|
|
||||||
* it is provided under the terms of version 2 of the GNU General Public
|
|
||||||
* License as published by the Free Software Foundation; see COPYING.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* ljconfig: display/change comm/control processor configuration */
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "debug.h"
|
|
||||||
#include "ue9.h"
|
|
||||||
#include "ue9error.h"
|
|
||||||
#include "opt.h"
|
|
||||||
#include "version.h"
|
|
||||||
|
|
||||||
#define DEFAULT_HOST "192.168.1.209"
|
|
||||||
#define UE9_COMMAND_PORT 52360
|
|
||||||
|
|
||||||
struct options opt[] = {
|
|
||||||
{'a', "address", "string", "host/address of UE9 (192.168.1.209)"},
|
|
||||||
{'h', "help", NULL, "this help"},
|
|
||||||
{'v', "verbose", NULL, "be verbose"},
|
|
||||||
{'V', "version", NULL, "show version number and exit"},
|
|
||||||
{0, NULL, NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
int
|
|
||||||
main (int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int optind;
|
|
||||||
char *optarg;
|
|
||||||
char c;
|
|
||||||
FILE *help = stderr;
|
|
||||||
char *address = strdup (DEFAULT_HOST);
|
|
||||||
int fd;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* Parse arguments */
|
|
||||||
opt_init (&optind);
|
|
||||||
while ((c = opt_parse (argc, argv, &optind, &optarg, opt)) != 0)
|
|
||||||
{
|
|
||||||
switch (c)
|
|
||||||
{
|
|
||||||
case 'a':
|
|
||||||
free (address);
|
|
||||||
address = strdup (optarg);
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
verb_count++;
|
|
||||||
break;
|
|
||||||
case 'V':
|
|
||||||
printf ("ljconfig " VERSION "\n");
|
|
||||||
printf ("Written by Jim Paris <jim@jtan.com>\n");
|
|
||||||
printf ("This program comes with no warranty and is "
|
|
||||||
"provided under the GPLv2.\n");
|
|
||||||
return 0;
|
|
||||||
break;
|
|
||||||
case 'h':
|
|
||||||
help = stdout;
|
|
||||||
default:
|
|
||||||
printhelp:
|
|
||||||
fprintf (help, "Usage: %s [options]\n", *argv);
|
|
||||||
opt_help (opt, help);
|
|
||||||
fprintf (help, "Displays/changes Labjack UE9 config.\n");
|
|
||||||
return (help == stdout) ? 0 : 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (optind < argc)
|
|
||||||
{
|
|
||||||
info ("Error: too many arguments (%s)\n\n", argv[optind]);
|
|
||||||
goto printhelp;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 1;
|
|
||||||
|
|
||||||
/* Open */
|
|
||||||
fd = ue9_open (address, UE9_COMMAND_PORT);
|
|
||||||
if (fd < 0)
|
|
||||||
{
|
|
||||||
info ("Connect failed: %s:%d\n", address, UE9_COMMAND_PORT);
|
|
||||||
goto out0;
|
|
||||||
}
|
|
||||||
|
|
||||||
goto out1;
|
|
||||||
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
out1:
|
|
||||||
/* Close */
|
|
||||||
ue9_close (fd);
|
|
||||||
out0:
|
|
||||||
return ret;
|
|
||||||
}
|
|
68
ljtest.c
68
ljtest.c
|
@ -1,68 +0,0 @@
|
||||||
/*
|
|
||||||
* Labjack Tools
|
|
||||||
* Copyright (c) 2003-2007 Jim Paris <jim@jtan.com>
|
|
||||||
*
|
|
||||||
* This is free software; you can redistribute it and/or modify it and
|
|
||||||
* it is provided under the terms of version 2 of the GNU General Public
|
|
||||||
* License as published by the Free Software Foundation; see COPYING.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include "debug.h"
|
|
||||||
#include "ue9.h"
|
|
||||||
#include "compat.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main (int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int fd_cmd;
|
|
||||||
struct ue9Calibration calib;
|
|
||||||
|
|
||||||
verb_count = 2;
|
|
||||||
|
|
||||||
fd_cmd = ue9_open ("192.168.1.209", 52360);
|
|
||||||
if (fd_cmd < 0)
|
|
||||||
{
|
|
||||||
fprintf (stderr, "ue9_open: %s\n", compat_strerror (errno));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ue9_get_calibration (fd_cmd, &calib) < 0)
|
|
||||||
{
|
|
||||||
fprintf (stderr, "ue9_get_calibration: %s\n", compat_strerror (errno));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf ("double unipolarSlope[0] = %lf\n", calib.unipolarSlope[0]);
|
|
||||||
printf ("double unipolarSlope[1] = %lf\n", calib.unipolarSlope[1]);
|
|
||||||
printf ("double unipolarSlope[2] = %lf\n", calib.unipolarSlope[2]);
|
|
||||||
printf ("double unipolarSlope[3] = %lf\n", calib.unipolarSlope[3]);
|
|
||||||
printf ("double unipolarOffset[0] = %lf\n", calib.unipolarOffset[0]);
|
|
||||||
printf ("double unipolarOffset[1] = %lf\n", calib.unipolarOffset[1]);
|
|
||||||
printf ("double unipolarOffset[2] = %lf\n", calib.unipolarOffset[2]);
|
|
||||||
printf ("double unipolarOffset[3] = %lf\n", calib.unipolarOffset[3]);
|
|
||||||
printf ("double bipolarSlope = %lf\n", calib.bipolarSlope);
|
|
||||||
printf ("double bipolarOffset = %lf\n", calib.bipolarOffset);
|
|
||||||
printf ("double DACSlope[0] = %lf\n", calib.DACSlope[0]);
|
|
||||||
printf ("double DACSlope[1] = %lf\n", calib.DACSlope[1]);
|
|
||||||
printf ("double DACOffset[0] = %lf\n", calib.DACOffset[0]);
|
|
||||||
printf ("double DACOffset[1] = %lf\n", calib.DACOffset[1]);
|
|
||||||
printf ("double tempSlope = %lf\n", calib.tempSlope);
|
|
||||||
printf ("double tempSlopeLow = %lf\n", calib.tempSlopeLow);
|
|
||||||
printf ("double calTemp = %lf\n", calib.calTemp);
|
|
||||||
printf ("double Vref = %lf\n", calib.Vref);
|
|
||||||
printf ("double VrefDiv2 = %lf\n", calib.VrefDiv2);
|
|
||||||
printf ("double VsSlope = %lf\n", calib.VsSlope);
|
|
||||||
printf ("double hiResUnipolarSlope = %lf\n", calib.hiResUnipolarSlope);
|
|
||||||
printf ("double hiResUnipolarOffset = %lf\n", calib.hiResUnipolarOffset);
|
|
||||||
printf ("double hiResBipolarSlope = %lf\n", calib.hiResBipolarSlope);
|
|
||||||
printf ("double hiResBipolarOffset = %lf\n", calib.hiResBipolarOffset);
|
|
||||||
|
|
||||||
ue9_close (fd_cmd);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
414
nerdjack.c
414
nerdjack.c
|
@ -25,13 +25,10 @@
|
||||||
#include "netutil.h"
|
#include "netutil.h"
|
||||||
#include "ethstream.h"
|
#include "ethstream.h"
|
||||||
|
|
||||||
#define NERDJACK_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */
|
|
||||||
|
|
||||||
#define NERD_HEADER_SIZE 8
|
#define NERD_HEADER_SIZE 8
|
||||||
#define MAX_SOCKETS 32
|
#define MAX_SOCKETS 32
|
||||||
|
|
||||||
typedef struct __attribute__ ((__packed__))
|
typedef struct __attribute__ ((__packed__)) {
|
||||||
{
|
|
||||||
unsigned char headerone;
|
unsigned char headerone;
|
||||||
unsigned char headertwo;
|
unsigned char headertwo;
|
||||||
unsigned short packetNumber;
|
unsigned short packetNumber;
|
||||||
|
@ -54,24 +51,22 @@ struct discover_t {
|
||||||
/* Choose the best ScanConfig and ScanInterval parameters for the
|
/* Choose the best ScanConfig and ScanInterval parameters for the
|
||||||
desired scanrate. Returns -1 if no valid config found */
|
desired scanrate. Returns -1 if no valid config found */
|
||||||
int
|
int
|
||||||
nerdjack_choose_scan (double desired_rate, double *actual_rate,
|
nerdjack_choose_scan(double desired_rate, double *actual_rate,
|
||||||
unsigned long *period)
|
unsigned long *period)
|
||||||
{
|
{
|
||||||
//The ffffe is because of a silicon bug. The last bit is unusable in all
|
//The ffffe is because of a silicon bug. The last bit is unusable in all
|
||||||
//devices so far. It is worked around on the chip, but giving it exactly
|
//devices so far. It is worked around on the chip, but giving it exactly
|
||||||
//0xfffff would cause the workaround code to roll over.
|
//0xfffff would cause the workaround code to roll over.
|
||||||
*period = floor ((double) NERDJACK_CLOCK_RATE / desired_rate);
|
*period = floor((double)NERDJACK_CLOCK_RATE / desired_rate);
|
||||||
if (*period > 0x0ffffe)
|
if (*period > 0x0ffffe) {
|
||||||
{
|
info("Cannot sample that slowly\n");
|
||||||
info ("Cannot sample that slowly\n");
|
*actual_rate = (double)NERDJACK_CLOCK_RATE / (double)0x0ffffe;
|
||||||
*actual_rate = (double) NERDJACK_CLOCK_RATE / (double) 0x0ffffe;
|
|
||||||
*period = 0x0ffffe;
|
*period = 0x0ffffe;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
//Period holds the period register for the NerdJack, so it needs to be right
|
//Period holds the period register for the NerdJack, so it needs to be right
|
||||||
*actual_rate = (double) NERDJACK_CLOCK_RATE / (double) *period;
|
*actual_rate = (double)NERDJACK_CLOCK_RATE / (double)*period;
|
||||||
if (*actual_rate != desired_rate)
|
if (*actual_rate != desired_rate) {
|
||||||
{
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -81,7 +76,8 @@ nerdjack_choose_scan (double desired_rate, double *actual_rate,
|
||||||
* Create a discovered socket and add it to the socket list structure.
|
* Create a discovered socket and add it to the socket list structure.
|
||||||
* All sockets in the structure should be created, bound, and ready for broadcasting
|
* All sockets in the structure should be created, bound, and ready for broadcasting
|
||||||
*/
|
*/
|
||||||
static int discovered_sock_create(struct discover_t *ds, uint32_t local_ip, uint32_t subnet_mask)
|
static int discovered_sock_create(struct discover_t *ds, uint32_t local_ip,
|
||||||
|
uint32_t subnet_mask)
|
||||||
{
|
{
|
||||||
if (ds->sock_count >= MAX_SOCKETS) {
|
if (ds->sock_count >= MAX_SOCKETS) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -95,12 +91,13 @@ static int discovered_sock_create(struct discover_t *ds, uint32_t local_ip, uint
|
||||||
|
|
||||||
/* Allow broadcast. */
|
/* Allow broadcast. */
|
||||||
int sock_opt = 1;
|
int sock_opt = 1;
|
||||||
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&sock_opt, sizeof(sock_opt));
|
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&sock_opt,
|
||||||
|
sizeof(sock_opt));
|
||||||
|
|
||||||
/* Set nonblocking */
|
/* Set nonblocking */
|
||||||
if (soblock (sock, 0) < 0)
|
if (soblock(sock, 0) < 0) {
|
||||||
{
|
verb("can't set nonblocking\n");
|
||||||
verb ("can't set nonblocking\n");
|
close(sock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,13 +120,15 @@ static int discovered_sock_create(struct discover_t *ds, uint32_t local_ip, uint
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enumerate all interfaces we can find and open sockets on each
|
* Enumerate all interfaces we can find and open sockets on each
|
||||||
*/
|
*/
|
||||||
#if defined(USE_IPHLPAPI)
|
#if defined(USE_IPHLPAPI)
|
||||||
static void enumerate_interfaces(struct discover_t *ds)
|
static void enumerate_interfaces(struct discover_t *ds)
|
||||||
{
|
{
|
||||||
PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
|
PIP_ADAPTER_INFO pAdapterInfo =
|
||||||
|
(IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO));
|
||||||
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
||||||
|
|
||||||
DWORD Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
DWORD Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
||||||
|
@ -138,7 +137,7 @@ static void enumerate_interfaces(struct discover_t *ds)
|
||||||
if (Ret != ERROR_BUFFER_OVERFLOW) {
|
if (Ret != ERROR_BUFFER_OVERFLOW) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
|
pAdapterInfo = (IP_ADAPTER_INFO *) malloc(ulOutBufLen);
|
||||||
Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
||||||
if (Ret != NO_ERROR) {
|
if (Ret != NO_ERROR) {
|
||||||
free(pAdapterInfo);
|
free(pAdapterInfo);
|
||||||
|
@ -150,8 +149,10 @@ static void enumerate_interfaces(struct discover_t *ds)
|
||||||
while (pAdapter) {
|
while (pAdapter) {
|
||||||
IP_ADDR_STRING *pIPAddr = &pAdapter->IpAddressList;
|
IP_ADDR_STRING *pIPAddr = &pAdapter->IpAddressList;
|
||||||
while (pIPAddr) {
|
while (pIPAddr) {
|
||||||
uint32_t local_ip = ntohl(inet_addr(pIPAddr->IpAddress.String));
|
uint32_t local_ip =
|
||||||
uint32_t mask = ntohl(inet_addr(pIPAddr->IpMask.String));
|
ntohl(inet_addr(pIPAddr->IpAddress.String));
|
||||||
|
uint32_t mask =
|
||||||
|
ntohl(inet_addr(pIPAddr->IpMask.String));
|
||||||
|
|
||||||
if (local_ip == 0) {
|
if (local_ip == 0) {
|
||||||
pIPAddr = pIPAddr->Next;
|
pIPAddr = pIPAddr->Next;
|
||||||
|
@ -169,7 +170,8 @@ static void enumerate_interfaces(struct discover_t *ds)
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
static void enumerate_interfaces(struct discover_t *ds) {
|
static void enumerate_interfaces(struct discover_t *ds)
|
||||||
|
{
|
||||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
return;
|
return;
|
||||||
|
@ -187,8 +189,8 @@ static void enumerate_interfaces(struct discover_t *ds) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *ptr = (uint8_t *)ifc.ifc_req;
|
uint8_t *ptr = (uint8_t *) ifc.ifc_req;
|
||||||
uint8_t *end = (uint8_t *)&ifc.ifc_buf[ifc.ifc_len];
|
uint8_t *end = (uint8_t *) & ifc.ifc_buf[ifc.ifc_len];
|
||||||
|
|
||||||
while (ptr <= end) {
|
while (ptr <= end) {
|
||||||
struct ifreq *ifr = (struct ifreq *)ptr;
|
struct ifreq *ifr = (struct ifreq *)ptr;
|
||||||
|
@ -197,7 +199,8 @@ static void enumerate_interfaces(struct discover_t *ds) {
|
||||||
if (ioctl(fd, SIOCGIFADDR, ifr) != 0) {
|
if (ioctl(fd, SIOCGIFADDR, ifr) != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
struct sockaddr_in *addr_in = (struct sockaddr_in *)&(ifr->ifr_addr);
|
struct sockaddr_in *addr_in =
|
||||||
|
(struct sockaddr_in *)&(ifr->ifr_addr);
|
||||||
uint32_t local_ip = ntohl(addr_in->sin_addr.s_addr);
|
uint32_t local_ip = ntohl(addr_in->sin_addr.s_addr);
|
||||||
if (local_ip == 0) {
|
if (local_ip == 0) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -207,7 +210,8 @@ static void enumerate_interfaces(struct discover_t *ds) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_in *mask_in = (struct sockaddr_in *)&(ifr->ifr_addr);
|
struct sockaddr_in *mask_in =
|
||||||
|
(struct sockaddr_in *)&(ifr->ifr_addr);
|
||||||
uint32_t mask = ntohl(mask_in->sin_addr.s_addr);
|
uint32_t mask = ntohl(mask_in->sin_addr.s_addr);
|
||||||
|
|
||||||
discovered_sock_create(ds, local_ip, mask);
|
discovered_sock_create(ds, local_ip, mask);
|
||||||
|
@ -228,55 +232,51 @@ static void destroy_socks(struct discover_t *ds)
|
||||||
free(ds);
|
free(ds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Perform autodetection. Returns 0 on success, -1 on error
|
/* Perform autodetection. Returns 0 on success, -1 on error
|
||||||
* Sets ipAddress to the detected address
|
* Sets ipAddress to the detected address
|
||||||
*/
|
*/
|
||||||
int
|
int nerdjack_detect(char *ipAddress)
|
||||||
nerdjack_detect (char *ipAddress)
|
|
||||||
{
|
{
|
||||||
int32_t receivesock;
|
int32_t receivesock;
|
||||||
struct sockaddr_in sa, receiveaddr, sFromAddr;
|
struct sockaddr_in sa, receiveaddr, sFromAddr;
|
||||||
int buffer_length;
|
int buffer_length;
|
||||||
char buffer[200];
|
char buffer[200];
|
||||||
char incomingData[10];
|
char incomingData[10];
|
||||||
unsigned int lFromLen;
|
socklen_t lFromLen;
|
||||||
|
|
||||||
sprintf (buffer, "TEST");
|
sprintf(buffer, "TEST");
|
||||||
buffer_length = strlen (buffer) + 1;
|
buffer_length = strlen(buffer) + 1;
|
||||||
|
|
||||||
net_init ();
|
net_init();
|
||||||
|
|
||||||
receivesock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
receivesock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
/* Set nonblocking */
|
/* Set nonblocking */
|
||||||
if (soblock (receivesock, 0) < 0)
|
if (soblock(receivesock, 0) < 0) {
|
||||||
{
|
verb("can't set nonblocking\n");
|
||||||
verb ("can't set nonblocking\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (-1 == receivesock) /* if socket failed to initialize, exit */
|
if (-1 == receivesock) { /* if socket failed to initialize, exit */
|
||||||
{
|
verb("Error Creating Socket\n");
|
||||||
verb ("Error Creating Socket\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Setup family for both sockets
|
//Setup family for both sockets
|
||||||
sa.sin_family = PF_INET;
|
sa.sin_family = PF_INET;
|
||||||
receiveaddr.sin_family = PF_INET;
|
receiveaddr.sin_family = PF_INET;
|
||||||
|
|
||||||
//Setup ports to send on DATA and receive on RECEIVE
|
//Setup ports to send on DATA and receive on RECEIVE
|
||||||
receiveaddr.sin_port = htons (NERDJACK_UDP_RECEIVE_PORT);
|
receiveaddr.sin_port = htons(NERDJACK_UDP_RECEIVE_PORT);
|
||||||
sa.sin_port = htons (NERDJACK_DATA_PORT);
|
sa.sin_port = htons(NERDJACK_DATA_PORT);
|
||||||
|
|
||||||
//Receive from any IP address
|
//Receive from any IP address
|
||||||
receiveaddr.sin_addr.s_addr = INADDR_ANY;
|
receiveaddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
bind (receivesock, (struct sockaddr *) &receiveaddr,
|
bind(receivesock, (struct sockaddr *)&receiveaddr,
|
||||||
sizeof (struct sockaddr_in));
|
sizeof(struct sockaddr_in));
|
||||||
|
|
||||||
struct discover_t *ds = (struct discover_t *)calloc(1, sizeof(struct discover_t));
|
struct discover_t *ds =
|
||||||
|
(struct discover_t *)calloc(1, sizeof(struct discover_t));
|
||||||
if (!ds) {
|
if (!ds) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -299,31 +299,67 @@ nerdjack_detect (char *ipAddress)
|
||||||
struct discovered_socket *dss = &ds->socks[i];
|
struct discovered_socket *dss = &ds->socks[i];
|
||||||
uint32_t target_ip = dss->local_ip | ~dss->subnet_mask;
|
uint32_t target_ip = dss->local_ip | ~dss->subnet_mask;
|
||||||
sa.sin_addr.s_addr = htonl(target_ip);
|
sa.sin_addr.s_addr = htonl(target_ip);
|
||||||
sendto (dss->sock, buffer, buffer_length, 0, (struct sockaddr *) &sa,
|
sendto(dss->sock, buffer, buffer_length, 0,
|
||||||
sizeof (struct sockaddr_in));
|
(struct sockaddr *)&sa, sizeof(struct sockaddr_in));
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_socks(ds);
|
destroy_socks(ds);
|
||||||
|
|
||||||
lFromLen = sizeof (sFromAddr);
|
lFromLen = sizeof(sFromAddr);
|
||||||
|
|
||||||
if (0 >
|
if (0 >
|
||||||
recvfrom_timeout (receivesock, incomingData, sizeof (incomingData), 0,
|
recvfrom_timeout(receivesock, incomingData, sizeof(incomingData), 0,
|
||||||
(struct sockaddr *) &sFromAddr, &lFromLen,
|
(struct sockaddr *)&sFromAddr, &lFromLen,
|
||||||
&(struct timeval)
|
&(struct timeval) {
|
||||||
{
|
.tv_sec = TIMEOUT})) {
|
||||||
.tv_sec = NERDJACK_TIMEOUT}))
|
|
||||||
{
|
|
||||||
close(receivesock);
|
close(receivesock);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ipAddress = malloc (INET_ADDRSTRLEN);
|
ipAddress = malloc(INET_ADDRSTRLEN);
|
||||||
|
|
||||||
//It isn't ipv6 friendly, but inet_ntop isn't on Windows...
|
//It isn't ipv6 friendly, but inet_ntop isn't on Windows...
|
||||||
strcpy (ipAddress, inet_ntoa (sFromAddr.sin_addr));
|
strcpy(ipAddress, inet_ntoa(sFromAddr.sin_addr));
|
||||||
|
|
||||||
|
close(receivesock);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the NerdJack version string and print it
|
||||||
|
*/
|
||||||
|
int nerd_get_version(const char *address)
|
||||||
|
{
|
||||||
|
int ret, fd_command;
|
||||||
|
char buf[200];
|
||||||
|
fd_command = nerd_open(address, NERDJACK_COMMAND_PORT);
|
||||||
|
if (fd_command < 0) {
|
||||||
|
info("Connect failed: %s:%d\n", address, NERDJACK_COMMAND_PORT);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send request */
|
||||||
|
ret = send_all_timeout(fd_command, "VERS", 4, 0, &(struct timeval) {
|
||||||
|
.tv_sec = TIMEOUT});
|
||||||
|
if (ret < 0) {
|
||||||
|
verb("short send %d\n", (int)ret);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = recv_all_timeout(fd_command, buf, 200, 0, &(struct timeval) {
|
||||||
|
.tv_sec = TIMEOUT});
|
||||||
|
|
||||||
|
nerd_close_conn(fd_command);
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
verb("Error receiving command\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
//Slice off the "OK" from the string
|
||||||
|
buf[strlen(buf) - 2] = '\0';
|
||||||
|
|
||||||
|
printf("%s\n", buf);
|
||||||
|
|
||||||
close (receivesock);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -331,52 +367,44 @@ nerdjack_detect (char *ipAddress)
|
||||||
* of the specified length. This expects the NerdJack to reply with OK
|
* of the specified length. This expects the NerdJack to reply with OK
|
||||||
* or NO
|
* or NO
|
||||||
*/
|
*/
|
||||||
int
|
int nerd_send_command(const char *address, void *command, int length)
|
||||||
nerd_send_command (const char *address, void *command, int length)
|
|
||||||
{
|
{
|
||||||
int ret, fd_command;
|
int ret, fd_command;
|
||||||
char buf[3];
|
char buf[3];
|
||||||
fd_command = nerd_open (address, NERDJACK_COMMAND_PORT);
|
fd_command = nerd_open(address, NERDJACK_COMMAND_PORT);
|
||||||
if (fd_command < 0)
|
if (fd_command < 0) {
|
||||||
{
|
info("Connect failed: %s:%d\n", address, NERDJACK_COMMAND_PORT);
|
||||||
info ("Connect failed: %s:%d\n", address, NERDJACK_COMMAND_PORT);
|
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send request */
|
/* Send request */
|
||||||
ret = send_all_timeout (fd_command, command, length, 0, &(struct timeval)
|
ret = send_all_timeout(fd_command, command, length, 0, &(struct timeval) {
|
||||||
{
|
.tv_sec = TIMEOUT});
|
||||||
.tv_sec = NERDJACK_TIMEOUT});
|
if (ret < 0 || ret != length) {
|
||||||
if (ret < 0 || ret != length)
|
verb("short send %d\n", (int)ret);
|
||||||
{
|
|
||||||
verb ("short send %d\n", (int) ret);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = recv_all_timeout (fd_command, buf, 3, 0, &(struct timeval)
|
ret = recv_all_timeout(fd_command, buf, 3, 0, &(struct timeval) {
|
||||||
{
|
.tv_sec = TIMEOUT});
|
||||||
.tv_sec = NERDJACK_TIMEOUT});
|
|
||||||
|
|
||||||
nerd_close_conn (fd_command);
|
nerd_close_conn(fd_command);
|
||||||
|
|
||||||
if (ret < 0 || ret != 3)
|
if (ret < 0 || ret != 3) {
|
||||||
{
|
verb("Error receiving OK for command\n");
|
||||||
verb ("Error receiving OK for command\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 != strcmp ("OK", buf))
|
if (0 != strcmp("OK", buf)) {
|
||||||
{
|
verb("Did not receive OK. Received %s\n", buf);
|
||||||
verb ("Did not receive OK. Received %s\n", buf);
|
|
||||||
return -4;
|
return -4;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
nerd_data_stream (int data_fd, int numChannels, int *channel_list,
|
nerd_data_stream(int data_fd, int numChannels, int *channel_list,
|
||||||
int precision, int convert, int lines, int showmem,
|
int precision, int convert, int lines, int showmem,
|
||||||
unsigned short *currentcount, unsigned int period,
|
unsigned short *currentcount, unsigned int period,
|
||||||
int wasreset)
|
int wasreset)
|
||||||
|
@ -394,13 +422,11 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
|
||||||
|
|
||||||
//The number sampled will be the highest channel requested plus 1
|
//The number sampled will be the highest channel requested plus 1
|
||||||
//(i.e. channel 0 requested means 1 sampled)
|
//(i.e. channel 0 requested means 1 sampled)
|
||||||
for (i = 0; i < numChannels; i++)
|
for (i = 0; i < numChannels; i++) {
|
||||||
{
|
|
||||||
if (channel_list[i] + 1 > numChannelsSampled)
|
if (channel_list[i] + 1 > numChannelsSampled)
|
||||||
numChannelsSampled = channel_list[i] + 1;
|
numChannelsSampled = channel_list[i] + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double voltline[numChannels];
|
double voltline[numChannels];
|
||||||
|
|
||||||
unsigned short dataline[numChannels];
|
unsigned short dataline[numChannels];
|
||||||
|
@ -420,171 +446,148 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
|
||||||
|
|
||||||
//Check to see if we're trying to resume
|
//Check to see if we're trying to resume
|
||||||
//Don't blow away linesleft in that case
|
//Don't blow away linesleft in that case
|
||||||
if (lines != 0 && linesleft == 0)
|
if (lines != 0 && linesleft == 0) {
|
||||||
{
|
|
||||||
linesleft = lines;
|
linesleft = lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
//If there was a reset, we still need to dump a line because of faulty PDCA start
|
//If there was a reset, we still need to dump a line because of faulty PDCA start
|
||||||
if (wasreset)
|
if (wasreset) {
|
||||||
{
|
|
||||||
linesdumped = 0;
|
linesdumped = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//If this is the first time called, warn the user if we're too fast
|
//If this is the first time called, warn the user if we're too fast
|
||||||
if (linesdumped == 0)
|
if (linesdumped == 0) {
|
||||||
{
|
if (period < (numChannelsSampled * 200 + 600)) {
|
||||||
if (period < (numChannelsSampled * 200 + 600))
|
info("You are sampling close to the limit of NerdJack\n");
|
||||||
{
|
info("Sample fewer channels or sample slower\n");
|
||||||
info ("You are sampling close to the limit of NerdJack\n");
|
|
||||||
info ("Sample fewer channels or sample slower\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Now destination structure array is set as well as numDuplicates.
|
//Now destination structure array is set as well as numDuplicates.
|
||||||
|
|
||||||
int totalGroups = NERDJACK_NUM_SAMPLES / numChannelsSampled;
|
int totalGroups = NERDJACK_NUM_SAMPLES / numChannelsSampled;
|
||||||
|
|
||||||
|
|
||||||
//Loop forever to grab data
|
//Loop forever to grab data
|
||||||
while ((charsread =
|
while ((charsread =
|
||||||
recv_all_timeout (data_fd, &buf, NERDJACK_PACKET_SIZE, 0,
|
recv_all_timeout(data_fd, &buf, NERDJACK_PACKET_SIZE, 0,
|
||||||
&(struct timeval)
|
&(struct timeval) {
|
||||||
{
|
.tv_sec = expectedtimeout}))) {
|
||||||
.tv_sec = expectedtimeout})))
|
|
||||||
{
|
|
||||||
|
|
||||||
if (charsread != NERDJACK_PACKET_SIZE)
|
if (charsread != NERDJACK_PACKET_SIZE) {
|
||||||
{
|
|
||||||
//There was a problem getting data. Probably a closed
|
//There was a problem getting data. Probably a closed
|
||||||
//connection.
|
//connection.
|
||||||
info ("Packet timed out or was too short\n");
|
info("Packet timed out or was too short\n");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
//First check the header info
|
//First check the header info
|
||||||
if (buf.headerone != 0xF0 || buf.headertwo != 0xAA)
|
if (buf.headerone != 0xF0 || buf.headertwo != 0xAA) {
|
||||||
{
|
info("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 = ntohs (buf.packetNumber);
|
tempshort = ntohs(buf.packetNumber);
|
||||||
if (tempshort != *currentcount)
|
if (tempshort != *currentcount) {
|
||||||
{
|
info("Count wrong. Expected %hd but got %hd\n",
|
||||||
info ("Count wrong. Expected %hd but got %hd\n", *currentcount,
|
*currentcount, tempshort);
|
||||||
tempshort);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Increment number of packets received
|
//Increment number of packets received
|
||||||
*currentcount = *currentcount + 1;
|
*currentcount = *currentcount + 1;
|
||||||
|
|
||||||
adcused = ntohs (buf.adcused);
|
adcused = ntohs(buf.adcused);
|
||||||
packetsready = ntohs (buf.packetsready);
|
packetsready = ntohs(buf.packetsready);
|
||||||
numgroupsProcessed = 0;
|
numgroupsProcessed = 0;
|
||||||
|
|
||||||
if (showmem)
|
if (showmem) {
|
||||||
{
|
printf("%hd %hd\n", adcused, packetsready);
|
||||||
printf ("%hd %hd\n", adcused, packetsready);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//While there is still more data in the packet, process it
|
//While there is still more data in the packet, process it
|
||||||
while (numgroupsProcessed < totalGroups)
|
while (numgroupsProcessed < totalGroups) {
|
||||||
{
|
|
||||||
//Poison the data structure
|
//Poison the data structure
|
||||||
switch (convert)
|
switch (convert) {
|
||||||
{
|
|
||||||
case CONVERT_VOLTS:
|
case CONVERT_VOLTS:
|
||||||
memset (voltline, 0, numChannels * sizeof (double));
|
memset(voltline, 0,
|
||||||
|
numChannels * sizeof(double));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
case CONVERT_HEX:
|
case CONVERT_HEX:
|
||||||
case CONVERT_DEC:
|
case CONVERT_DEC:
|
||||||
memset (dataline, 0, numChannels * sizeof (unsigned char));
|
memset(dataline, 0,
|
||||||
|
numChannels * sizeof(unsigned char));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read in each group
|
//Read in each group
|
||||||
for (i = 0; i < numChannels; i++)
|
for (i = 0; i < numChannels; i++) {
|
||||||
{
|
|
||||||
//Get the datapoint associated with the desired channel
|
//Get the datapoint associated with the desired channel
|
||||||
datapoint =
|
datapoint =
|
||||||
ntohs (buf.
|
ntohs(buf.data[channel_list[i] +
|
||||||
data[channel_list[i] +
|
numgroupsProcessed *
|
||||||
numgroupsProcessed * numChannelsSampled]);
|
numChannelsSampled]);
|
||||||
|
|
||||||
//Place it into the line
|
//Place it into the line
|
||||||
switch (convert)
|
switch (convert) {
|
||||||
{
|
|
||||||
case CONVERT_VOLTS:
|
case CONVERT_VOLTS:
|
||||||
if (channel_list[i] <= 5)
|
if (channel_list[i] <= 5) {
|
||||||
{
|
|
||||||
volts =
|
volts =
|
||||||
(double) (datapoint / 32767.0) *
|
(double)(datapoint /
|
||||||
((precision & 0x01) ? 5.0 : 10.0);
|
32767.0) *
|
||||||
}
|
((precision & 0x01) ? 5.0 :
|
||||||
else
|
10.0);
|
||||||
{
|
} else {
|
||||||
volts =
|
volts =
|
||||||
(double) (datapoint / 32767.0) *
|
(double)(datapoint /
|
||||||
((precision & 0x02) ? 5.0 : 10.0);
|
32767.0) *
|
||||||
|
((precision & 0x02) ? 5.0 :
|
||||||
|
10.0);
|
||||||
}
|
}
|
||||||
voltline[i] = volts;
|
voltline[i] = volts;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
case CONVERT_HEX:
|
case CONVERT_HEX:
|
||||||
case CONVERT_DEC:
|
case CONVERT_DEC:
|
||||||
dataline[i] = (unsigned short) (datapoint - INT16_MIN);
|
dataline[i] =
|
||||||
|
(unsigned short)(datapoint -
|
||||||
|
INT16_MIN);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//We want to dump the first line because it's usually spurious
|
//We want to dump the first line because it's usually spurious
|
||||||
if (linesdumped != 0)
|
if (linesdumped != 0) {
|
||||||
{
|
|
||||||
//Now print the group
|
//Now print the group
|
||||||
switch (convert)
|
switch (convert) {
|
||||||
{
|
|
||||||
case CONVERT_VOLTS:
|
case CONVERT_VOLTS:
|
||||||
for (i = 0; i < numChannels; i++)
|
for (i = 0; i < numChannels; i++) {
|
||||||
{
|
if (printf("%lf ", voltline[i])
|
||||||
if (printf ("%lf ", voltline[i]) < 0)
|
< 0)
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CONVERT_HEX:
|
case CONVERT_HEX:
|
||||||
for (i = 0; i < numChannels; i++)
|
for (i = 0; i < numChannels; i++) {
|
||||||
{
|
if (printf("%04hX", dataline[i])
|
||||||
if (printf ("%04hX", dataline[i]) < 0)
|
< 0)
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
case CONVERT_DEC:
|
case CONVERT_DEC:
|
||||||
for (i = 0; i < numChannels; i++)
|
for (i = 0; i < numChannels; i++) {
|
||||||
{
|
if (printf("%hu ", dataline[i])
|
||||||
if (printf ("%hu ", dataline[i]) < 0)
|
< 0)
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (printf ("\n") < 0)
|
if (printf("\n") < 0)
|
||||||
goto bad;
|
goto bad;
|
||||||
|
|
||||||
//If we're counting lines, decrement them
|
//If we're counting lines, decrement them
|
||||||
if (lines != 0)
|
if (lines != 0) {
|
||||||
{
|
|
||||||
linesleft--;
|
linesleft--;
|
||||||
if (linesleft == 0)
|
if (linesleft == 0) {
|
||||||
{
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
linesdumped = linesdumped + 1;
|
linesdumped = linesdumped + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -596,61 +599,56 @@ nerd_data_stream (int data_fd, int numChannels, int *channel_list,
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
bad:
|
bad:
|
||||||
info ("Output error (disk full?)\n");
|
info("Output error (disk full?)\n");
|
||||||
return -3;
|
return -3;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Open a connection to the NerdJack */
|
/* Open a connection to the NerdJack */
|
||||||
int
|
int nerd_open(const char *address, int port)
|
||||||
nerd_open (const char *address, int port)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
struct hostent *he;
|
struct hostent *he;
|
||||||
|
|
||||||
net_init ();
|
net_init();
|
||||||
|
|
||||||
int32_t i32SocketFD = socket (PF_INET, SOCK_STREAM, 0);
|
int32_t i32SocketFD = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
if (-1 == i32SocketFD)
|
if (-1 == i32SocketFD) {
|
||||||
{
|
verb("cannot create socket");
|
||||||
verb ("cannot create socket");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set nonblocking */
|
/* Set nonblocking */
|
||||||
if (soblock (i32SocketFD, 0) < 0)
|
if (soblock(i32SocketFD, 0) < 0) {
|
||||||
{
|
verb("can't set nonblocking\n");
|
||||||
verb ("can't set nonblocking\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_in stSockAddr;
|
struct sockaddr_in stSockAddr;
|
||||||
memset (&stSockAddr, 0, sizeof (stSockAddr));
|
memset(&stSockAddr, 0, sizeof(stSockAddr));
|
||||||
|
|
||||||
stSockAddr.sin_family = AF_INET;
|
stSockAddr.sin_family = AF_INET;
|
||||||
stSockAddr.sin_port = htons (port);
|
stSockAddr.sin_port = htons(port);
|
||||||
|
|
||||||
he = gethostbyname (address);
|
he = gethostbyname(address);
|
||||||
if (he == NULL)
|
if (he == NULL) {
|
||||||
{
|
verb("gethostbyname(\"%s\") failed\n", address);
|
||||||
verb ("gethostbyname(\"%s\") failed\n", address);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
stSockAddr.sin_addr = *((struct in_addr *) he->h_addr);
|
stSockAddr.sin_addr = *((struct in_addr *)he->h_addr);
|
||||||
|
|
||||||
debug ("Resolved %s -> %s\n", address, inet_ntoa (stSockAddr.sin_addr));
|
debug("Resolved %s -> %s\n", address, inet_ntoa(stSockAddr.sin_addr));
|
||||||
|
|
||||||
/* Connect */
|
/* Connect */
|
||||||
if (connect_timeout
|
if (connect_timeout
|
||||||
(i32SocketFD, (struct sockaddr *) &stSockAddr, sizeof (stSockAddr),
|
(i32SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr),
|
||||||
&(struct timeval)
|
&(struct timeval) {
|
||||||
{
|
.tv_sec = 3}) < 0) {
|
||||||
.tv_sec = 3}) < 0)
|
verb("connection to %s:%d failed: %s\n",
|
||||||
{
|
inet_ntoa(stSockAddr.sin_addr), port,
|
||||||
verb ("connection to %s:%d failed: %s\n",
|
compat_strerror(errno));
|
||||||
inet_ntoa (stSockAddr.sin_addr), port, compat_strerror (errno));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -659,7 +657,7 @@ nerd_open (const char *address, int port)
|
||||||
|
|
||||||
//Generate an appropriate sample initiation command
|
//Generate an appropriate sample initiation command
|
||||||
int
|
int
|
||||||
nerd_generate_command (getPacket * command, int *channel_list,
|
nerd_generate_command(getPacket * command, int *channel_list,
|
||||||
int channel_count, int precision, unsigned long period)
|
int channel_count, int precision, unsigned long period)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -667,17 +665,14 @@ nerd_generate_command (getPacket * command, int *channel_list,
|
||||||
int i;
|
int i;
|
||||||
int highestchannel = 0;
|
int highestchannel = 0;
|
||||||
|
|
||||||
for (i = 0; i < channel_count; i++)
|
for (i = 0; i < channel_count; i++) {
|
||||||
{
|
if (channel_list[i] > highestchannel) {
|
||||||
if (channel_list[i] > highestchannel)
|
|
||||||
{
|
|
||||||
highestchannel = channel_list[i];
|
highestchannel = channel_list[i];
|
||||||
}
|
}
|
||||||
//channelbit = channelbit | (0x1 << channel_list[i]);
|
//channelbit = channelbit | (0x1 << channel_list[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i <= highestchannel; i++)
|
for (i = 0; i <= highestchannel; i++) {
|
||||||
{
|
|
||||||
channelbit = channelbit | (0x01 << i);
|
channelbit = channelbit | (0x01 << i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -685,19 +680,18 @@ nerd_generate_command (getPacket * command, int *channel_list,
|
||||||
command->word[1] = 'E';
|
command->word[1] = 'E';
|
||||||
command->word[2] = 'T';
|
command->word[2] = 'T';
|
||||||
command->word[3] = 'D';
|
command->word[3] = 'D';
|
||||||
command->channelbit = htons (channelbit);
|
command->channelbit = htons(channelbit);
|
||||||
command->precision = precision;
|
command->precision = precision;
|
||||||
command->period = htonl (period);
|
command->period = htonl(period);
|
||||||
command->prescaler = 0;
|
command->prescaler = 0;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int nerd_close_conn(int data_fd)
|
||||||
nerd_close_conn (int data_fd)
|
|
||||||
{
|
{
|
||||||
shutdown (data_fd, 2);
|
shutdown(data_fd, 2);
|
||||||
close (data_fd);
|
close(data_fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
23
nerdjack.h
23
nerdjack.h
|
@ -25,39 +25,42 @@
|
||||||
#define NERDJACK_NUM_SAMPLES 726
|
#define NERDJACK_NUM_SAMPLES 726
|
||||||
|
|
||||||
/* Packet structure used in message to start sampling on NerdJack */
|
/* Packet structure used in message to start sampling on NerdJack */
|
||||||
typedef struct __attribute__ ((__packed__))
|
typedef struct __attribute__ ((__packed__)) {
|
||||||
{
|
|
||||||
char word[4];
|
char word[4];
|
||||||
unsigned long period;
|
unsigned int period; //CHANGED FROM TYPE LONG. With 64 bit compilers longs are 8 bytes and nerdjack expects a 4 byte value
|
||||||
|
//since the int type is 4 bytes this works but should be changed to use defined datatypes rather than rely on compiler data types
|
||||||
unsigned short channelbit;
|
unsigned short channelbit;
|
||||||
unsigned char precision;
|
unsigned char precision;
|
||||||
unsigned char prescaler;
|
unsigned char prescaler;
|
||||||
} getPacket;
|
} getPacket;
|
||||||
|
|
||||||
/* Open/close TCP/IP connection to the NerdJack */
|
/* Open/close TCP/IP connection to the NerdJack */
|
||||||
int nerd_open (const char *address, int port);
|
int nerd_open(const char *address, int port);
|
||||||
int nerd_close_conn (int data_fd);
|
int nerd_close_conn(int data_fd);
|
||||||
|
|
||||||
/* Generate the command word for the NerdJack */
|
/* Generate the command word for the NerdJack */
|
||||||
int nerd_generate_command (getPacket * command, int *channel_list,
|
int nerd_generate_command(getPacket * command, int *channel_list,
|
||||||
int channel_count, int precision,
|
int channel_count, int precision,
|
||||||
unsigned long period);
|
unsigned long period);
|
||||||
|
|
||||||
/* Send given command to NerdJack */
|
/* Send given command to NerdJack */
|
||||||
int nerd_send_command (const char *address, void *command, int length);
|
int nerd_send_command(const char *address, void *command, int length);
|
||||||
|
|
||||||
|
/* Get the version string from NerdJack */
|
||||||
|
int nerd_get_version(const char *address);
|
||||||
|
|
||||||
/* Stream data out of the NerdJack */
|
/* Stream data out of the NerdJack */
|
||||||
int nerd_data_stream (int data_fd, int numChannels, int *channel_list,
|
int nerd_data_stream(int data_fd, int numChannels, int *channel_list,
|
||||||
int precision, int convert, int lines, int showmem,
|
int precision, int convert, int lines, int showmem,
|
||||||
unsigned short *currentcount, unsigned int period,
|
unsigned short *currentcount, unsigned int period,
|
||||||
int wasreset);
|
int wasreset);
|
||||||
|
|
||||||
/* Detect the IP Address of the NerdJack and return in ipAddress */
|
/* Detect the IP Address of the NerdJack and return in ipAddress */
|
||||||
int nerdjack_detect (char *ipAddress);
|
int nerdjack_detect(char *ipAddress);
|
||||||
|
|
||||||
/* Choose the best ScanConfig and ScanInterval parameters for the
|
/* Choose the best ScanConfig and ScanInterval parameters for the
|
||||||
desired scanrate. Returns -1 if no valid config found */
|
desired scanrate. Returns -1 if no valid config found */
|
||||||
int nerdjack_choose_scan (double desired_rate, double *actual_rate,
|
int nerdjack_choose_scan(double desired_rate, double *actual_rate,
|
||||||
unsigned long *period);
|
unsigned long *period);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
116
netutil.c
116
netutil.c
|
@ -5,31 +5,28 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/* Initialize networking */
|
/* Initialize networking */
|
||||||
void
|
void net_init(void)
|
||||||
net_init (void)
|
|
||||||
{
|
{
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
WSADATA blah;
|
WSADATA blah;
|
||||||
WSAStartup (0x0101, &blah);
|
WSAStartup(0x0101, &blah);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set socket blocking/nonblocking */
|
/* Set socket blocking/nonblocking */
|
||||||
int
|
int soblock(int socket, int blocking)
|
||||||
soblock (int socket, int blocking)
|
|
||||||
{
|
{
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
unsigned long arg = blocking ? 0 : 1;
|
unsigned long arg = blocking ? 0 : 1;
|
||||||
if (ioctlsocket (socket, FIONBIO, &arg) != 0)
|
if (ioctlsocket(socket, FIONBIO, &arg) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
int sockopt;
|
int sockopt;
|
||||||
|
|
||||||
/* Get flags */
|
/* Get flags */
|
||||||
sockopt = fcntl (socket, F_GETFL);
|
sockopt = fcntl(socket, F_GETFL);
|
||||||
if (sockopt == -1)
|
if (sockopt == -1) {
|
||||||
{
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,17 +37,16 @@ soblock (int socket, int blocking)
|
||||||
sockopt |= O_NONBLOCK;
|
sockopt |= O_NONBLOCK;
|
||||||
|
|
||||||
/* Set flags */
|
/* Set flags */
|
||||||
if (fcntl (socket, F_SETFL, sockopt) != 0)
|
if (fcntl(socket, F_SETFL, sockopt) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Like connect(2), but with a timeout. Socket must be non-blocking. */
|
/* Like connect(2), but with a timeout. Socket must be non-blocking. */
|
||||||
int
|
int
|
||||||
connect_timeout (int s, const struct sockaddr *serv_addr, socklen_t addrlen,
|
connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen,
|
||||||
struct timeval *timeout)
|
struct timeval *timeout)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -60,17 +56,16 @@ connect_timeout (int s, const struct sockaddr *serv_addr, socklen_t addrlen,
|
||||||
socklen_t optlen;
|
socklen_t optlen;
|
||||||
|
|
||||||
/* Start connect */
|
/* Start connect */
|
||||||
ret = connect (s, serv_addr, addrlen);
|
ret = connect(s, serv_addr, addrlen);
|
||||||
|
|
||||||
if (ret == 0)
|
if (ret == 0) {
|
||||||
{
|
|
||||||
/* Success */
|
/* Success */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for immediate failure */
|
/* Check for immediate failure */
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
errno = WSAGetLastError ();
|
errno = WSAGetLastError();
|
||||||
if (ret < 0 && errno != WSAEWOULDBLOCK && errno != WSAEINVAL)
|
if (ret < 0 && errno != WSAEWOULDBLOCK && errno != WSAEINVAL)
|
||||||
return -1;
|
return -1;
|
||||||
#else
|
#else
|
||||||
|
@ -79,30 +74,27 @@ connect_timeout (int s, const struct sockaddr *serv_addr, socklen_t addrlen,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* In progress, wait for result. */
|
/* In progress, wait for result. */
|
||||||
FD_ZERO (&writefds);
|
FD_ZERO(&writefds);
|
||||||
FD_SET (s, &writefds);
|
FD_SET(s, &writefds);
|
||||||
FD_ZERO (&exceptfds);
|
FD_ZERO(&exceptfds);
|
||||||
FD_SET (s, &exceptfds);
|
FD_SET(s, &exceptfds);
|
||||||
ret = select (s + 1, NULL, &writefds, &exceptfds, timeout);
|
ret = select(s + 1, NULL, &writefds, &exceptfds, timeout);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
{
|
|
||||||
/* Error */
|
/* Error */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (ret == 0)
|
if (ret == 0) {
|
||||||
{
|
|
||||||
/* Timed out */
|
/* Timed out */
|
||||||
errno = ETIMEDOUT;
|
errno = ETIMEDOUT;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check the socket state */
|
/* Check the socket state */
|
||||||
optlen = sizeof (optval);
|
optlen = sizeof(optval);
|
||||||
if (getsockopt (s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen) != 0)
|
if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (optval != 0)
|
if (optval != 0) {
|
||||||
{
|
|
||||||
/* Connection failed. */
|
/* Connection failed. */
|
||||||
errno = optval;
|
errno = optval;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -110,8 +102,7 @@ connect_timeout (int s, const struct sockaddr *serv_addr, socklen_t addrlen,
|
||||||
|
|
||||||
/* On Windows, SO_ERROR sometimes shows no error but the connection
|
/* On Windows, SO_ERROR sometimes shows no error but the connection
|
||||||
still failed. Sigh. */
|
still failed. Sigh. */
|
||||||
if (FD_ISSET (s, &exceptfds) || !FD_ISSET (s, &writefds))
|
if (FD_ISSET(s, &exceptfds) || !FD_ISSET(s, &writefds)) {
|
||||||
{
|
|
||||||
errno = EIO;
|
errno = EIO;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -124,103 +115,95 @@ connect_timeout (int s, const struct sockaddr *serv_addr, socklen_t addrlen,
|
||||||
The timeout only applies if no data at all is sent -- this function
|
The timeout only applies if no data at all is sent -- this function
|
||||||
may still send less than requested. */
|
may still send less than requested. */
|
||||||
ssize_t
|
ssize_t
|
||||||
send_timeout (int s, const void *buf, size_t len, int flags,
|
send_timeout(int s, const void *buf, size_t len, int flags,
|
||||||
struct timeval * timeout)
|
struct timeval * timeout)
|
||||||
{
|
{
|
||||||
fd_set writefds;
|
fd_set writefds;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
FD_ZERO (&writefds);
|
FD_ZERO(&writefds);
|
||||||
FD_SET (s, &writefds);
|
FD_SET(s, &writefds);
|
||||||
ret = select (s + 1, NULL, &writefds, NULL, timeout);
|
ret = select(s + 1, NULL, &writefds, NULL, timeout);
|
||||||
if (ret == 0)
|
if (ret == 0) {
|
||||||
{
|
|
||||||
/* Timed out */
|
/* Timed out */
|
||||||
errno = ETIMEDOUT;
|
errno = ETIMEDOUT;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (ret != 1)
|
if (ret != 1) {
|
||||||
{
|
|
||||||
/* Error */
|
/* Error */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return send (s, buf, len, flags);
|
return send(s, buf, len, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Like recv(2), but with a timeout. Socket must be non-blocking.
|
/* Like recv(2), but with a timeout. Socket must be non-blocking.
|
||||||
The timeout only applies if no data at all is received -- this
|
The timeout only applies if no data at all is received -- this
|
||||||
function may still return less than requested. */
|
function may still return less than requested. */
|
||||||
ssize_t
|
ssize_t
|
||||||
recv_timeout (int s, void *buf, size_t len, int flags,
|
recv_timeout(int s, void *buf, size_t len, int flags, struct timeval * timeout)
|
||||||
struct timeval * timeout)
|
|
||||||
{
|
{
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
FD_ZERO (&readfds);
|
FD_ZERO(&readfds);
|
||||||
FD_SET (s, &readfds);
|
FD_SET(s, &readfds);
|
||||||
ret = select (s + 1, &readfds, NULL, NULL, timeout);
|
ret = select(s + 1, &readfds, NULL, NULL, timeout);
|
||||||
if (ret == 0)
|
if (ret == 0) {
|
||||||
{
|
|
||||||
/* Timed out */
|
/* Timed out */
|
||||||
errno = ETIMEDOUT;
|
errno = ETIMEDOUT;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (ret != 1)
|
if (ret != 1) {
|
||||||
{
|
|
||||||
/* Error */
|
/* Error */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return recv (s, buf, len, flags);
|
return recv(s, buf, len, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Like recvfrom(2), but with a timeout. Socket must be non-blocking.
|
/* Like recvfrom(2), but with a timeout. Socket must be non-blocking.
|
||||||
The timeout only applies if no data at all is received -- this
|
The timeout only applies if no data at all is received -- this
|
||||||
function may still return less than requested. */
|
function may still return less than requested. */
|
||||||
ssize_t
|
ssize_t
|
||||||
recvfrom_timeout (int s, void *buf, size_t len, int flags,
|
recvfrom_timeout(int s, void *buf, size_t len, int flags,
|
||||||
struct sockaddr * address, socklen_t * address_len,
|
struct sockaddr * address, socklen_t * address_len,
|
||||||
struct timeval * timeout)
|
struct timeval * timeout)
|
||||||
{
|
{
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
FD_ZERO (&readfds);
|
FD_ZERO(&readfds);
|
||||||
FD_SET (s, &readfds);
|
FD_SET(s, &readfds);
|
||||||
ret = select (s + 1, &readfds, NULL, NULL, timeout);
|
ret = select(s + 1, &readfds, NULL, NULL, timeout);
|
||||||
if (ret == 0)
|
if (ret == 0) {
|
||||||
{
|
|
||||||
/* Timed out */
|
/* Timed out */
|
||||||
errno = ETIMEDOUT;
|
errno = ETIMEDOUT;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (ret != 1)
|
if (ret != 1) {
|
||||||
{
|
|
||||||
/* Error */
|
/* Error */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return recvfrom (s, buf, len, flags, address, address_len);
|
return recvfrom(s, buf, len, flags, address, address_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Like send_timeout, but retries (with the same timeout) in case of
|
/* Like send_timeout, but retries (with the same timeout) in case of
|
||||||
partial transfers. This is a stronger attempt to send all
|
partial transfers. This is a stronger attempt to send all
|
||||||
requested data. */
|
requested data. */
|
||||||
ssize_t
|
ssize_t
|
||||||
send_all_timeout (int s, const void *buf, size_t len, int flags,
|
send_all_timeout(int s, const void *buf, size_t len, int flags,
|
||||||
struct timeval * timeout)
|
struct timeval * timeout)
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
size_t left = len;
|
size_t left = len;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
while (left > 0)
|
while (left > 0) {
|
||||||
{
|
|
||||||
tv.tv_sec = timeout->tv_sec;
|
tv.tv_sec = timeout->tv_sec;
|
||||||
tv.tv_usec = timeout->tv_usec;
|
tv.tv_usec = timeout->tv_usec;
|
||||||
ret = send_timeout (s, buf, left, flags, &tv);
|
ret = send_timeout(s, buf, left, flags, &tv);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -239,18 +222,17 @@ send_all_timeout (int s, const void *buf, size_t len, int flags,
|
||||||
partial transfers. This is a stronger attempt to recv all
|
partial transfers. This is a stronger attempt to recv all
|
||||||
requested data. */
|
requested data. */
|
||||||
ssize_t
|
ssize_t
|
||||||
recv_all_timeout (int s, void *buf, size_t len, int flags,
|
recv_all_timeout(int s, void *buf, size_t len, int flags,
|
||||||
struct timeval * timeout)
|
struct timeval * timeout)
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
size_t left = len;
|
size_t left = len;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
while (left > 0)
|
while (left > 0) {
|
||||||
{
|
|
||||||
tv.tv_sec = timeout->tv_sec;
|
tv.tv_sec = timeout->tv_sec;
|
||||||
tv.tv_usec = timeout->tv_usec;
|
tv.tv_usec = timeout->tv_usec;
|
||||||
ret = recv_timeout (s, buf, left, flags, &tv);
|
ret = recv_timeout(s, buf, left, flags, &tv);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
16
netutil.h
16
netutil.h
|
@ -27,28 +27,28 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Initialize networking */
|
/* Initialize networking */
|
||||||
void net_init (void);
|
void net_init(void);
|
||||||
|
|
||||||
/* Set socket blocking/nonblocking */
|
/* Set socket blocking/nonblocking */
|
||||||
int soblock (int socket, int blocking);
|
int soblock(int socket, int blocking);
|
||||||
|
|
||||||
/* Like send(2), recv(2), connect(2), but with timeouts.
|
/* Like send(2), recv(2), connect(2), but with timeouts.
|
||||||
Socket must be O_NONBLOCK. */
|
Socket must be O_NONBLOCK. */
|
||||||
int connect_timeout (int s, const struct sockaddr *serv_addr,
|
int connect_timeout(int s, const struct sockaddr *serv_addr,
|
||||||
socklen_t addrlen, struct timeval *timeout);
|
socklen_t addrlen, struct timeval *timeout);
|
||||||
ssize_t send_timeout (int s, const void *buf, size_t len, int flags,
|
ssize_t send_timeout(int s, const void *buf, size_t len, int flags,
|
||||||
struct timeval *timeout);
|
struct timeval *timeout);
|
||||||
ssize_t recv_timeout (int s, void *buf, size_t len, int flags,
|
ssize_t recv_timeout(int s, void *buf, size_t len, int flags,
|
||||||
struct timeval *timeout);
|
struct timeval *timeout);
|
||||||
ssize_t recvfrom_timeout (int s, void *buf, size_t len, int flags,
|
ssize_t recvfrom_timeout(int s, void *buf, size_t len, int flags,
|
||||||
struct sockaddr *address, socklen_t * address_len,
|
struct sockaddr *address, socklen_t * address_len,
|
||||||
struct timeval *timeout);
|
struct timeval *timeout);
|
||||||
|
|
||||||
/* Like send_timeout and recv_timeout, but they retry (with the same timeout)
|
/* Like send_timeout and recv_timeout, but they retry (with the same timeout)
|
||||||
in case of partial transfers, in order to try to transfer all data. */
|
in case of partial transfers, in order to try to transfer all data. */
|
||||||
ssize_t send_all_timeout (int s, const void *buf, size_t len, int flags,
|
ssize_t send_all_timeout(int s, const void *buf, size_t len, int flags,
|
||||||
struct timeval *timeout);
|
struct timeval *timeout);
|
||||||
ssize_t recv_all_timeout (int s, void *buf, size_t len, int flags,
|
ssize_t recv_all_timeout(int s, void *buf, size_t len, int flags,
|
||||||
struct timeval *timeout);
|
struct timeval *timeout);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
51
opt.c
51
opt.c
|
@ -11,14 +11,13 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "opt.h"
|
#include "opt.h"
|
||||||
|
|
||||||
void
|
void opt_init(int *optind)
|
||||||
opt_init (int *optind)
|
|
||||||
{
|
{
|
||||||
*optind = 0;
|
*optind = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char
|
char
|
||||||
opt_parse (int argc, char **argv, int *optind, char **optarg,
|
opt_parse(int argc, char **argv, int *optind, char **optarg,
|
||||||
struct options *opt)
|
struct options *opt)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
|
@ -28,8 +27,7 @@ opt_parse (int argc, char **argv, int *optind, char **optarg,
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (argv[*optind][0] == '-' &&
|
if (argv[*optind][0] == '-' &&
|
||||||
argv[*optind][1] != '-' && argv[*optind][1] != 0)
|
argv[*optind][1] != '-' && argv[*optind][1] != 0) {
|
||||||
{
|
|
||||||
/* Short option (or a bunch of 'em) */
|
/* Short option (or a bunch of 'em) */
|
||||||
/* Save this and shift others over */
|
/* Save this and shift others over */
|
||||||
c = argv[*optind][1];
|
c = argv[*optind][1];
|
||||||
|
@ -42,67 +40,58 @@ opt_parse (int argc, char **argv, int *optind, char **optarg,
|
||||||
for (i = 0; opt[i].shortopt != 0; i++)
|
for (i = 0; opt[i].shortopt != 0; i++)
|
||||||
if (opt[i].shortopt == c)
|
if (opt[i].shortopt == c)
|
||||||
break;
|
break;
|
||||||
if (opt[i].shortopt == 0)
|
if (opt[i].shortopt == 0) {
|
||||||
{
|
fprintf(stderr, "Error: unknown option '-%c'\n", c);
|
||||||
fprintf (stderr, "Error: unknown option '-%c'\n", c);
|
|
||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
if (opt[i].arg == NULL)
|
if (opt[i].arg == NULL)
|
||||||
return c;
|
return c;
|
||||||
(*optind)++;
|
(*optind)++;
|
||||||
if (*optind >= argc || (argv[*optind][0] == '-' &&
|
if (*optind >= argc || (argv[*optind][0] == '-' &&
|
||||||
argv[*optind][1] != 0))
|
argv[*optind][1] != 0)) {
|
||||||
{
|
fprintf(stderr, "Error: option '-%c' requires an "
|
||||||
fprintf (stderr, "Error: option '-%c' requires an "
|
|
||||||
"argument\n", c);
|
"argument\n", c);
|
||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
(*optarg) = argv[*optind];
|
(*optarg) = argv[*optind];
|
||||||
return c;
|
return c;
|
||||||
}
|
} else if (argv[*optind][0] == '-' &&
|
||||||
else if (argv[*optind][0] == '-' &&
|
argv[*optind][1] == '-' && argv[*optind][2] != 0) {
|
||||||
argv[*optind][1] == '-' && argv[*optind][2] != 0)
|
|
||||||
{
|
|
||||||
/* Long option */
|
/* Long option */
|
||||||
for (i = 0; (c = opt[i].shortopt) != 0; i++)
|
for (i = 0; (c = opt[i].shortopt) != 0; i++)
|
||||||
if (strcmp (opt[i].longopt, argv[*optind] + 2) == 0)
|
if (strcmp(opt[i].longopt, argv[*optind] + 2) == 0)
|
||||||
break;
|
break;
|
||||||
if (opt[i].shortopt == 0)
|
if (opt[i].shortopt == 0) {
|
||||||
{
|
fprintf(stderr, "Error: unknown option '%s'\n",
|
||||||
fprintf (stderr, "Error: unknown option '%s'\n", argv[*optind]);
|
argv[*optind]);
|
||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
if (opt[i].arg == NULL)
|
if (opt[i].arg == NULL)
|
||||||
return c;
|
return c;
|
||||||
(*optind)++;
|
(*optind)++;
|
||||||
if (*optind >= argc || (argv[*optind][0] == '-' &&
|
if (*optind >= argc || (argv[*optind][0] == '-' &&
|
||||||
argv[*optind][1] != 0))
|
argv[*optind][1] != 0)) {
|
||||||
{
|
fprintf(stderr, "Error: option '%s' requires an "
|
||||||
fprintf (stderr, "Error: option '%s' requires an "
|
|
||||||
"argument\n", argv[*optind - 1]);
|
"argument\n", argv[*optind - 1]);
|
||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
(*optarg) = argv[*optind];
|
(*optarg) = argv[*optind];
|
||||||
return c;
|
return c;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
/* End of options */
|
/* End of options */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void opt_help(struct options *opt, FILE * out)
|
||||||
opt_help (struct options *opt, FILE * out)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int printed;
|
int printed;
|
||||||
|
|
||||||
for (i = 0; opt[i].shortopt != 0; i++)
|
for (i = 0; opt[i].shortopt != 0; i++) {
|
||||||
{
|
fprintf(out, " -%c, --%s%n", opt[i].shortopt,
|
||||||
fprintf (out, " -%c, --%s%n", opt[i].shortopt,
|
|
||||||
opt[i].longopt, &printed);
|
opt[i].longopt, &printed);
|
||||||
fprintf (out, " %-*s%s\n", 30 - printed,
|
fprintf(out, " %-*s%s\n", 30 - printed,
|
||||||
opt[i].arg ? opt[i].arg : "", opt[i].help);
|
opt[i].arg ? opt[i].arg : "", opt[i].help);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
9
opt.h
9
opt.h
|
@ -11,19 +11,18 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct options
|
struct options {
|
||||||
{
|
|
||||||
char shortopt;
|
char shortopt;
|
||||||
char *longopt;
|
char *longopt;
|
||||||
char *arg;
|
char *arg;
|
||||||
char *help;
|
char *help;
|
||||||
};
|
};
|
||||||
|
|
||||||
void opt_init (int *optind);
|
void opt_init(int *optind);
|
||||||
|
|
||||||
char opt_parse (int argc, char **argv, int *optind, char **optarg,
|
char opt_parse(int argc, char **argv, int *optind, char **optarg,
|
||||||
struct options *opt);
|
struct options *opt);
|
||||||
|
|
||||||
void opt_help (struct options *opt, FILE * out);
|
void opt_help(struct options *opt, FILE * out);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
70
ue9.h
70
ue9.h
|
@ -16,8 +16,7 @@
|
||||||
#include "netutil.h"
|
#include "netutil.h"
|
||||||
|
|
||||||
/* Calibration data */
|
/* Calibration data */
|
||||||
struct ue9Calibration
|
struct ue9Calibration {
|
||||||
{
|
|
||||||
double unipolarSlope[4];
|
double unipolarSlope[4];
|
||||||
double unipolarOffset[4];
|
double unipolarOffset[4];
|
||||||
double bipolarSlope;
|
double bipolarSlope;
|
||||||
|
@ -37,8 +36,7 @@ struct ue9Calibration
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Comm config */
|
/* Comm config */
|
||||||
struct ue9CommConfig
|
struct ue9CommConfig {
|
||||||
{
|
|
||||||
uint8_t local_id;
|
uint8_t local_id;
|
||||||
uint8_t power_level;
|
uint8_t power_level;
|
||||||
in_addr_t address;
|
in_addr_t address;
|
||||||
|
@ -54,8 +52,7 @@ struct ue9CommConfig
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Control config */
|
/* Control config */
|
||||||
struct ue9ControlConfig
|
struct ue9ControlConfig {
|
||||||
{
|
|
||||||
uint8_t power_level;
|
uint8_t power_level;
|
||||||
uint8_t reset_source;
|
uint8_t reset_source;
|
||||||
double control_fw_version;
|
double control_fw_version;
|
||||||
|
@ -71,74 +68,89 @@ struct ue9ControlConfig
|
||||||
uint16_t dac1;
|
uint16_t dac1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* These are correct! 0, 1, 2, 3, 8 */
|
||||||
#define UE9_UNIPOLAR_GAIN1 0x00
|
#define UE9_UNIPOLAR_GAIN1 0x00
|
||||||
#define UE9_UNIPOLAR_GAIN2 0x01
|
#define UE9_UNIPOLAR_GAIN2 0x01
|
||||||
#define UE9_UNIPOLAR_GAIN4 0x02
|
#define UE9_UNIPOLAR_GAIN4 0x02
|
||||||
#define UE9_UNIPOLAR_GAIN8 0x03
|
#define UE9_UNIPOLAR_GAIN8 0x03
|
||||||
#define UE9_BIPOLAR_GAIN1 0x08
|
#define UE9_BIPOLAR_GAIN1 0x08
|
||||||
|
|
||||||
#define UE9_CHANNELS 14
|
#define UE9_MAX_CHANNEL_COUNT 128
|
||||||
|
#define UE9_MAX_CHANNEL 255
|
||||||
|
#define UE9_MAX_ANALOG_CHANNEL 13
|
||||||
|
#define UE9_TIMERS 6
|
||||||
|
|
||||||
/* Fill checksums in data buffers */
|
/* Fill checksums in data buffers */
|
||||||
void ue9_checksum_normal (uint8_t * buffer, size_t len);
|
void ue9_checksum_normal(uint8_t * buffer, size_t len);
|
||||||
void ue9_checksum_extended (uint8_t * buffer, size_t len);
|
void ue9_checksum_extended(uint8_t * buffer, size_t len);
|
||||||
|
|
||||||
/* Verify checksums in data buffers. Returns 0 on error. */
|
/* Verify checksums in data buffers. Returns 0 on error. */
|
||||||
int ue9_verify_normal (uint8_t * buffer, size_t len);
|
int ue9_verify_normal(uint8_t * buffer, size_t len);
|
||||||
int ue9_verify_extended (uint8_t * buffer, size_t len);
|
int ue9_verify_extended(uint8_t * buffer, size_t len);
|
||||||
|
|
||||||
/* Open/close TCP/IP connection to the UE9 */
|
/* Open/close TCP/IP connection to the UE9 */
|
||||||
int ue9_open (const char *host, int port);
|
int ue9_open(const char *host, int port);
|
||||||
void ue9_close (int fd);
|
void ue9_close(int fd);
|
||||||
|
|
||||||
/* Read a memory block from the device. Returns -1 on error. */
|
/* Read a memory block from the device. Returns -1 on error. */
|
||||||
int ue9_memory_read (int fd, int blocknum, uint8_t * buffer, int len);
|
int ue9_memory_read(int fd, int blocknum, uint8_t * buffer, int len);
|
||||||
|
|
||||||
/* Convert 64-bit fixed point to double type */
|
/* Convert 64-bit fixed point to double type */
|
||||||
double ue9_fp64_to_double (uint8_t * data);
|
double ue9_fp64_to_double(uint8_t * data);
|
||||||
|
|
||||||
/* Retrieve calibration data or configuration from the device */
|
/* Retrieve calibration data or configuration from the device */
|
||||||
int ue9_get_calibration (int fd, struct ue9Calibration *calib);
|
int ue9_get_calibration(int fd, struct ue9Calibration *calib);
|
||||||
int ue9_get_comm_config (int fd, struct ue9CommConfig *config);
|
int ue9_get_comm_config(int fd, struct ue9CommConfig *config);
|
||||||
int ue9_get_control_config (int fd, struct ue9ControlConfig *config);
|
int ue9_get_control_config(int fd, struct ue9ControlConfig *config);
|
||||||
|
|
||||||
/* Data conversion. If calib is NULL, use uncalibrated conversions. */
|
/* Data conversion. If calib is NULL, use uncalibrated conversions. */
|
||||||
double ue9_binary_to_analog (struct ue9Calibration *calib,
|
double ue9_binary_to_analog(struct ue9Calibration *calib,
|
||||||
uint8_t gain, uint8_t resolution, uint16_t data);
|
int gain, uint8_t resolution, uint16_t data);
|
||||||
|
|
||||||
|
/* Temperature conversion. If calib is NULL, use uncalibrated conversions. */
|
||||||
|
double ue9_binary_to_temperature(struct ue9Calibration *calib, uint16_t data);
|
||||||
|
|
||||||
/* Compute scanrate based on the provided values. */
|
/* Compute scanrate based on the provided values. */
|
||||||
double ue9_compute_rate (uint8_t scanconfig, uint16_t scaninterval);
|
double ue9_compute_rate(uint8_t scanconfig, uint16_t scaninterval);
|
||||||
|
|
||||||
/* Choose the best ScanConfig and ScanInterval parameters for the
|
/* Choose the best ScanConfig and ScanInterval parameters for the
|
||||||
desired scanrate. Returns 0 if nothing can be chosen. */
|
desired scanrate. Returns 0 if nothing can be chosen. */
|
||||||
int ue9_choose_scan (double desired_rate, double *actual_rate,
|
int ue9_choose_scan(double desired_rate, double *actual_rate,
|
||||||
uint8_t * scanconfig, uint16_t * scaninterval);
|
uint8_t * scanconfig, uint16_t * scaninterval);
|
||||||
|
|
||||||
/* Flush data buffers */
|
/* Flush data buffers */
|
||||||
void ue9_buffer_flush (int fd);
|
void ue9_buffer_flush(int fd);
|
||||||
|
|
||||||
/* Stop stream. Returns < 0 on failure. */
|
/* Stop stream. Returns < 0 on failure. */
|
||||||
int ue9_stream_stop (int fd);
|
int ue9_stream_stop(int fd);
|
||||||
|
|
||||||
/* Start stream. Returns < 0 on failure. */
|
/* Start stream. Returns < 0 on failure. */
|
||||||
int ue9_stream_start (int fd);
|
int ue9_stream_start(int fd);
|
||||||
|
|
||||||
/* Execute a command on the UE9. Returns -1 on error. Fills the
|
/* Execute a command on the UE9. Returns -1 on error. Fills the
|
||||||
checksums on the outgoing packets, and verifies them on the
|
checksums on the outgoing packets, and verifies them on the
|
||||||
incoming packets. Data in "out" is transmitted, data in "in" is
|
incoming packets. Data in "out" is transmitted, data in "in" is
|
||||||
received. */
|
received. */
|
||||||
int ue9_command (int fd, uint8_t * out, uint8_t * in, int inlen);
|
int ue9_command(int fd, uint8_t * out, uint8_t * in, int inlen);
|
||||||
|
|
||||||
/* "Simple" stream configuration, assumes the channels are all
|
/* "Simple" stream configuration, assumes the channels are all
|
||||||
configured with the same gain. */
|
configured with the same gain. */
|
||||||
int ue9_streamconfig_simple (int fd, int *channel_list, int channel_count,
|
int ue9_streamconfig_simple(int fd, int *channel_list, int channel_count,
|
||||||
uint8_t scanconfig, uint16_t scaninterval,
|
uint8_t scanconfig, uint16_t scaninterval,
|
||||||
uint8_t gain);
|
uint8_t gain);
|
||||||
|
|
||||||
|
/* Stream configuration, each Analog Input channel can have its own gain. */
|
||||||
|
int ue9_streamconfig(int fd, int *channel_list, int channel_count,
|
||||||
|
uint8_t scanconfig, uint16_t scaninterval,
|
||||||
|
int *gain_list, int gain_count);
|
||||||
|
|
||||||
|
/* Timer configuration */
|
||||||
|
int ue9_timer_config(int fd, int *mode_list, int *value_list, int count, int divisor);
|
||||||
|
|
||||||
/* Stream data and pass it to the data callback. If callback returns
|
/* Stream data and pass it to the data callback. If callback returns
|
||||||
negative, stops reading and returns 0. Returns < 0 on error. */
|
negative, stops reading and returns 0. Returns < 0 on error. */
|
||||||
typedef int (*ue9_stream_cb_t) (int channels, uint16_t * data, void *context);
|
typedef int (*ue9_stream_cb_t) (int channels, int *channel_list, int gain_count, int *gain_list, uint16_t * data, void *context);
|
||||||
int ue9_stream_data (int fd, int channels,
|
int ue9_stream_data(int fd, int channels, int *channel_list, int gain_count, int *gain_list,
|
||||||
ue9_stream_cb_t callback, void *context);
|
ue9_stream_cb_t callback, void *context);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -42,10 +42,9 @@ const char *ue9_error_text[] = {
|
||||||
[PLL_NOT_LOCKED] = "PLL_NOT_LOCKED"
|
[PLL_NOT_LOCKED] = "PLL_NOT_LOCKED"
|
||||||
};
|
};
|
||||||
|
|
||||||
const char *
|
const char *ue9_error(int errorcode)
|
||||||
ue9_error (int errorcode)
|
|
||||||
{
|
{
|
||||||
if (errorcode > ARRAY_SIZE (ue9_error_text))
|
if (errorcode > ARRAY_SIZE(ue9_error_text))
|
||||||
return "(invalid errorcode)";
|
return "(invalid errorcode)";
|
||||||
else
|
else
|
||||||
return ue9_error_text[errorcode];
|
return ue9_error_text[errorcode];
|
||||||
|
|
|
@ -44,6 +44,6 @@
|
||||||
|
|
||||||
extern const char *ue9_error_text[];
|
extern const char *ue9_error_text[];
|
||||||
|
|
||||||
const char *ue9_error (int errorcode);
|
const char *ue9_error(int errorcode);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user