LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

create a function in CVI

Solved!
Go to solution

i can't have the input parameter RES after i build the DLL . 

/********   INIT CAN Callback    **************/
char* init_can (int Port_CAN,char* RES)

 
{
unsigned   char target[20]={0};
   char msg[512] ;
 int state=0;

 
	 strcpy(DEVICE,"COM6");		
		   
	//TD=OpenComConfig(Port_CAN,DEVICE,57600, 2, 8, 1, 0, 0);   //COM pour la passerelle CAN 
	TD=OpenComConfig(Port_CAN,DEVICE,9600, 2, 8, 1, 0, 0);  
		if(TD != 0)
		{
		    printf ("port not open\n" );

		  goto END;
		}																		   
  	
      	FlushInQ(Port_CAN);	 
  FlushOutQ(Port_CAN);  
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x06);
		ComWrtByte(Port_CAN,0x21);
		ComWrtByte(Port_CAN,0xC3);
		ComWrtByte(Port_CAN,0x3A);
		ComWrtByte(Port_CAN,0xFA);
		ComWrtByte(Port_CAN,0xF9);
	
	    ComRd(Port_CAN,Reponse,20);
		memcpy(target,Reponse,5);
        strcpy (msg, "String =");
		for (i = 0; i <5;i++)
		{ sprintf (msg, "%s 0x%X", msg, target[i]);
			 }
strcat (msg, "\n");
  	
		
	printf(msg);
		
		 RES=msg ;
	   
		if(Reponse[3]==0x41)
		{
            printf (" INIT VITESSE CAN SUCCESS\n "); 
		 state++;
  		 Delay(1);					   
		}
		  
		else
		{
	 
		  printf ( " ERROR CAN INIT VITESSE\n ");
		  
		  CloseCom(Port_CAN);
		  goto END;
		}  
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x05);
		ComWrtByte(Port_CAN,0x20);
		ComWrtByte(Port_CAN,0xFE);
		ComWrtByte(Port_CAN,0x01);
		ComWrtByte(Port_CAN,0x07);
		ComRd(Port_CAN,Reponse,5); 
		
		if(Reponse[3]==0x46)
		{
		  printf (" SYNCHRO RS232 SUCCESS\n "); 
		 state++;
		 Delay(1);
		}
		else
		{
		  printf ( " ERROR CAN SYNCHRO RS232\n ");
		  CloseCom(Port_CAN);
		  goto END;
		} 
		
		CloseCom(Port_CAN); 
		OpenComConfig(Port_CAN, DEVICE, 57600, 2, 8, 1, 0, 0);  // COM pour la passerelle CAN   115200
		FlushInQ(Port_CAN);
       //FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x2B);
		ComWrtByte(Port_CAN,0xF5);
		ComRd(Port_CAN,Reponse,5);
		
		if(Reponse[3]==0x46)
		{
		  printf (" TEST RS232 SUCCESS\n "); 
		 state++;
		 Delay(1);
		}
		else
		{
	 printf ( " ERROR CAN TEST RS232\n ");
		  CloseCom(Port_CAN);
		  goto END;
		}
		// INIT REGISTER
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x05);
		ComWrtByte(Port_CAN,0x23);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFB);
		 //   RESET Can Reception Buffer
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x2D);
		ComWrtByte(Port_CAN,0xF3);
		  //Time maxi beetween 2 char is 200 ms ( on RS232 link)
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x04);
		ComWrtByte(Port_CAN,0x2E);
		ComWrtByte(Port_CAN,0x14);
		ComWrtByte(Port_CAN,0xE3);
		 // Can Message not acknowledge request
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x29);
		ComWrtByte(Port_CAN,0xF7);
		
		if(state==3)
        {
      printf (" CAN INIT SUCCESS...PLEASE PRESS BSI ON\n");
        }
        else
        {
      printf ( "ERROR CAN INIT\n");
			   
        } 
		

	
END:   
   
   return RES;
} 

0 Kudos
Message 11 of 32
(1,824 Views)

I suppose this is because you are assigning to RES the address of the variable msg which is local to the function and at its end it is no more valid. Try with memcpy instead.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 12 of 32
(1,822 Views)

i try it but still doesn't work any other suggestions please 

0 Kudos
Message 13 of 32
(1,822 Views)
Solution
Accepted by ahmedkochbati92

This structure works in a compiled executable:

char * myFunction (char * msg)
{
  char  buf[32];

  // Manipulate the string
  strcpy (buf, "Hello world!");

  // Save the string
  strcpy (msg, buf);

  // Return the string
  return msg;
}

I never tried to do the same in a DLL and I cannot test it now, but I suppose should work as well.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 14 of 32
(1,824 Views)

still have the same problem i can't have the Input /Output paremeter  RES after i build the DLL als here the buffer type is unsigned char not const char so alawys i have an error with strcpy .

i need this function to return the paremeter msg 

char*  init_can (int Port_CAN, char* RES )



{

 unsigned   char target[20]={0}; 
 
   char msg[512] ;
 int state=0;

 
	 strcpy(DEVICE,"COM6");		
		   
	//TD=OpenComConfig(Port_CAN,DEVICE,57600, 2, 8, 1, 0, 0);   //COM pour la passerelle CAN 
	TD=OpenComConfig(Port_CAN,DEVICE,9600, 2, 8, 1, 0, 0);  
		if(TD != 0)
		{
		    printf ("port not open\n" );

		  goto END;
		}																		   
  	
      	FlushInQ(Port_CAN);	 
  FlushOutQ(Port_CAN);  
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x06);
		ComWrtByte(Port_CAN,0x21);
		ComWrtByte(Port_CAN,0xC3);
		ComWrtByte(Port_CAN,0x3A);
		ComWrtByte(Port_CAN,0xFA);
		ComWrtByte(Port_CAN,0xF9);
	
	    ComRd(Port_CAN,Reponse,20);
		memcpy(target,Reponse,5);
        strcpy (msg, "String =");
		for (i = 0; i <5;i++)
		{ sprintf (msg, "%s 0x%X", msg, target[i]);
			 }
strcat (msg, "\n");
 	
		
	printf(msg);
		
		  strcpy (RES,msg);
		  
	
	
	   
		if(Reponse[3]==0x41)
		{
            printf (" INIT VITESSE CAN SUCCESS\n "); 
		 state++;
  		 Delay(1);					   
		}
		  
		else
		{
	 
		  printf ( " ERROR CAN INIT VITESSE\n ");
		  
		  CloseCom(Port_CAN);
		  goto END;
		}  
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x05);
		ComWrtByte(Port_CAN,0x20);
		ComWrtByte(Port_CAN,0xFE);
		ComWrtByte(Port_CAN,0x01);
		ComWrtByte(Port_CAN,0x07);
		ComRd(Port_CAN,Reponse,5); 
		
		if(Reponse[3]==0x46)
		{
		  printf (" SYNCHRO RS232 SUCCESS\n "); 
		 state++;
		 Delay(1);
		}
		else
		{
		  printf ( " ERROR CAN SYNCHRO RS232\n ");
		  CloseCom(Port_CAN);
		  goto END;
		} 
		
		CloseCom(Port_CAN); 
		OpenComConfig(Port_CAN, DEVICE, 57600, 2, 8, 1, 0, 0);  // COM pour la passerelle CAN   115200
		FlushInQ(Port_CAN);
       //FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x2B);
		ComWrtByte(Port_CAN,0xF5);
		ComRd(Port_CAN,Reponse,5);
		
		if(Reponse[3]==0x46)
		{
		  printf (" TEST RS232 SUCCESS\n "); 
		 state++;
		 Delay(1);
		}
		else
		{
	 printf ( " ERROR CAN TEST RS232\n ");
		  CloseCom(Port_CAN);
		  goto END;
		}
		// INIT REGISTER
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x05);
		ComWrtByte(Port_CAN,0x23);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFB);
		 //   RESET Can Reception Buffer
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x2D);
		ComWrtByte(Port_CAN,0xF3);
		  //Time maxi beetween 2 char is 200 ms ( on RS232 link)
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x04);
		ComWrtByte(Port_CAN,0x2E);
		ComWrtByte(Port_CAN,0x14);
		ComWrtByte(Port_CAN,0xE3);
		 // Can Message not acknowledge request
		FlushInQ(Port_CAN);
		FlushOutQ(Port_CAN);
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);	
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x29);
		ComWrtByte(Port_CAN,0xF7);
		
		if(state==3)
        {
      printf (" CAN INIT SUCCESS...PLEASE PRESS BSI ON\n");
        }
        else
        {
      printf ( "ERROR CAN INIT\n");
			   
        } 
		

	
END:   
   
   return RES ;
} 

 

i try to call this DLL from Teststand and Labview and still the same problem

Spoiler
Capture.PNG

 

0 Kudos
Message 15 of 32
(1,808 Views)

Well, since your problem lies in the interaction between CVI and TestStand, it seems to me that the best place where to ask is the TestStand board. There are several discussions there on this subject, look for example at this aswer from Josh and the KnowledgeBase article he links. Hope this makes sense for you, it doesn't for me as I'm not using TestStand.

 

On the CVI side, I don't understand why you don'use ComWrt and use a series of ComWrtByte instead. And finally, I suggest you to pass -1 to OutputQueueSize in OpenComConfig in order to exclude the use of the output buffer in serial communication and have a more responsive system.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 16 of 32
(1,806 Views)

thank you for your response 

i tried to import the DLL in Labview and it work 

in the CVI side i change only the return type of the funcyion in char* 

 

 

Capture1.PNG

 

0 Kudos
Message 17 of 32
(1,807 Views)

Hi Mr Roberto 

i have problem with this function i can't read the true value of the received frame 

this is the function :

PSA MODE
RAZ Buffer Reception Can
CAN :->88 55 03 2D F3
CAN : Envoi DIAG ON CAN Frame 2.A
CAN :->88 55 0E 22 07 65 08 02 10 03 FF FF FF FF FF 75
CAN : Demande Nb Message Can
CAN :->88 55 03 2C F2
CAN6 :<-88 55 05 47 00 00 9F :OK
CAN : Demande Nb Message Can
CAN :->88 55 03 2C F2
CAN6 :<-88 55 05 47 00 01 9E :OK
CAN : Demande Message Can
CAN :->88 55 03 24 FA
CAN6 :<-88 55 09 40 06 65 03 02 50 03 A5 :OK
CAN -> N° message Can1
CAN :<-025003A5 : OK   (  i want the fuction to return this value )

	  //PSA_MODE 

	char*  PSA_MODE  (int Port_CAN )  
		{  	
unsigned   char target[20]={0};
		
	
 
 char msg[512];

		
		//1
	 	FlushInQ(Port_CAN);	 
       //FlushOutQ(Port_CAN);   	  
		ComWrtByte(Port_CAN,0x88);		   
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x2D);
		ComWrtByte(Port_CAN,0xF3);
		    
	
		//2
	 	FlushInQ(Port_CAN);	 
  //FlushOutQ(Port_CAN); 
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x0E);
		ComWrtByte(Port_CAN,0x22);
		ComWrtByte(Port_CAN,0x07);
		ComWrtByte(Port_CAN,0x65);
		ComWrtByte(Port_CAN,0x08);		   
		ComWrtByte(Port_CAN,0x02);
		ComWrtByte(Port_CAN,0x10);
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0x75);
		 	  
				
		//3
	 	FlushInQ(Port_CAN);	 
       //FlushOutQ(Port_CAN); 
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);			   
		ComWrtByte(Port_CAN,0x2C);			  
		ComWrtByte(Port_CAN,0xF2);	
	    ComRd(Port_CAN,Reponse,20);
	 
  
		   
  		
	if(Reponse[3]==0x47)
		{
		  printf ("PASSED_1 \n "); 
		
		 Delay(1);
		}
		else
		{
		  printf ( "NOT_PASSED_1 \n ");
		 
		} 
	
	
	
		  		//3'
	 	FlushInQ(Port_CAN);	 
       //FlushOutQ(Port_CAN); 
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);			   
		ComWrtByte(Port_CAN,0x2C);			  
		ComWrtByte(Port_CAN,0xF2);	
	    
		

	
	  ComRd(Port_CAN,Reponse,20);

     
  		
	if(Reponse[3]==0x47)
		{
		  printf ("PASSED_1' \n "); 
		
		 Delay(1);
		}
		else
		{
		  printf ( "NOT_PASSED_1' \n ");
		 
		} 
		
	   	//4
	 	FlushInQ(Port_CAN);	 
    //FlushOutQ(Port_CAN);  						   
                                                     
	    ComWrtByte(Port_CAN,0x88);			    
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);			   
		ComWrtByte(Port_CAN,0x24);				
		ComWrtByte(Port_CAN,0xFA);
		ComRd(Port_CAN,Reponse,20);
;
		memcpy(target,Reponse,10);
        strcpy (msg,"");
		for (i = 0; i <10;i++)
		{ sprintf (msg, "%s%X", msg, target[i]);
			 }
strcat (msg, "\n");
 	
		
	printf(msg);
		
		if(Reponse[3]==0x40) 
		{
		  printf ("PASSED_2 \n "); 
		
		  Delay(1);
		}
		else
		{
		  printf ( "NOT_PASSED_2 \n ");
		 ;
		} 

         
	  	return msg;
		}

 

Capture01.PNGthis the result any help please  

0 Kudos
Message 18 of 32
(1,709 Views)

'msg' variable is local to the function and is released after the function ends, which means the caller can receive an invalid string. Have you tried passing the string to manipulate to the function as I told you before?
Try doing so and see if the string is correctly returned calling it from CVI: if it works, then the problem is likely to lie in how the function is defined in TestStand, which I cannot discuss.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 19 of 32
(1,703 Views)

Thank you for your response 

This is my try but still doasen't work, i can't undrestand how it works in the Init can Function and no in this one  

After i build the dll i'm calling it from labview it works properly in the init function return the msg received without adding a new paremeter like Input /Outpout  RES

 

 

 

 

	  //PSA_MODE 

	char *PSA_MODE  (int Port_CAN ,char* RES)
		{  	
unsigned   char target[20]={0};
		
	
 
 char msg[512];

		
		//1
	 	FlushInQ(Port_CAN);	 
       //FlushOutQ(Port_CAN);   	  
		ComWrtByte(Port_CAN,0x88);		   
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0x2D);
		ComWrtByte(Port_CAN,0xF3);
		    
	
		//2
	 	FlushInQ(Port_CAN);	 
  //FlushOutQ(Port_CAN); 
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x0E);
		ComWrtByte(Port_CAN,0x22);
		ComWrtByte(Port_CAN,0x07);
		ComWrtByte(Port_CAN,0x65);
		ComWrtByte(Port_CAN,0x08);		   
		ComWrtByte(Port_CAN,0x02);
		ComWrtByte(Port_CAN,0x10);
		ComWrtByte(Port_CAN,0x03);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0xFF);
		ComWrtByte(Port_CAN,0x75);
		 	  
				
		//3
	 	FlushInQ(Port_CAN);	 
       //FlushOutQ(Port_CAN); 
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);			   
		ComWrtByte(Port_CAN,0x2C);			  
		ComWrtByte(Port_CAN,0xF2);	
	    ComRd(Port_CAN,Reponse,20);
	 
  
		   
  		
	if(Reponse[3]==0x47)
		{
		  printf ("PASSED_1 \n "); 
		
		 Delay(1);
		}
		else
		{
		  printf ( "NOT_PASSED_1 \n ");
		 
		} 
	
	
	
		  		//3'
	 	FlushInQ(Port_CAN);	 
       //FlushOutQ(Port_CAN); 
		ComWrtByte(Port_CAN,0x88);
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);			   
		ComWrtByte(Port_CAN,0x2C);			  
		ComWrtByte(Port_CAN,0xF2);	
	    
		

	
	  ComRd(Port_CAN,Reponse,20);

     
  		
	if(Reponse[3]==0x47)
		{
		  printf ("PASSED_1' \n "); 
		
		 Delay(1);
		}
		else
		{
		  printf ( "NOT_PASSED_1' \n ");
		 
		} 
		
	   	//4
	 	FlushInQ(Port_CAN);	 
    //FlushOutQ(Port_CAN);  						   
                                                     
	    ComWrtByte(Port_CAN,0x88);			    
		ComWrtByte(Port_CAN,0x55);
		ComWrtByte(Port_CAN,0x03);			   
		ComWrtByte(Port_CAN,0x24);				
		ComWrtByte(Port_CAN,0xFA);
		ComRd(Port_CAN,Reponse,20);
;
		memcpy(target,Reponse,10);
        strcpy (msg,"");
		for (i = 0; i <10;i++)
		{ sprintf (msg, "%s%X", msg, target[i]);
			 }
	
strcat (msg, "\n");
 	
	strcpy(RES ,msg);
	printf(msg);
		
		if(Reponse[3]==0x40) 
		{
		  printf ("PASSED_2 \n "); 
		
		  Delay(1);
		}
		else
		{
		  printf ( "NOT_PASSED_2 \n ");
		 ;
		} 

         
	  	return RES ;
		}

 

 

0 Kudos
Message 20 of 32
(1,697 Views)