Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

usb event labwindow cvi problem



@jeffh12345 wrote:
If you are using viReadAsync, you will need to handle the VI_EVENT_IO_COMPLETION event, and ensure that you check the status with viGetAttribute on VI_ATTR_STATUS.

Done, but was not checking this: This status is updated when the handler completes and closes the event, correct?



Correct.  Yeah, if you don't check the status you won't know if a timeout occured, if there was an error, if you received more than you asked for etc...

Shawn Bohrer
National Instruments
Use NI products on Linux? Come join the NI Linux Users Community
0 Kudos
Message 11 of 16
(2,534 Views)

Something to add for clarification:

 

The messages i get are async: they can happen at any time, any number of them from the network

I need to capture and process the messages: some belong to me, some don't

I have no idea what is waiting for me in the buffer, how many, what size

All I know is that a fragment can be 8-64 bytes long: multiple fragments can happen right after each other, or spaced over several seconds

I have a limited time to reply to some messages before they get resent

 

using USB like a serial port is a good description of what I am doing

 

Hope this helps:

Jeff

 

 

 

0 Kudos
Message 12 of 16
(2,534 Views)
I guess that description is a little confusing to me.  I guess the part that is confusing is USB really isn't like serial, meaning that a USB device can't send you data unless you ask for it.

Actually I guess after rereading your description I might understand.  I guess you are saying that when you perform a read you have no way of know how much data you will be getting, and in that data how you will need to parse it correct? 

Parsing the data is your problem I suppose.  As for requesting data from you USB device I would think you can just use viReadAsyc with a large timeout.  You can request less than 64 bytes if you find that you aren't receiving the data fast enough to respond, but you will probably never know how much data you have sitting in the buffer.  If there is data already in the buffer though the next read will simply read from the buffer instead of querying the device.

Hope that helps,
Shawn Bohrer
National Instruments
Use NI products on Linux? Come join the NI Linux Users Community
0 Kudos
Message 13 of 16
(2,531 Views)

 

BULK IN does not seem to queue properly.

There must be an extremely small queue, as I still drop data, unless I grab

256 bytes at a time, even though my packet size is 64.

There may be a bug there.

 

I tried to impliment the following:

For each packet received:

Read 1 byte for beginning of packet byte

Read 7 bytes more for header

Read x bytes for rest of packet based upon size of packet inside header

Where x would be between 0 and 56 bytes

Very unstable, I was loosing packets, only got pieces

 

This works great using RS232

 

How big is the queue?

Any way to see how many bytes are there, like the RS232 port?

 

Thanks,

 

Jeff

0 Kudos
Message 14 of 16
(2,518 Views)
Hey Jeff,

The buffer is 8 KB on Windows XP and 2000.  On Windows Vista it is handled by the OS so I'm not sure how big it is.

Unfortunately at this time there is no way to know how many bytes remain in the buffer, like there is with a serial port.

Shawn Bohrer
National Instruments
Use NI products on Linux? Come join the NI Linux Users Community
0 Kudos
Message 15 of 16
(2,474 Views)

hi,

 

         i am vinoth. I have written the code for USB communication. I have used a cypress IC, whose part no is cy7c68013 (56-pin), which is an high speed USB controller. I have attached the coding which I have written in CVI.

 

#include <utility.h>
#include <formatio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "visa.h"

static ViSession defaultRM;
static ViSession instr;
static ViStatus status; 
static ViUInt32 retCount;
static ViUInt32 writeCount;
static unsigned char buffer[100];
static ViBuf stringinput[5];
char test[9000];
static unsigned char test_gdd[20];
static unsigned char test_gdd_out[20];
char test2[64];
char temp[50];
static unsigned char test1[2];
static ViPUInt16 retur;
static ViPUInt16 readcount;
int fp,fp1, fp2;		
int i;
char testint1[1024], testint2[1024], testint3[1024], testint4[1024], testint5[1024], testint6[1024], testint7[1024], testint8[1024];
char testintout1[1024];
static ViPUInt16 readcount1;
static ViPUInt16 readcountbuf1;
char bulkloopbuffer[8200];
char bulkloopbuf1[1024], bulkloopbuf2[1024], bulkloopbuf3[1024], bulkloopbuf4[1024], bulkloopbuf5[1024], bulkloopbuf6[1024], bulkloopbuf7[1024], bulkloopbuf8[1024];


int main(void)
{
   status=viOpenDefaultRM (&defaultRM);
   if (status < VI_SUCCESS)
   {
      printf("Could not open a session to the VISA Resource Manager!\n");
      exit (EXIT_FAILURE);
   } 
                                                                         
   status = viOpen (defaultRM,"cypress_EZusb", VI_LOAD_CONFIG, VI_NULL, &instr);
   if (status < VI_SUCCESS)  
   {
        printf ("Cannot open a session to the device.\n");
        goto Close;
   }
  
   status = viSetAttribute (instr, VI_ATTR_TMO_VALUE, 5000);
  
   status = viUsbControlIn (instr, 128, 06, 256, 0, 18, test_gdd, retur); // for Get Descriptor-Device
	 
   Delay (0.001); // delay for 1ms
	 
   test1[0] = 1;	  // for reset condition
   status = viUsbControlOut (instr, 64, 160, 58880, 0, 1, test1); // reset condition out
   printf("\nStatus_reset_1 = %d",status); 
   Delay (.001);	// delay for 1ms
   fp2 = OpenFile ("c:\\cypress\\USB\\Examples\\FX2LP\\Bulkloop\\bulkloop.hex", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
   ReadFile (fp2, bulkloopbuffer, 7200);	   // to be read only 7KB(in hex, 7168) of data
   CloseFile (fp2);
	
   //------------------ downloading bulkloop.hex for renumeration ---------------------------------------------
	
	for (i = 0; i <= 1023; i++)
	{
		bulkloopbuf1[i] = bulkloopbuffer[i];
	}

	status = viUsbControlOut (instr, 64, 160, 0, 0, 1024, bulkloopbuf1);   // 1st 1024 bits download
	
	Delay (.001); // delay for 1ms
		
	status = viUsbControlIn (instr, 192, 160, 0, 0, 1024, bulkloopbuf1, readcountbuf1);  // 1st 1024 bits upload
	
	for (i = 0; i <= 1023; i++)
	{
		bulkloopbuf2[i] = bulkloopbuffer[i + 1024];
	}
	
	status = viUsbControlOut (instr, 64, 160, 1024, 0, 1024, bulkloopbuf2); 
	
	for (i = 0; i <= 1023; i++)
	{
		bulkloopbuf3[i] = bulkloopbuffer[i + 2048];
	}
	
	status = viUsbControlOut (instr, 64, 160, 2048, 0, 1024, bulkloopbuf3);  
	
	for (i = 0; i <= 1023; i++)
	{
		bulkloopbuf4[i] = bulkloopbuffer[i + 3072];	
	}
	
	status = viUsbControlOut (instr, 64, 160, 3072, 0, 1024, bulkloopbuf4); 
	
	for (i = 0; i <= 1023; i++)
	{
		bulkloopbuf5[i] = bulkloopbuffer[i + 4096];
	}
	
	status = viUsbControlOut (instr, 64, 160, 4096, 0, 1024, bulkloopbuf5);
	
	for (i = 0; i <= 1023; i++)
	{
		bulkloopbuf6[i] = bulkloopbuffer[i + 5120]; 
	}	
	
	status = viUsbControlOut (instr, 64, 160, 5120, 0, 1024, bulkloopbuf6);
	
	for (i = 0; i <= 1023; i++)
	{
		bulkloopbuf7[i] = bulkloopbuffer[i + 6144]; 
	}
	
	status = viUsbControlOut (instr, 64, 160, 6144, 0, 1024, bulkloopbuf7);
	
	//----------------------- end of downloading bulkloop.bex ------------------------------------------------
	
	
	
	test1[0] = 0;     // for reset condition 
	status = viUsbControlOut (instr, 64, 160, 58880, 0, 1, test1);    // reset condition out
	
	//---------------------------------------------------------------------------------------------------------  
	
	Delay (0.001); // delay for 1ms
	
	//---------------------------------------------------------------------------------------------------------  
	
	test1[0] = 1;	  // for reset condition
	status = viUsbControlOut (instr, 64, 160, 58880, 0, 1, test1); // reset condition out
	//---------------------------------------------------------------------------------------------------------
	
	Delay (.001);	// delay for 1ms
	
	//---------------------------------------------------------------------------------------------------------

	for (i = 0; i <= 4; i++)
	{
		stringinput[i] = "a";
	}  
	
	status = viWrite (instr, (ViBuf)stringinput, 1, &writeCount);  
	
	if (status < VI_SUCCESS)  
        {
           printf("Error writing to the device\n");
           goto Close;
        }
	
        Delay(0.001);     // delay for 1ms
	
	
	test1[0] = 0;  
	status = viUsbControlOut (instr, 64, 160, 58880, 0, 1, test1);   
	printf("\nStatus_reset_0 = %d",status); 
		
    Close:
   printf("\nClosing Sessions\nHit enter to continue.");
   fflush(stdin);
   getchar();  
   status = viClose(instr);
   status = viClose(defaultRM);

   return 0;
}

 

It shows an error as follows:

 NON-FATAL RUN-TIME ERROR:   "test_case.c", line 217, col 14, thread id 0x00000408:   Function viWrite: (return value == -1073807302 [0xbfff003a]). Unable to start operation because setup is invalid (due to attributes being set to an inconsistent state).

Will you please explain the error what is saying. Please send the necessary information regarding this code. I am waiting for your reply soon.

 

0 Kudos
Message 16 of 16
(2,168 Views)