Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

viScanf not succeeding

Hi All,

 

I am trying to collect data from an oscilloscope using Visa programming. I have modified the existing program examples bufferio.cpp and formatio.cpp. When I use the viScanf function, it does not succeed and reports me error. I am attaching the modified code that I use. 

 

 

double* ReadWaveform(ViSession vi, long* elements)
{
	ViStatus	status;
	float		yoffset, ymult;
	ViChar		buffer[256];
	ViChar		c;
	long		count, i;
	double*		ptr = NULL;
	unsigned char		szBuff[256] = { 0,};
	static ViUInt32 Wrcount;
	static char stringinput[512];

	assert(elements != NULL);

	status = viSetAttribute(vi,VI_ATTR_WR_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
	status = viSetAttribute(vi,VI_ATTR_RD_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
	
	 /* Set the timeout for message-based communication*/
   status = viSetAttribute(vi, VI_ATTR_TMO_VALUE, 25000);

   strcpy(stringinput,"MEASUREMENT:IMMED:SOURCE1 CH3");
   status = viWrite (vi, (ViBuf)stringinput, (ViUInt32)strlen(stringinput), &Wrcount);
   if (status < VI_SUCCESS)  
   {
      printf("Error writing to the device\n");
      goto error;
	  
   }
	// Turn headers off, this makes parsing easier
	status = viPrintf(vi, "header off\n");
	if (status < VI_SUCCESS) 
	{
      printf("Error writing Header off to the device\n");
      goto error;
	  
    }

	// Get record length value
	status = viQueryf(vi, "hor:reco?\n", "%ld", elements);
	printf("The number of elements is %ld \n",*elements);
	if (status < VI_SUCCESS)
	{
      printf("Error getting the elements value\n");              WORKS FINE
      goto error;
	  
    }

	// Make sure start, stop values for curve query match the full record length
	status = viPrintf(vi, ":data:start 1;:data:stop %d\n", *elements);
	if (status < VI_SUCCESS)
	{
      printf("Error writing start and stop values\n");
      goto error;
	  
    }

	// Get the yoffset to help calculate the vertical values.

	status = viQueryf(vi, "WFMOutpre:YOFF?\n", "%f", &yoffset);
	printf("The yoffset value is %f \n",yoffset);                WORKS FINE
	if (status < VI_SUCCESS)
	{
      printf("Error getting the yoffset value\n");
      goto error;
	  
    }
		 

	// Get the ymult to help calculate the vertical values.
	status = viQueryf(vi, "WFMOutpre:YMULT?\n", "%f", &ymult);	     WORKS FINE
	printf("The ymult value is %f \n",ymult);
	if (status < VI_SUCCESS)
	{
      printf("Error getting the ymult value\n");
      goto error;
	  
    }

	// Request 8bit binary data on the curve query
	status = viPrintf(vi, "DATA:ENCDG RIBINARY;WIDTH 1\n");
	if (status < VI_SUCCESS)
	{
      printf("Error writing the encoding width value to the device\n");
      goto error;
	  
    } 

	// Request the curve
	status = viPrintf(vi, "CURVE?\n");
	if (status < VI_SUCCESS)
	{
      printf("Error writing CURVE to the device\n");
      goto error;
	  
    }
		
    // Always flush if a viScanf follows a viPrintf or viBufWrite.
	status = viFlush(vi, VI_WRITE_BUF | VI_READ_BUF_DISCARD);
	if (status < VI_SUCCESS) 
	{
      printf("Error writing Flush to the device\n");
      goto error;	  
    }

	// Get first char and validate
	status = viSetAttribute(vi,VI_ATTR_RD_BUF_OPER_MODE, VI_FLUSH_DISABLE);

	status = viScanf(vi, "%c", &c);					ERROR HERE
	if (status < VI_SUCCESS) 
	{
      printf("Error Setting attribute and getting a char \n");       PRINTING THIS MESSAGE
      goto error;
    }
	assert(c == '#');

	// Get width of element field.
	status = viScanf(vi, "%c", &c);
	if (status < VI_SUCCESS) 
	{
      printf("Error getting width of element from the device\n");
      goto error;
	  
    }
	assert(c >= '0' && c <= '9');

	// Read element characters
	count = c - '0';
	for (i = 0; i < count; i++) 
	{
		status = viScanf(vi, "%c", &c);
		if (status < VI_SUCCESS) 
		{
		printf("Error reading element characters from the device\n");
		goto error;		
		}
		assert(c >= '0' && c <= '9');
	}

	// Read waveform into allocated storage
	ptr = (double*) malloc(*elements*sizeof(double));

	for (i = 0; i < *elements; i++) 
	{
		status = viScanf(vi, "%c", &c);
		if (status < VI_SUCCESS)
		{
		printf("Error writing to the device\n");
		goto error;
		
		} 
		
		ptr[i] = (((double) c) - yoffset) * ymult;
		fprintf(fp,"%c \t", ptr[i]);
	}

	status = viFlush(vi, VI_WRITE_BUF | VI_READ_BUF_DISCARD);
	if (status < VI_SUCCESS) 
	{
      printf("Error Flush to the device\n");
	  goto error;
	  
    }

	return ptr;

error:
	// Report error and clean up
	viStatusDesc(vi, status, buffer);
	fprintf(stderr, "failure: %s\n", buffer);
	if (ptr != NULL) free(ptr);
	return NULL;
}

What should I do to get the data using viScanf. Can somebody please help? Thanks

 

Ashwin

 

0 Kudos
Message 1 of 2
(3,862 Views)

It works now. I was not looking at the correct specifier. Also I replaced the viWrite with viPrintf for setting the oscilloscope channel as I think we should not use viWrite and viPrintf in the same session (not sure though). 

 

status = viPrintf(vi, "DATA:SOURCE CH3\n");

0 Kudos
Message 2 of 2
(3,856 Views)