Rapid Block mode with two channels

Post your C and C++ discussions here
Post Reply
elemosci
Newbie
Posts: 0
Joined: Mon Jul 22, 2013 8:40 am

Rapid Block mode with two channels

Post by elemosci » Mon Jul 22, 2013 9:09 am

Hello,

I want to read two channels with rapid block mode.
I write the next code,but it doesnot work because it only read one channel.

Code: Select all

PICO_STATUS BlockDataHandler(UNIT * unit,char *directorio,int cont_fich, int longitud_i)
{
	int i, j, aa, aa1, aa2;
	float timeInterval;
	long sampleCount= BUFFER_SIZE;
	FILE * fp, * fpb;
	long maxSamples;
	short **buffer, **bufferb;//s[PS6000_MAX_CHANNEL_BUFFERS];
	
    long timeIndisposed;
	//char  *directorio;//[22]="C:/CAPTURASPICOSCOPE/";
	char *numero;
    char *nombre_fichero, *nombre_ficherob;
   	clock_t clk1,clk2;
	float tiempo;
    PICO_STATUS status;
    unsigned long nSegments =NUM_SEGMENT;
    unsigned long nMaxSamples;
    char numerosec[100];

    
    status = ps6000MemorySegments(unit->handle, nSegments, &nMaxSamples);  
    
    status = ps6000SetNoOfCaptures (unit->handle, nSegments);
    
	buffer= (short**)malloc(nSegments * sizeof(short*));
	for (aa = 0; aa < nSegments; aa++)  {
    buffer[aa] = (short *) malloc(sampleCount*sizeof(int)); 
    }
    
    bufferb= (short**)malloc(nSegments * sizeof(short*));
	for (aa = 0; aa < nSegments; aa++)  {
    bufferb[aa] = (short *) malloc(sampleCount*sizeof(int)); 
    }
    
    for (aa1 = 0; aa1 < nSegments; aa1++)
    status = ps6000SetDataBufferBulk(unit->handle, PS6000_CHANNEL_C, bufferb[aa1],BUFFER_SIZE,aa1,PS6000_RATIO_MODE_NONE);
    status = ps6000SetDataBufferBulk(unit->handle, PS6000_CHANNEL_A, buffer[aa1],BUFFER_SIZE,aa1,PS6000_RATIO_MODE_NONE);
    
  
	/*  find the maximum number of samples, the time interval (in timeUnits),
	*		 the most suitable time units, and the maximum oversample at the current timebase*/
	while (ps6000GetTimebase2(unit->handle, timebase, sampleCount, &timeInterval, oversample, &maxSamples, 0))
	{
		timebase++;
	}
	printf("timebase: %ld\toversample:%hd\ttimeInterval: %f\tmaxsamples: %ld\n", timebase, oversample,timeInterval,maxSamples);
    

    
    
	/* Start it collecting, then wait for completion*/
	g_ready = FALSE;
	clk1=clock();
	status = ps6000RunBlock(unit->handle, 0, sampleCount, timebase, oversample,&timeIndisposed, 0, CallBackBlock, NULL);

	while (!g_ready && !_kbhit())
	{
		Sleep(0);
	}
	clk2=clock();
	status = ps6000Stop(unit->handle);

	//printf("BlockDataHandler:ps6000Stop ------ %i \n", status);
	
	tiempo=clk2-clk1;  
    printf("Tiempo: %fs\n",((float)tiempo/(float)CLOCKS_PER_SEC)); 

	if(g_ready) 
	{   
        
		status = ps6000GetValuesBulk(unit->handle,(unsigned long*) &sampleCount,0 , NUM_SEGMENT-1, 1, PS6000_RATIO_MODE_NONE, NULL);
		printf("BlockDataHandler:ps6000GetValues ------ %i \n", status);
        printf("sampleCount: %ld\tBUFFER_SIZE:%ld\n", sampleCount, BUFFER_SIZE);
		sampleCount = min(sampleCount, BUFFER_SIZE);
        
        printf("Guardando datos en fichero\n");
        numero=(char*)malloc((longitud_i+1)*sizeof(char));
        sprintf(numero,"%d",cont_fich);
        
        
        for (aa2 = 0; aa2 < nSegments; aa2++){
        nombre_fichero=(char*)malloc((longitud_i+strlen(directorio)+100)* sizeof(char));
        memcpy(nombre_fichero,directorio,strlen(directorio));
        memcpy(&nombre_fichero[strlen(directorio)],numero,longitud_i);
        itoa(aa2,numerosec,10);
        memcpy(&nombre_fichero[strlen(directorio)+longitud_i],"a",1);  
        memcpy(&nombre_fichero[strlen(directorio)+longitud_i+1],numerosec,strlen(numerosec));   
        memcpy(&nombre_fichero[strlen(directorio)+longitud_i+strlen(numerosec)+1],"a.dat\0",6);
        fp=fopen(nombre_fichero, "wb");
        
        nombre_ficherob=(char*)malloc((longitud_i+strlen(directorio)+100)* sizeof(char));
        memcpy(nombre_ficherob,directorio,strlen(directorio));
        memcpy(&nombre_ficherob[strlen(directorio)],numero,longitud_i);
        itoa(aa2,numerosec,10);
        memcpy(&nombre_ficherob[strlen(directorio)+longitud_i],"b",1);  
        memcpy(&nombre_ficherob[strlen(directorio)+longitud_i+1],numerosec,strlen(numerosec));   
        memcpy(&nombre_ficherob[strlen(directorio)+longitud_i+strlen(numerosec)+1],"b.dat\0",6);
        fpb=fopen(nombre_ficherob, "wb");
        //printf("final de guardar\n");
		if (fp != NULL)
		{
			fwrite(buffer[aa2],2,sampleCount,fp);
            fwrite(bufferb[aa2],2,sampleCount,fpb);
		}
		else
			printf(	"Cannot open the file for writing.\nPlease ensure that you have permission to access.\n");
		fclose(fp);
		fclose(fpb);
		free(nombre_fichero);
		free(nombre_ficherob);
    }

	} 
	else 
	{
		printf("data collection aborted\n");
		_getch();
	}

	
	
	for (aa = 0; aa < nSegments; aa++) {
        free(buffer[aa]);
        free(bufferb[aa]);
}
    free(buffer);
	free(bufferb);
    
	free(numero);
}

Martyn
Site Admin
Site Admin
Posts: 3372
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Rapid Block mode with two channels

Post by Martyn » Mon Jul 22, 2013 12:39 pm

The following is the code from our console example which should read the data from the active channels

Code: Select all

/****************************************************************************
* CollectRapidBlock
*  this function demonstrates how to collect a set of captures using 
*  rapid block mode.
****************************************************************************/
void CollectRapidBlock(UNIT * unit)
{
	unsigned short nCaptures;
	unsigned long nMaxSamples, nSamples = 1000;
	long timeIndisposed;
	short capture, channel;
	short ***rapidBuffers;
	short *overflow;
	PICO_STATUS status;
	short i;
	unsigned long nCompletedCaptures;

	long timeInterval;
	unsigned long maxSamples;

	short	triggerVoltage = mv_to_adc(100, unit->channelSettings[PS6000_CHANNEL_A].range);

	struct tPS6000TriggerChannelProperties sourceDetails = {	triggerVoltage,
																														256 * 10,
																														triggerVoltage,
																														256 * 10,
																														PS6000_CHANNEL_A,
																														PS6000_LEVEL};

	struct tPS6000TriggerConditions conditions = {	PS6000_CONDITION_TRUE,
																									PS6000_CONDITION_DONT_CARE,
																									PS6000_CONDITION_DONT_CARE,
																									PS6000_CONDITION_DONT_CARE,
																									PS6000_CONDITION_DONT_CARE,
																									PS6000_CONDITION_DONT_CARE,
																									PS6000_CONDITION_DONT_CARE};

	struct tPwq pulseWidth;

	struct tTriggerDirections directions = {	PS6000_RISING,
																						PS6000_NONE,
																						PS6000_NONE,
																						PS6000_NONE,
																						PS6000_NONE,
																						PS6000_NONE };

	memset(&pulseWidth, 0, sizeof(struct tPwq));

	printf("Collect rapid block triggered...\n");
	printf("Collects when value rises past %dmV\n",	
		adc_to_mv(sourceDetails.thresholdUpper, unit->channelSettings[PS6000_CHANNEL_A].range));
	printf("Press any key to abort\n");

	SetDefaults(unit);

	// Trigger enabled
	SetTrigger(unit->handle, &sourceDetails, 1, &conditions, 1, &directions, &pulseWidth, 0, 0, 0);
	
	//Set the number of captures
	nCaptures = 10;

	//Segment the memory
	status = ps6000MemorySegments(unit->handle, nCaptures, &nMaxSamples);

	//Set the number of captures
	status = ps6000SetNoOfCaptures(unit->handle, nCaptures);

	//Run
	
	while (ps6000GetTimebase(unit->handle, timebase, nSamples, &timeInterval, oversample, &maxSamples, 0))
	{
		timebase++;
	}

	printf("Timebase: %d", timebase);

	status = ps6000RunBlock(unit->handle, 0, nSamples, timebase, 1, &timeIndisposed, 0, CallBackBlock, NULL);

	//Wait until data ready
	g_ready = 0;
	while(!g_ready && !_kbhit())
	{
		Sleep(0);
	}

	if(!g_ready)
	{
		_getch();
		status = ps6000Stop(unit->handle);
		status = ps6000GetNoOfCaptures(unit->handle, &nCompletedCaptures);
		printf("Rapid capture aborted. %d complete blocks were captured\n", nCompletedCaptures);
		printf("\nPress any key...\n\n");
		_getch();

		if(nCompletedCaptures == 0)
			return;
		
		//Only display the blocks that were captured
		nCaptures = (unsigned short)nCompletedCaptures;
	}

	//Allocate memory
	rapidBuffers = calloc(unit->channelCount, sizeof(short*));
	overflow = calloc(unit->channelCount * nCaptures, sizeof(short));

	for (channel = 0; channel < unit->channelCount; channel++) 
	{
		rapidBuffers[channel] = calloc(nCaptures, sizeof(short*));
	}

	for (channel = 0; channel < unit->channelCount; channel++) 
	{	
		if(unit->channelSettings[channel].enabled)
		{
			for (capture = 0; capture < nCaptures; capture++) 
			{
				rapidBuffers[channel][capture] = calloc(nSamples, sizeof(short));
			}
		}
	}

	for (channel = 0; channel < unit->channelCount; channel++) 
	{
		if(unit->channelSettings[channel].enabled)
		{
			for (capture = 0; capture < nCaptures; capture++) 
			{
				status = ps6000SetDataBufferBulk(unit->handle, channel, rapidBuffers[channel][capture], nSamples, capture, PS6000_RATIO_MODE_NONE);
			}
		}
	}

	//Get data
	status = ps6000GetValuesBulk(unit->handle, &nSamples, 0, nCaptures - 1, 1, PS6000_RATIO_MODE_NONE, overflow);

	//Stop
	status = ps6000Stop(unit->handle);

	//print first 10 samples from each capture
	for (capture = 0; capture < nCaptures; capture++)
	{
		printf("Capture %d\n", capture + 1);
		printf("Channel A\tChannel B\tChannel C\tChannel D\n");

		for(i = 0; i < 10; i++)
		{
			printf("%d\t\t%d\t\t%d\t\t%d\n", rapidBuffers[0][capture][i], rapidBuffers[1][capture][i], rapidBuffers[2][capture][i], rapidBuffers[3][capture][i]);
		}
		printf("\n");
	}

	//Free memory
	free(overflow);

	for (channel = 0; channel < unit->channelCount; channel++) 
	{	
		if(unit->channelSettings[channel].enabled)
		{
			for (capture = 0; capture < nCaptures; capture++) 
			{
				free(rapidBuffers[channel][capture]);
			}
		}
	}

	for (channel = 0; channel < unit->channelCount; channel++) 
	{
		free(rapidBuffers[channel]);
	}
	free(rapidBuffers);
}
Martyn
Technical Support Manager

Post Reply