144 lines
3.2 KiB
C
144 lines
3.2 KiB
C
/*********************************************************************
|
|
*
|
|
* ANSI C program:
|
|
* Digital.c
|
|
*
|
|
* Description:
|
|
* This program reads digital lines on a NI-6509 Digital I/O card.
|
|
* Direction, and are all configurable.
|
|
*
|
|
*********************************************************************/
|
|
|
|
#define CNTR_CHK(func) if (cntr_fatal(error=(func))) goto Error; else
|
|
|
|
#include "Digital.h"
|
|
#include "utility.h"
|
|
#include "params.h"
|
|
#include "sock.h"
|
|
#include "dio.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[])
|
|
{
|
|
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;
|
|
int port;
|
|
int idx = 1;
|
|
|
|
if (idx >= argc)
|
|
return usage(argc, argv);
|
|
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);
|
|
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
|
|
return usage(argc, argv);
|
|
|
|
if (idx >= argc)
|
|
return usage(argc, argv);
|
|
if (isdigit(argv[idx][0]))
|
|
{
|
|
port = atoi(argv[idx]);
|
|
++idx;
|
|
}
|
|
else
|
|
return usage(argc, argv);
|
|
|
|
memset(&counter, 0, sizeof(counter));
|
|
|
|
CNTR_CHK(cntr_init(&counter, device));
|
|
// CNTR_CHK(cntr_start(&counter));
|
|
sock_init(port);
|
|
|
|
printf("Continuously polling. Press Ctrl+C to interrupt\n");
|
|
gettimeofday(&now, NULL);
|
|
last_poll = 1000 * (uint64) now.tv_sec + now.tv_usec / 1000;
|
|
while (1)
|
|
{
|
|
COUNTER* cp = &counter;
|
|
PARAMETERS* pp = &cp->params;
|
|
uint64 timeofday;
|
|
int timeout = 0;
|
|
do {
|
|
sock_check(timeout);
|
|
gettimeofday(&now, NULL);
|
|
timeofday = 1000 * (uint64) now.tv_sec + now.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)
|
|
dprintf(0, "Poll timeout < 0 at %d\n", timeout);
|
|
timeout = 0;
|
|
}
|
|
}
|
|
//dprintf(0, "Poll timeout = %d\n", timeout);
|
|
} while (timeout > 0);
|
|
cp->current_time = now;
|
|
#if 1
|
|
dprintf(0, "-%s %s %.3f %s %.3f %4d\n",
|
|
make_timestamp(&cp->current_time),
|
|
make_timestamp(&cp->sample_timer),
|
|
cntr_time_to_next_sample(cp),
|
|
make_timestamp(&cp->report_timer),
|
|
cntr_time_to_next_report(cp),
|
|
cp->sample_index);
|
|
#endif
|
|
CNTR_CHK(cntr_poll(cp));
|
|
last_poll = timeofday;
|
|
}
|
|
|
|
Error:
|
|
puts("");
|
|
if (cntr_fatal(error))
|
|
{
|
|
cntr_errmsg(errBuff, sizeof(errBuff));
|
|
printf("DAQmx Error: %s\n", errBuff);
|
|
}
|
|
cntr_term(&counter);
|
|
printf("End of program\n");
|
|
return 0;
|
|
}
|