/* * * wattscope.c ... PC code for a wattmeter with scope and datalogger * * * * Copyright (C) 2012 Jiri Pittner * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * * * * * */ #include #include #include #include #include void xerror(error_text) char error_text[]; { fprintf(stderr,"wattscope: %s\n",error_text); exit(10); } int main(int argc, char **argv) { char line[1024]; int plotx=1; //time by default int iploty=0; int nploty=1; int *ploty=malloc(nploty*sizeof(int)); ploty[0]=3; //true power while((--argc>0)&&((*++argv)[0]=='-')){ if(argc>0) switch ((*argv)[1]){ case 'x': if(--argc <0) xerror("missing format after option -x"); sscanf(*++argv,"%d",&plotx); break; case 'y': if(--argc <0) xerror("missing format after option -y"); ++iploty; if(iploty > nploty) {ploty=realloc(ploty,iploty*sizeof(int)); nploty=iploty;} sscanf(*++argv,"%d",ploty+iploty-1); break; default : xerror("unknown option"); } } if(argc!=0) xerror("wrong command line"); int mode=0; FILE *f,*g,*l,*h; l=fopen("/tmp/wattscope.log","w"); if(!l) {perror("cannot fopen /tmp/wattscope.log for w\n");exit(1);} f=fopen("/tmp/wattscope.tmp","w"); if(!f) {perror("cannot fopen /tmp/wattscope.tmp for w\n");exit(1);} g=popen("gnuplot","w"); if(!g) {perror("cannot popen gnuplot for w\n");exit(1);} h=popen("gnuplot","w"); if(!h) {perror("cannot popen gnuplot for w\n");exit(1);} setlinebuf(l); setlinebuf(g); setlinebuf(h); fprintf(h,"set xlabel 'Time'\n"); fprintf(h,"set autoscale y\n"); fprintf(h,"set grid\n"); fprintf(g,"set xlabel 'Time'\n"); fprintf(g,"set ylabel 'Volts'\n"); fprintf(g,"set y2label 'Ampers'\n"); fprintf(g,"set grid\n"); fprintf(g,"set format y2 \"%%.3f\"\n"); fprintf(g,"set autoscale y\n"); fprintf(g,"set autoscale y2\n"); fprintf(g,"set y2tics nomirror tc lt 3\n"); fprintf(g,"set xrange [0:0.02]\n"); int first=1; int nline=0; while(fgets(line,1024,stdin)) { if(mode==0) { if(!strncmp(line,"Time =",6)) { nline=0; fprintf(l,"\n"); fprintf(h,"plot "); int i; for(i=0; i0?", ":""),plotx, ploty[i]); } fprintf(h,"\n"); } char *p = strchr(line, '='); if(p) { double x=0; sscanf(p+1,"%lf",&x); fprintf(l,"%g ",x); } } if(!strncmp(line,"BEGIN SAMPLE POINTS",19)) { if(ftruncate(fileno(f),0)) perror("cannot ftruncate"); mode=1; continue; } if(!strncmp(line,"END SAMPLE POINTS",17)) { fflush(f); usleep(50000); mode=0; if(first) { first=0; fprintf(g,"plot \"/tmp/wattscope.tmp\" using 1:2 axes x1y1 title 'Voltage' with lines, \"/tmp/wattscope.tmp\" using 3:4 axes x1y2 title 'Current' with lines, \"/tmp/wattscope.tmp\" using 5:6 axes x1y1 title 'Power' with lines\n"); } else { fprintf(g,"replot\n"); } continue; } fputs(line,mode==0?stdout:f); } fclose(f); pclose(g); return 0; }