#!/bin/ch #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/time.h> #include <cgi.h> #include <chplot.h> #include "DIO96.h" #include "control.h" #define DATABUF 2 int main() { unsigned char buf[DATABUF], data_ready; struct timeval tv; fd_set rfds; int retval, fd0, fd1, ctl, sem, n, i, NumPoints, ViewSignal; struct msg_struct msg; class CRequest Request; class CResponse Response; chchar *DataPoints, *SigNumber; class CPlot plot; /* GET THE REQUESTED NUMBER OF DATA POINTS */ DataPoints = Request.getForm("DataPoints"); NumPoints = strtod(DataPoints, NULL); SigNumber = Request.getForm("SigNumber"); ViewSignal = strtod(SigNumber, NULL); if(ViewSignal < 0 || ViewSignal > 15) { fprintf(stderr, "Incorrect Signal chosen\n"); exit(-1); } /* OPEN THE FIFOS */ if ((fd0 = open("/dev/rtf0", O_RDONLY)) == NULL) { fprintf(stderr, "Error opening /dev/rtf0\n"); exit(-1); } if ((fd1 = open("/dev/rtf1", O_RDONLY)) == NULL) { fprintf(stderr, "Error opening /dev/rtf1\n"); close(fd0); exit(-1); } if ((ctl = open("/dev/rtf2", O_WRONLY)) == NULL) { fprintf(stderr, "Error opening /dev/rtf2\n"); close(fd0); close(fd1); exit(-1); } if ((sem = open("/dev/rtf4", O_RDONLY)) == NULL) { fprintf(stderr, "Error opening /dev/rtf4\n"); close(fd0); close(fd1); close(ctl); exit(-1); } /* SETUP THE START MESSAGE */ msg.command = START_TASK; msg.period = TICK_RESOLUTION; /* 1ns per tick */ msg.conf = DIO96_CONF; msg.buffer_size = DATABUF; msg.dataPts = NumPoints; /* SEND THE MESSAGE */ if (write(ctl, &msg, sizeof(msg)) < 0) { fprintf(stderr, "Can't send a command to RT-task\n"); close(fd0); close(fd1); close(ctl); close(sem); exit(-1); } /* CREATE BUFFER TO STORE THE DATA */ unsigned char Signal[NumPoints]; int x[NumPoints]; for(i=0; i<NumPoints; i++) { Signal[i]=0; x[i]=0; } /* ACQUIRE THE DATA */ for(i=0; i<NumPoints; i++) { FD_ZERO(&rfds); FD_SET(sem, &rfds); tv.tv_sec = 2; tv.tv_usec = 0; retval = select(sem+1, &rfds, NULL, NULL, &tv); if (retval > 0) { n = read(sem, &data_ready, 1); if(data_ready==0x00) read(fd0, buf, DATABUF); if(data_ready==0x01) read(fd1, buf, DATABUF); // printf("%8b %8b\n", buf[0], buf[1]); //printf("%04d %04d\n", buf[0], buf[1]); if(ViewSignal > 7) Signal[i] = (buf[1] & 1<<(ViewSignal-8)) >> (ViewSignal-8); else Signal[i] = (buf[0] & 1<<ViewSignal) >> ViewSignal; // printf("%d\n", Signal[i]); x[i] = i; } } string_t filename = stradd(tmpnam(NULL), ".png"); /* PLOT INFO TO TEMPORARY FILE */ plot.data2D(x, Signal); plot.plotType(PLOT_PLOTTYPE_STEPS, 0, 1, 1); plot.outputType(PLOT_OUTPUTTYPE_FILE, "png color", stradd("../../htdocs", filename)); plot.title("Signal Output"); plot.label(PLOT_AXIS_Y, "Signal"); plot.label(PLOT_AXIS_X, "Data Point"); plot.axisRange(PLOT_AXIS_Y, -0.1, 1.1); plot.plotting(); /* START HTML RESPONSE */ Response.begin(); Response.title("Web-based Real-Time Data Acquisition"); printf( "<HTML>\n" " <HEAD>\n" " <TITLE>Web-based Real-Time Data Acquisition</TITLE>\n" " </HEAD>\n" " <BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" VLINK=\"#FF0000\">\n" " <P ALIGN=\"CENTER\">\n" " <IMG SRC=\"%s\">\n" " <BR><BR>\n" " <A HREF=\"../../realtime/form.html\">RESTART</A>\n" " </BODY>\n" "</HTML>\n", filename); Response.end(); /* CLOSE THE OPENED FIFOS */ close(fd0); close(fd1); close(ctl); close(sem); /* REMOVE PLOT */ // rm -rf $(stradd("../../htdocs", filename)) return 0; }