Refinement alongside Digital

r1198 | dcl | 2006-10-26 18:38:13 +1000 (Thu, 26 Oct 2006) | 2 lines
This commit is contained in:
Douglas Clowes
2006-10-26 18:38:13 +10:00
parent 7e43aa3e16
commit 7dfa37057b
6 changed files with 75 additions and 109 deletions

View File

@@ -7,7 +7,7 @@ CDEBUG = -ggdb3 -Wall
LDFLAGS += -g
TARGETS = Monitor
OBJS = Monitor.o params.o utility.o sock.o display.o cntr.o hctr.o
OBJS = Monitor.o params.o utility.o sock.o display.o device.o hware.o
ifeq ($(MAKECMDGOALS),rlp)
OBJECTS =
LIBS =
@@ -32,28 +32,25 @@ all: $(TARGETS)
rlp: all
clean:
rm -f $(OBJS) Monitor Pulser.o Pulser core
rm -f $(OBJS) Monitor core
Monitor : $(OBJS)
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBFLAGS) -ggdb3
Monitor.o: Monitor.c Monitor.h params.h utility.h sock.h cntr.h
Monitor.o: Monitor.c Monitor.h params.h utility.h sock.h device.h
cntr.o: cntr.c cntr.h params.h sock.h
device.o: device.c device.h params.h sock.h
display.o: display.c display.h utility.h params.h sock.h cntr.h
display.o: display.c display.h utility.h params.h sock.h device.h
hctr.o: hctr.c hctr.h
hware.o: hware.c hware.h
params.o: params.c params.h sock.h
sock.o: sock.c sock.h utility.h display.h cntr.h
sock.o: sock.c sock.h utility.h display.h device.h
utility.o: utility.c utility.h
Pulser : Pulser.c
$(CXX) -o $@ $< $(CXXFLAGS) $(CDEBUG) $(LDFLAGS) $(LIBFLAGS)
%.o : %.c
$(CXX) -c -o $@ $< $(CXXFLAGS) $(CDEBUG)

View File

@@ -3,44 +3,15 @@
* ANSI C program:
* Monitor.c
*
* Description:
* This program counts digital events on a NI-6602
* Counter Input Channel. The Device, Counter Channel, Initial Count, Count
* Direction, and are all configurable.
*
* Edges are counted on the counter's default input terminal (refer
* to the I/O Connections Overview section below for more
* information), but could easily be modified to count edges on a
* PFI or RTSI line.
*
* Instructions for Running:
* 1. Select the Device and Channel which corresponds to the counter
* you want to count on.
* 2. Select the TCP/IP port number you want to listen on.
* 3. Enter the Initial Count, Count Direction, to specify how you want
* the counter to count.
*
* Steps:
* 1. Create a task.
* 2. Create a Counter Input channel to Count Events. The Edge
* parameter is used to determine if the counter will increment
* on rising or falling edges.
* 3. Call the Start function to arm the counter and begin
* counting. The counter will be preloaded with the Initial
* Count.
* 4. The counter will be continually polled.
* 5. Call the Clear Task function to clear the Task.
* 6. Display an error if any.
*
*********************************************************************/
#define CNTR_CHK(func) if (cntr_fatal(error=(func))) goto Error; else
#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 "cntr.h"
#include "device.h"
#include <unistd.h>
#include <stdlib.h>
@@ -54,8 +25,6 @@
#include <sys/time.h>
#include <sys/types.h>
#define MAX_COUNTERS 8
int usage(int argc, char* argv[], const char* reason)
{
int i;
@@ -74,11 +43,11 @@ int main(int argc, char* argv[])
char errBuff[2048]={'\0'};
struct timeval now;
uint64 last_poll;
char* device = DEFAULT_COUNTER_DEVICE;
char* device = DEFAULT_DEVICE;
uint dev_no;
int port = DEFAULT_LISTEN_PORT;
int idx = 1;
COUNTER* counters[MAX_COUNTERS];
DEVICE* devices[MAX_DEVICES];
if (idx >= argc)
return usage(argc, argv, "no args");
@@ -128,12 +97,12 @@ int main(int argc, char* argv[])
dev_no = atoi(&device[3]);
sock_init();
gettimeofday(&now, NULL);
for (idx = 0; idx < MAX_COUNTERS; ++idx)
for (idx = 0; idx < MAX_DEVICES; ++idx)
{
sprintf(device, "dev%d/ctr%d", dev_no, idx);
CNTR_CHK(cntr_init(&counters[idx], device));
sock_listen(port + idx, cntr_command, counters[idx]);
counters[idx]->current_time = now;
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");
@@ -148,12 +117,12 @@ int main(int argc, char* argv[])
sock_check(timeout);
gettimeofday(&now, NULL);
timeofday = 1000 * (uint64) now.tv_sec + now.tv_usec / 1000;
for (idx = 0; idx < MAX_COUNTERS; ++idx)
for (idx = 0; idx < MAX_DEVICES; ++idx)
{
COUNTER* cp = counters[idx];
PARAMETERS* pp = &cp->params;
last_poll = 1000 * (uint64) cp->current_time.tv_sec
+ cp->current_time.tv_usec / 1000;
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
@@ -170,18 +139,18 @@ int main(int argc, char* argv[])
next_timeout = timeout;
if (timeout == 0)
{
cp->current_time = now;
device->current_time = now;
#if 1
dbg_printf(0, "%d:-%s %s %.3f %s %.3f %4d\n",
idx,
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);
make_timestamp(&device->current_time),
make_timestamp(&device->sample_timer),
device_time_to_next_sample(device),
make_timestamp(&device->report_timer),
device_time_to_next_report(device),
device->sample_index);
#endif
CNTR_CHK(cntr_poll(cp));
DEVICE_CHK(device_poll(device));
}
}
if (next_timeout > 999)
@@ -193,13 +162,13 @@ int main(int argc, char* argv[])
Error:
puts("");
if (cntr_fatal(error))
if (device_fatal(error))
{
cntr_errmsg(errBuff, sizeof(errBuff));
device_errmsg(errBuff, sizeof(errBuff));
printf("DAQmx Error: %s\n", errBuff);
}
for (idx = 0; idx < MAX_COUNTERS; ++idx)
cntr_term(counters[idx]);
for (idx = 0; idx < MAX_DEVICES; ++idx)
device_term(devices[idx]);
printf("End of program\n");
return 0;
}

View File

@@ -5,7 +5,7 @@
#ifndef _MONITOR_H_
#define _MONITOR_H_
#define DEFAULT_COUNTER_DEVICE "DEV1/CTR0"
#define DEFAULT_DEVICE "DEV1"
#define DEFAULT_LISTEN_PORT 3000
#endif

View File

@@ -2,7 +2,7 @@
#include "utility.h"
#include "params.h"
#include "sock.h"
#include "cntr.h"
#include "device.h"
#include <ctype.h>
@@ -162,8 +162,8 @@ void put_form(int n)
dbg_printf(0, "put_form\n");
BUFFER html;
BUFFER buffer;
COUNTER* cp = (COUNTER*) sock_device(n);
PARAMETERS* pp = &cp->params;
DEVICE* device = (DEVICE*) sock_device(n);
PARAMETERS* pp = &device->params;
char *bp;
buffer.length = 0;
bp = &buffer.body[buffer.length];
@@ -323,24 +323,24 @@ void put_page(int n)
dbg_printf(0, "put_page\n");
BUFFER html;
BUFFER buffer;
COUNTER* cp = (COUNTER*) sock_device(n);
PARAMETERS* pp = &cp->params;
DEVICE* device = (DEVICE*) sock_device(n);
PARAMETERS* pp = &device->params;
SAMPLE* sp;
char *bp;
int refresh;
#if 1
{
refresh = (int) cntr_time_to_next_report(cp) + 1;
refresh = (int) device_time_to_next_report(device) + 1;
}
#else
make_report(cp);
make_report(device);
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
#endif
if (refresh < MIN_REFRESH)
refresh = MIN_REFRESH;
if (refresh > MAX_REFRESH)
refresh = MAX_REFRESH;
sp = &cp->report;
sp = &device->report;
buffer.length = 0;
bp = &buffer.body[buffer.length];
snprintf(bp, sizeof(buffer.body) - buffer.length,
@@ -351,25 +351,25 @@ void put_page(int n)
"<tr><th align=\"right\">Field</th>\r\n"
"<th align=\"left\">Value</th></tr>\r\n");
buffer.length += strlen(bp);
show_text(&buffer, "State", cp->state == counter_stopped ? "STOPPED" :
cp->state == counter_running ? "RUNNING" :
cp->state == counter_paused ? "PAUSED" : "IDLE");
show_text(&buffer, "State", device->state == counter_stopped ? "STOPPED" :
device->state == counter_running ? "RUNNING" :
device->state == counter_paused ? "PAUSED" : "IDLE");
show_text(&buffer, "Direction", pp->direction == COUNT_UP ? "UP" : "DOWN");
show_int(&buffer, "Scan", pp->poll_period);
show_int(&buffer, "Sample", pp->sample_period);
show_int(&buffer, "Report", pp->report_period);
show_time(&buffer, "Start Time", &cp->start_time, false);
show_time(&buffer, "Current Time", &cp->current_time, false);
show_time(&buffer, "Start Time", &device->start_time, false);
show_time(&buffer, "Current Time", &device->current_time, false);
{
struct timeval tv = cp->stop_time;
if (cp->state == counter_running || cp->state == counter_paused)
struct timeval tv = device->stop_time;
if (device->state == counter_running || device->state == counter_paused)
{
tv = cp->current_time;
tv = device->current_time;
}
time_sub(&tv, &cp->start_time);
time_sub(&tv, &device->start_time);
show_time(&buffer, "Elapsed", &tv, true);
}
show_time(&buffer, "Runtime", &cp->accumulated, true);
show_time(&buffer, "Runtime", &device->accumulated, true);
show_time(&buffer, "Report Time", &sp->timestamp, false);
show_counter(&buffer, "Counter", sp->counter_value);
show_int(&buffer, "Num Polls", sp->num_polls);
@@ -382,9 +382,9 @@ void put_page(int n)
if (pp->range_check_enable)
{
char* result;
if (cp->range_error == 1)
if (device->range_error == 1)
result = "<font color=\"BLUE\">LOW</font>";
else if (cp->range_error == 2)
else if (device->range_error == 2)
result = "<font color=\"RED\">HIGH</font>";
else
result = "<font color=\"GREEN\">OK</font>";
@@ -439,6 +439,7 @@ void put_page(int n)
void process_form(int n, BUFFER* bp)
{
dbg_printf(0, "process_form\n");
DEVICE* device = (DEVICE*) sock_device(n);
int i, j;
int state;
char name[80];
@@ -446,7 +447,6 @@ void process_form(int n, BUFFER* bp)
char hex_str[3];
unsigned int hex_val;
char* tp;
COUNTER* cp = (COUNTER*) sock_device(n);
state = 0;
j = 0;
for (i = 0; i < bp->length; ++i)
@@ -498,7 +498,7 @@ void process_form(int n, BUFFER* bp)
if (state == 9)
{
dbg_printf(0, "name=\"%s\", value=\"%s\"\n", name, value);
if (param_set(&cp->params, name, value))
if (param_set(&device->params, name, value))
;
else if (strcmp(name, "xxx"))
{
@@ -522,16 +522,16 @@ void put_page_refresh(int n)
BUFFER html;
BUFFER buffer;
char* bp;
COUNTER* cp = (COUNTER*) sock_device(n);
DEVICE* device = (DEVICE*) sock_device(n);
int refresh;
#if 1
{
refresh = (int) cntr_time_to_next_report(cp) + 1;
refresh = (int) device_time_to_next_report(device) + 1;
}
#else
PARAMETERS* pp = &cp->params;
make_report(cp);
PARAMETERS* pp = &device->params;
make_report(device);
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
#endif
if (refresh < MIN_REFRESH)
@@ -572,19 +572,19 @@ void put_page_refresh(int n)
void put_form_refresh(int n)
{
dbg_printf(0, "put_form_refresh\n");
DEVICE* device = (DEVICE*) sock_device(n);
BUFFER html;
BUFFER buffer;
char* bp;
COUNTER* cp = (COUNTER*) sock_device(n);
int refresh;
#if 1
{
refresh = (int) cntr_time_to_next_report(cp) + 1;
refresh = (int) device_time_to_next_report(device) + 1;
}
#else
PARAMETERS* pp = &cp->params;
make_report(cp);
PARAMETERS* pp = &device->params;
make_report(device);
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
#endif
if (refresh < MIN_REFRESH)
@@ -625,13 +625,13 @@ void put_form_refresh(int n)
void process_command(int n, BUFFER* bp)
{
dbg_printf(0, "process_command(%d, %s)\n", n, bp->body);
DEVICE* device = (DEVICE*) sock_device(n);
bool sics = false;
int error = 1;
char command[80];
char param[80];
int len = 0;
char* tp = bp->body;
COUNTER* cp = static_cast<COUNTER*>(sock_device(n));
while (isspace(*tp))
++tp;
len = 0;
@@ -659,13 +659,13 @@ void process_command(int n, BUFFER* bp)
while (isspace(*tp))
++tp;
if (strcasecmp(command, "START") == 0)
error = cntr_start(cp);
error = device_start(device);
else if (strcasecmp(command, "STOP") == 0)
error = cntr_stop(cp);
error = device_stop(device);
else if (strcasecmp(command, "PAUSE") == 0)
error = cntr_pause(cp);
error = device_pause(device);
else if (strcasecmp(command, "RESUME") == 0)
error = cntr_resume(cp);
error = device_resume(device);
else if (strcasecmp(command, "REPORT") == 0)
{
int match = 1;
@@ -688,23 +688,23 @@ void process_command(int n, BUFFER* bp)
{
/* set parameter */
dbg_printf(0, "SET %s\n", tp);
if (param_set_cmd(&cp->params, tp))
if (param_set_cmd(&device->params, tp))
error = 0;
}
else if (strcasecmp(command, "GET") == 0)
{
/* get parameter */
param_get_cmd(&cp->params, tp, n);
param_get_cmd(&device->params, tp, n);
return;
}
else if (strcasecmp(command, "READ") == 0)
{
cntr_read(cp, n);
device_read(device, n);
return;
}
else if (!sics)
{
cntr_send(cp, n);
device_send(device, n);
return;
}
if (error == 0)

View File

@@ -56,7 +56,7 @@ static struct param_command_t {
{CMD_SOURCE, TXT_SOURCE},
{0, NULL}
};
#define NUM_CMDS ((int) (sizeof(param_command)/sizeof(param_command[0])))
#define NUM_CMDS ((int) ((sizeof(param_command)/sizeof(param_command[0]))))
static int name_to_command(char* name)
{

View File

@@ -35,7 +35,7 @@ double time_diff(struct timeval* later, struct timeval* earlier)
{
double delta =
((double) later->tv_sec - (double) earlier->tv_sec)
+ 0.000001 * ((double) later->tv_usec - (double) earlier->tv_usec);
+ 0.000001 * ((double) later->tv_usec - (double) earlier->tv_usec);
return delta;
}
@@ -64,7 +64,7 @@ int time_sub(struct timeval* later, struct timeval* earlier)
int time_cmp(struct timeval* later, struct timeval* earlier)
{
double delta = (1.0 * later->tv_sec + 0.000001 * later->tv_usec) -
(1.0 * earlier->tv_sec + 0.000001 * earlier->tv_usec);
(1.0 * earlier->tv_sec + 0.000001 * earlier->tv_usec);
if (delta == 0.0)
return 0;
else if (delta > 0.0)