Files
sics/site_ansto/hardsup/Monitor/Monitor.c
Douglas Clowes 58099af3da Push down device specific code
r1290 | dcl | 2006-11-15 08:38:29 +1100 (Wed, 15 Nov 2006) | 2 lines
2012-11-15 12:53:00 +11:00

165 lines
4.0 KiB
C

/*********************************************************************
*
* ANSI C program:
* Monitor.c
*
*********************************************************************/
#define DEVICE_CHK(func) if (device_fatal(error=(func))) goto Error; else
#include "Monitor.h"
#include "utility.h"
#include "params.h"
#include "sock.h"
#include "device.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <libiberty.h>
#include <sys/time.h>
#include <sys/types.h>
int usage(int argc, char* argv[], const char* reason)
{
int i;
fprintf(stderr, "%s", argv[0]);
for (i = 1; i < argc; ++i)
fprintf(stderr, " %s", argv[i]);
fprintf(stderr, "\n%s\n", reason);
fprintf(stderr, "usage: %s <DEVn/CTRn> <PORT>\n",
argv[0]);
return EXIT_FAILURE;
}
int main(int argc, char* argv[])
{
int error=0;
char errBuff[2048]={'\0'};
struct timeval now;
uint64 last_poll;
char* device = DEFAULT_DEVICE;
uint dev_no;
int port = DEFAULT_LISTEN_PORT;
int idx = 1;
DEVICE* devices[MAX_DEVICES];
if (idx >= argc)
return usage(argc, argv, "no args");
if (argv[idx][0] == '-')
{
if (tolower(argv[idx][1]) == 'd')
{
if (isdigit(argv[idx][2]))
set_debug_level(argv[idx][2] - '0');
else
set_debug_level(0);
}
++idx;
}
if (idx >= argc)
return usage(argc, argv, "no device");
if (tolower(argv[idx][0]) == 'd' &&
tolower(argv[idx][1]) == 'e' &&
tolower(argv[idx][2]) == 'v' &&
isdigit(argv[idx][3]))
{
device = argv[idx];
++idx;
}
else if (tolower(argv[idx][0]) == 'p' &&
tolower(argv[idx][1]) == 'x' &&
tolower(argv[idx][2]) == 'i' &&
isdigit(argv[idx][3]))
{
device = argv[idx];
++idx;
}
else
return usage(argc, argv, "bad device");
if (idx >= argc)
return usage(argc, argv, "no port");
if (isdigit(argv[idx][0]))
{
port = atoi(argv[idx]);
++idx;
}
else
return usage(argc, argv, "bad port");
dev_no = atoi(&device[3]);
sock_init();
gettimeofday(&now, NULL);
for (idx = 0; idx < MAX_DEVICES; ++idx)
{
sprintf(device, "dev%d/ctr%d", dev_no, idx);
DEVICE_CHK(device_init(&devices[idx], device));
sock_listen(port + idx, device_command, devices[idx]);
devices[idx]->current_time = now;
}
printf("Continuously polling. Press Ctrl+C to interrupt\n");
last_poll = 1000 * (uint64) now.tv_sec + now.tv_usec / 1000;
while (1)
{
uint64 timeofday;
int timeout = 0;
int next_timeout = 0;
do {
next_timeout = 1000;
sock_check(timeout);
gettimeofday(&now, NULL);
timeofday = 1000 * (uint64) now.tv_sec + now.tv_usec / 1000;
for (idx = 0; idx < MAX_DEVICES; ++idx)
{
DEVICE* device = devices[idx];
PARAMETERS* pp = &device->params;
last_poll = 1000 * (uint64) device->current_time.tv_sec
+ device->current_time.tv_usec / 1000;
if (timeofday / pp->poll_period > last_poll / pp->poll_period)
timeout = 0;
else
{
timeout = pp->poll_period - timeofday % pp->poll_period;
if (timeout < 0)
{
if (timeout < 0)
dbg_printf(0, "Poll timeout < 0 at %d\n", timeout);
timeout = 0;
}
}
if (timeout < next_timeout)
next_timeout = timeout;
if (timeout == 0)
{
device->current_time = now;
DEVICE_CHK(device_poll(device));
}
}
if (next_timeout > 999)
next_timeout = 1;
timeout = next_timeout;
//dbg_printf(0, "Poll timeout = %d\n", timeout);
} while (timeout > 0);
}
Error:
puts("");
if (device_fatal(error))
{
device_errmsg(errBuff, sizeof(errBuff));
printf("DAQmx Error: %s\n", errBuff);
}
for (idx = 0; idx < MAX_DEVICES; ++idx)
device_term(devices[idx]);
printf("End of program\n");
return 0;
}