AnsweredAssumed Answered

Sporadic -420 Query unterminated error in agilent 34410A using visa in c++

Question asked by white-star on Nov 5, 2015
Latest reply on Nov 6, 2015 by white-star
Hi 

I wrote a function in visual studio 2010 in c++, which calculates the average current consumption from defined number os samples with defined sample interval. Problem is that i sporadically get -420 query unterminated error and communication hangs. Need to reconnect usb cable to resume communication. Problem especially occurs after first switch on of device. I thought it can be a timing error, so I add some *WAI and *OPC? commands but with no effect. I add my functions below (and attach cpp file). Any help would be greatly appreciated.

float MeasureAverageCurrentDC_mA(int sample_number, float sample_interval)
{
     float result;
     
     if(sample_interval < 0.05)
          sample_interval = 0.05;
     else if(sample_interval > 600)
          sample_interval = 600;

     if(sample_number < 1)
          sample_number = 1;
     else if(sample_number > 50000)
          sample_number = 50000;

     char samp_num[10];
     char samp_int[10];
     char table[90];
     char var1[45] = "CONF:CURR:DC 0.1, DEF;:SAMP:COUN ";
     char var2[45] = ":SAMP:SOUR TIM;:SAMP:TIM ";

     _itoa(sample_number,samp_num,10);

     for(int i = 0; i < sizeof(samp_num); i++)
     {
          if(samp_num[i] == '\0')
          {
               samp_num[i+1] = '\0';
               samp_num[i] = ';';
               break;
          }
     }

     int k = 0;
     for(int i = 0; i < sizeof(var1); i++)
     {
          if(var1[i] == '\0')
          {
               for(int j = i; j < (i + sizeof(samp_num)); j++)
               {
                    var1[j] = samp_num[k]; 
                    k++;
               }
               break;
          }
     }


     sprintf(samp_int, "%.2f;", sample_interval);
     k = 0;
     for(int i = 0; i < sizeof(var2); i++)
     {
          if(var2[i] == '\0')
          {
               for(int j = i; j < (i + sizeof(samp_int)); j++)
               {
                    var2[j] = samp_int[k]; 
                    k++;
               }
               break;
          }
     }

     for(int i = 0; i < sizeof(var1); i++)
     {
          table[i] = var1[i];
     }

     k = 0;
     for(int i = 0; i < sizeof(var1); i++)
     {
          if(var1[i] == '\0')
          {
               for(int j = i; j < (i + sizeof(var2)); j++)
               {
                    table[j] = var2[k]; 
                    k++;
               }
               break;
          }
     }

     for(int i = 0; i < sizeof(table); i++)
     {
          if(table[i] == '\0')
          {
               for(int j = i; j < sizeof(table); j++)
               {
                    table[j] = '\0'; 
               }
               break;
          }
     }


     int retval_size = 800000; // Set this variable to the maximum size of the array.
     char *retval = new char[retval_size];
     long opc = 0;

     ViSession defaultRM, vi; 
     ViStatus viStatus = 0; 
     viStatus=viOpenDefaultRM(&defaultRM);
     viStatus=viOpen(defaultRM, DEVICE_ADDRESS, VI_NULL, VI_NULL, &vi);
     if(viStatus)
     {
          return 666;
     }
     
     viSetAttribute (vi, VI_ATTR_WR_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
     viSetAttribute (vi, VI_ATTR_RD_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
     viSetAttribute(vi, VI_ATTR_TMO_VALUE, -1);
     viPrintf(vi, "*CLS");
     viPrintf(vi, "*WAI\n");
     viPrintf(vi, table);
     viPrintf(vi, "*WAI\n");
     viQueryf(vi, "*OPC?\n", "%d", &opc);
     viQueryf(vi, "READ?\n", "%#T", &retval_size, retval);
     viQueryf(vi, "*OPC?\n", "%d", &opc);

     cout << retval << endl;

     result = (float)calculate_average_result(retval)*1000;

     viGpibControlREN(vi,  VI_GPIB_REN_ADDRESS_GTL);       // go to local
     viClose(vi);
     viClose(defaultRM);
     delete[] retval;

     return result;
}

double calculate_average_result(char retval[])
{
     int arraysize = 15;
     int j = 0;
     int licznik_znakow = 0;
     int licznik_kropka = 0;
     int czy_kropka = 0;
     int czyE = 0;
     int EE = 0;
     long long lolek = 0;
     double result;
     int retval_size = 500;
     int result_counter = 0;
     double sum = 0;
     
     for(int i = 0; i < retval_size; i++)
     {
          if(retval[i] == ',')
               result_counter++;
     }
     result_counter++;

     char **tab = new char *[result_counter];
     for ( int i = 0; i < result_counter; ++i ) 
     { 
          tab[i] = new char[16];
          for ( int j = 0; j < 16; ++j) 
               tab[i][j]= '\0'; 
     } 

     int k = 0;
     for(int i = 0; i < result_counter; i++)
     {
          for(int j = 0; j < 16; j++)
          {
               tab[i][j] = retval[k];
               k++;
               if(retval[k] == ',')
               {
                    k++;
                    break;
               }
          }

     }

     double *res_table = new double[result_counter]();
     
     //////////////////////////////
     for(int count = 0; count < result_counter; count++)
     {
          j = 0;
          licznik_znakow = 0;
          licznik_kropka = 0;
          czy_kropka = 0;
          czyE = 0;
          EE = 0;
          lolek = 0;
          arraysize = 15;
          
          int *myvector = new int[arraysize]();

          for(int i = 0; i < arraysize; i++)
          {
               if(tab[count][i] == 69)
               {
                    czyE = 1;
                    break;
               }

               if(tab[count][i] == 46)
               {
                    czy_kropka = 1;
                    licznik_kropka = 0;
               }

               if(tab[count][i] > 47 && tab[count][i] < 58)
               {
                    licznik_kropka = licznik_kropka + 1;
                    myvector[j] = tab[count][i] - '0';
                    j++;
               }
               else
               {
                    licznik_znakow = licznik_znakow + 1;
               }
          }

          if(czyE == 1)
          {
               for(int i = j + 2 + licznik_znakow; i < arraysize; i++)
               {
                    EE = EE + (tab[count][i] - '0');
               }
          }

          arraysize = j - 1;

          for(int i = 0; i < arraysize; i++)
          {
               lolek = lolek + (myvector[i]) * (pow(10.0, (arraysize-i)));     
          }
          lolek = lolek + myvector[arraysize];

          if(czy_kropka == 1)
               result = (double)lolek / pow(10.0, licznik_kropka);
          else
               result = (double)lolek;

          if(czyE == 1)
               result = result / pow(10.0, EE);

          delete [] myvector;

          res_table[count] = result;
     }

     for(int count = 0; count < result_counter; count++)
     {
          sum = sum + res_table[count];
     }

     delete [] res_table;

     for ( int i(0); i < result_counter; ++i ) 
          delete [] tab[i]; 
     delete [] tab; 
     tab = NULL; 

     return sum/result_counter;
}

Edited by: white-star on Nov 5, 2015 1:52 AM  

Attachments

Outcomes