Commit 1e488563 authored by Matheus Müller's avatar Matheus Müller
Browse files

Verso 0.1 operaciontal com plot periodico

parent 77987ca2
......@@ -36,14 +36,6 @@
#define N957_SAMPLE_NUM_BITS 13
#define N957_MAX_HISTO_SAMPLES (1<<N957_SAMPLE_NUM_BITS)
// Executable gnuplot. NOTE: use pgnuplot instead of wgnuplot in Windows, otherwise
// the pipe will not work.
#ifdef WIN32
#define GNUPLOT_COMMAND "pgnuplot"
#else
#define GNUPLOT_COMMAND "gnuplot"
#endif
////////////////////////////////////////////
// File local variables declaration
////////////////////////////////////////////
......@@ -101,9 +93,11 @@ int main(int argc, char **argv)
{
int w;
unsigned long int histograma[8192];
for(w=0; w<8191; w++)
unsigned int histograma[8192];
unsigned int temp = 0;
FILE* log;
short int fator = 1;
for(w=0; w<8192; w++)
{
histograma[w] = 0;
......@@ -118,8 +112,6 @@ int main(int argc, char **argv)
unsigned long *histo_buff= NULL; // Histogram data buffer
int32_t tot_data_read= 0;
int i;
FILE* gnu_plot_pipe= NULL; // gnu_plot pipe
long next_refresh= 0;
char aux_string[ 100];
BOOL paused= FALSE;
BOOL do_exit= FALSE;
......@@ -133,17 +125,7 @@ int main(int argc, char **argv)
memset( &user_setting, 0, sizeof( user_setting));
//
// print header
printf( "\n");
printf( "-------------------------------------------------------\n");
printf( "- N 9 5 7 R U N -\n");
printf( "- -\n");
printf( "- www.caen.it rev.02.03 -\n");
printf( "-------------------------------------------------------\n");
printf( "\n");
//
// init user setting module
if( !user_settings_open( &user_setting))
{
......@@ -169,20 +151,20 @@ int main(int argc, char **argv)
{
// Get firmware revision
char fw_rev[10];
TRACE( "\nN957 FW Rev. ");
//TRACE( "\nN957 FW Rev. ");
if( ( N957_error_code= N957_GetFWRelease( user_setting.m_N957_handle, fw_rev, 10))!= N957Success)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -3;
goto exit_point;
}
TRACE( fw_rev);
// TRACE( fw_rev);
}
TRACE( "\n");
TRACE( "\nHit 's' to start acquisition mode , any other to quit ...\n");
// TRACE( "\n");
// TRACE( "\nHit 's' to start acquisition mode , any other to quit ...\n");
switch( getch()) {
case 's':
case 'S':
......@@ -193,63 +175,63 @@ int main(int argc, char **argv)
}
// Scaler Reading
TRACE("\nADC Conversions Number : ");
// TRACE("\nADC Conversions Number : ");
if( ( N957_error_code=N957_GetScaler(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -5;
goto exit_point;
}
TRACE1("%d", data32);
// TRACE1("%d", data32);
// Timer Reading
TRACE("\nTime (ms) : ");
// TRACE("\nTime (ms) : ");
if( ( N957_error_code=N957_GetTimer(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -6;
goto exit_point;
}
TRACE1("%d", data32);
// TRACE1("%d", data32);
// Live Time Reading
TRACE("\nLive Time (ms) : ");
// TRACE("\nLive Time (ms) : ");
if( ( N957_error_code=N957_GetLiveTime(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -7;
goto exit_point;
}
TRACE1("%d",data32);
// TRACE1("%d",data32);
// Start acquisition with specific mode
TRACE1( "\nN957 Start Acquisition mode %i : ", user_setting.m_mode);
// TRACE1( "\nN957 Start Acquisition mode %i : ", user_setting.m_mode);
if( ( N957_error_code= N957_StartAcquire( user_setting.m_N957_handle, user_setting.m_mode))!= N957Success)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -8;
goto exit_point;
}
TRACE( "Ok");
// TRACE( "Ok");
if( user_setting.m_debug) {
//
// If debugging features enable, convertion well be simulated by software
//
// Set software convertion
TRACE( "\nN957 Debug Software convertion enable : ");
// TRACE( "\nN957 Debug Software convertion enable : ");
if( ( N957_error_code= N957_SetSwConvFlag( user_setting.m_N957_handle, N957_TRUE))!= N957Success)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -9;
goto exit_point;
}
TRACE( "Ok");
// TRACE( "Ok");
}
// Allocate buffer storage
......@@ -257,7 +239,7 @@ int main(int argc, char **argv)
if( data_buff== NULL)
{
// Insufficient memory
TRACE1( "N957 Insufficient memory to allocate data buffer (%li bytes)", (long)( user_setting.m_bldim* sizeof( N957_UINT16)));
// TRACE1( "N957 Insufficient memory to allocate data buffer (%li bytes)", (long)( user_setting.m_bldim* sizeof( N957_UINT16)));
ret_val= -10;
goto exit_point;
}
......@@ -266,7 +248,7 @@ int main(int argc, char **argv)
if( histo_buff== NULL)
{
// Insufficient memory
TRACE1( "N957 Insufficient memory to allocate histogram data buffer (%li bytes)", (long)N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
// TRACE1( "N957 Insufficient memory to allocate histogram data buffer (%li bytes)", (long)N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
ret_val= -11;
goto exit_point;
}
......@@ -276,43 +258,13 @@ int main(int argc, char **argv)
// Create output file
if( ( out_file= fopen( user_setting.m_log_filename, "wt"))== NULL)
{
TRACE1( "N957 output file creation failure '%s'", user_setting.m_log_filename);
// TRACE1( "N957 output file creation failure '%s'", user_setting.m_log_filename);
ret_val= -12;
goto exit_point;
}
}
/*// open gnuplot for the first time
{
char *gnu_plot_filename= (char*)malloc( MAX_FILENAME_LENGHT);
sprintf( gnu_plot_filename, "%s%s", user_setting.m_gnu_plot_path, GNUPLOT_COMMAND);
// open the pipe
if(( gnu_plot_pipe = popen( gnu_plot_filename, "w"))== NULL){
TRACE( "N957 GNU Plot pipe creation failure");
ret_val= -13;
goto exit_point;
}
//// set some variables in gnuplot
//fprintf( gnu_plot_pipe, "ch = %d; mod = %d; unit = %d; sens = %f\n", ChSel, ModSel, Unit, Sensitivity);
free( gnu_plot_filename);
}
// gnu plot commands
strcpy( aux_string, "load 'plot_histo.cfg'\n");
if( fwrite( aux_string, 1, strlen( aux_string), gnu_plot_pipe)!= strlen( aux_string)) {
// out file write error
TRACE( "\n Pipe write failure\n");
ret_val= -14;
goto exit_point;
}*/
TRACE( "\nAcquisition running ...\n");
TRACE( "\nHit 'r' to reset histogram, any other to quit ...");
//TRACE( "\nHit 'p' to pause/resume, any other to quit ...");
TRACE( "\nAny other to quit ...");
//
// Main loop
do_exit= FALSE;
while( !do_exit) {
......@@ -356,8 +308,8 @@ int main(int argc, char **argv)
//
// ReadData
if( ( N957_error_code= N957_ReadData( user_setting.m_N957_handle, data_buff, &data_read))!= N957Success) {
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -15;
goto exit_point;
}
......@@ -370,29 +322,61 @@ int main(int argc, char **argv)
case 'r':
case 'R':
// reset the histogram here
memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
//memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
for(w=0; w<8192; w++)
histograma[w] = 0;
break;
case 'd':
while(getch() != 'e')
Sleep( 1);
case 'p':
case 'P':
paused= !paused;
break;
case 'a':
fator = 1;
break;
case 'b':
fator = 2;
break;
case 'c':
fator = 4;
break;
case 'd':
fator = 8;
break;
case 'e':
fator = 16;
break;
case 'f':
fator = 32;
break;
case 'g':
fator = 64;
break;
case 'h':
fator = 128;
break;
case 'i':
fflush(stdout);
for(w=0; w<8191; w++)
for(w=0; w<(8192-fator+1); w)
{
TRACE1("%li\n",histograma[w]);
for(i=w; i<=(w+fator-1); i++)
temp = temp + histograma[i];
TRACE1("%i\n",temp);
temp = 0;
w=w+fator;
}
for(w=0; w<8191; w++)
{
histograma[w] = 0;
break;
case 'z':
log = fopen("save.dat", "w+");
for(w=0; w<8192; w++)
fprintf(log, "%i\n", histograma[w]);
fclose(log);
break;
case 'y':
log = fopen("save.dat", "r");
for(w=0; w<8192; w++){
fscanf(log, "%i", &histograma[w]);
}
fclose(log);
break;
default:
do_exit= TRUE;
break;
......@@ -405,132 +389,58 @@ int main(int argc, char **argv)
if( out_file) {
sprintf( aux_string, "%04d\n", data_buff[ i]);
histograma[atoi(aux_string)+1] = histograma[atoi(aux_string)+1] +1;
TRACE1("%d \n", atoi(aux_string));
/*if(fwrite( aux_string, 1, strlen( aux_string), out_file)!= strlen( aux_string)) {
// out file write error
TRACE( "\n output file write failure\n");
ret_val= -16;
goto exit_point;
}*/
}
}
/*if( get_time()>= next_refresh+1000) {
FILE* histo_file= NULL;
next_refresh= get_time()+ user_setting.m_gnu_plot_refresh;
for(w=0; w<8191; w++)
{
TRACE1("%li ",histograma[w]);
}
TRACE("\n \n \n \n");
}
if(( histo_file= fopen( HISTO_FILENAME, "wt"))== NULL){
// out file write error
TRACE1( "N957 output file creation failure '%s'", HISTO_FILENAME);
ret_val= -17;
goto exit_point;
}
// Save data to histogram file
for( i= 0; i< N957_MAX_HISTO_SAMPLES; i++) {
// Check for user commands
if( kbhit()) {
switch( getch()) {
case 'r':
case 'R':
// reset the histogram here
memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
break;
//case 'p':
//case 'P':
// paused= !paused;
// break;
default:
do_exit= TRUE;
break;
}
}
sprintf( aux_string, "%ld\n", histo_buff[ i]);
//sprintf( aux_string, "%f\t%ld\n", (float)i* user_setting.m_gnu_plot_x_scale, histo_buff[ i]);
if( fwrite( aux_string, 1, strlen( aux_string), histo_file)!= strlen( aux_string)){
// out file write error
TRACE( "\n Histogram file write failure\n");
ret_val= -18;
goto exit_point;
//TRACE1("%d \n", atoi(aux_string));
}
}
fclose( histo_file);
// write to gnuplot
strcpy( aux_string, "plot 'histo.dat' with histogram 2\n");
// strcpy( aux_string, "set style histogram rows\n plot 'histo.dat' using 2, '' using 4, '' using 6:xticlabels(1)\n");
if( fwrite( aux_string, 1, strlen( aux_string), gnu_plot_pipe)!= strlen( aux_string))
{
// out file write error
TRACE( "\n Pipe write failure\n");
ret_val= -19;
goto exit_point;
}
fflush( gnu_plot_pipe);
Sleep( 1);
}*/
}
if( user_setting.m_debug)
{
//
// clear software convertion
TRACE( "\nN957 Debug Software convertion disable : ");
// TRACE( "\nN957 Debug Software convertion disable : ");
if( ( N957_error_code= N957_SetSwConvFlag( user_setting.m_N957_handle, N957_FALSE))!= N957Success)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
//TRACE( "\n");
ret_val= -20;
goto exit_point;
}
TRACE( "Ok");
// TRACE( "Ok");
}
//Scaler Reading
TRACE("\nADC Conversions Number : ");
// TRACE("\nADC Conversions Number : ");
if( ( N957_error_code=N957_GetScaler(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -21;
goto exit_point;
}
TRACE1("%d", data32);
TRACE1("%d\n", data32);
//Timer Reading
TRACE("\nTime (ms) : ");
// TRACE("\nTime (ms) : ");
if( ( N957_error_code=N957_GetTimer(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -22;
goto exit_point;
}
TRACE1("%d", data32);
TRACE1("%d\n", data32);
//Live Time Reading
TRACE("\nLive Time (ms) : ");
// TRACE("\nLive Time (ms) : ");
if( ( N957_error_code=N957_GetLiveTime(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -23;
goto exit_point;
}
TRACE1("%d",data32);
TRACE1("%d\n",data32);
exit_point:
/////////////////////////////////////////
......@@ -541,45 +451,32 @@ exit_point:
// Demo application specific
/////////////////////////////////////////
TRACE( "\n\nHit any key to exit...");
//TRACE( "\n\nHit any key to exit...");
getch();
// Stop acquisition
TRACE( "\nN957 Stop Acquisition : ");
// TRACE( "\nN957 Stop Acquisition : ");
if( ( N957_error_code= N957_StopAcquire( user_setting.m_N957_handle))!= N957Success)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -24;
goto exit_point;
}
TRACE( "Ok");
// TRACE( "Ok");
/*if( out_file!= NULL)
{
fclose( out_file);
}
if( gnu_plot_pipe!= NULL)
{
fprintf( gnu_plot_pipe, "exit\n");
pclose( gnu_plot_pipe);
}
if( data_buff!= NULL)
{
free( data_buff);
}*/
// SW Clear
TRACE( "\nSW Clear : ");
//TRACE( "\nSW Clear : ");
if( ( N957_error_code= N957_SwClear(user_setting.m_N957_handle)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
// TRACE( N957_DecodeError( N957_error_code));
// TRACE( "\n");
ret_val= -25;
goto exit_point;
}
TRACE( "Ok");
//TRACE( "Ok");
// close modules
user_settings_close( &user_setting);
......
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
from subprocess import PIPE
import subprocess
import os
import pexpect
import Tkinter as tk
import tkMessageBox
import matplotlib.pyplot as plt
from pylab import *
import time
##############################################################################
##############################################################################
# Drivers loading
os.system('sh V0.6/n957_load.2.6')
# Subprocess variable
subp = subprocess.Popen('./N957Run', shell=True, stdin=PIPE)#, stdout=PIPE)
TempData = list()
Data=list()
FinalData=list()
for a in range(8192):
Data.insert(a, 0)
child = pexpect.spawn('./N957Run')
time.sleep(3.3)
child.send('s')
##############################################################################
##############################################################################
# Histrogram
def start_subprocess():
global subp
subp.stdin.write("s")
t = 2000
bmin = 0
bmax = 8192
Data=list()
for a in range(8192):
Data.insert(a, 0)
xx = arange(0, 8192)
temp, = plot(xx, xx*0)
temp, = plot(xx, xx*0, 'y.', lw=0.9)
plt.grid(True)
plt.xlabel('Channel')
plt.ylabel('Counts')
ax = subplot(111)
ax.xaxis.set_major_locator(MultipleLocator(1024))
ax.xaxis.set_minor_locator(MultipleLocator(128))
ax.xaxis.grid(True,'minor')
ax.xaxis.grid(True,'major',linewidth=2)
##############################################################################
##############################################################################
Interface = dict(frames=list(), buttons=list(), labels=list(), spinboxes=list(), entries=list(), labelframes=list())
Livetime = dict(ADC_Conversion='0' , Time='0' , LiveTime='0' )
def set_fator():
global Interface, child, bmax
if int(Interface['spinboxes'][0].get()) == 8192:
child.send('a')
bmax = 8192
elif int(Interface['spinboxes'][0].get()) == 4096:
child.send('b')
bmax = 4096
elif int(Interface['spinboxes'][0].get()) == 2048:
child.send('c')
bmax = 2048
elif int(Interface['spinboxes'][0].get()) == 1024:
child.send('d')
bmax = 1024
elif int(Interface['spinboxes'][0].get()) == 512:
child.send('e')
bmax = 512
elif int(Interface['spinboxes'][0].get()) == 256:
child.send('f')
bmax = 256
elif int(Interface['spinboxes'][0].get()) == 128:
child.send('g')
bmax = 128
elif int(Interface['spinboxes'][0].get()) == 64:
child.send('h')
bmax = 64
def dump():
global child
child.send('i')
def graphic():
global subp, Data, FinalData, TempData
#subp.stdin.write("d")
#s = loadtxt('data.log', dtype='string')
for k in range(len(TempData)):
i = int(TempData[k], 16)
Data[i] = Data[i] + 1
if Interface['spinboxes'][0].get() != 8192:
Num = int(Interface['spinboxes'][0].get())
dx = 8192/Num
for k in arange(Num):
FinalData.insert(k, 0)
for k in arange(0, Num, dx):
i = i + 1
for n in range(dx):
FinalData[i] = FinalData[i] + Data[k+n]
temp.set_xdata(arange(Num))
temp.set_ydata(FinalData)
ylim([0,max(FinalData)*1.1])
xlim([0,len(FinalData)])
else:
temp.set_xdata(arange(0,8192))
global child, Data, Interface, bmin, bmax
for n in arange(0, int(Interface['spinboxes'][0].get())):
Data[n] = int(child.readline().strip("\r\n"))
temp.set_ydata(Data)
ylim([0,max(Data)*1.1])