LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

create a function in CVI

Solved!
Go to solution

Hi,

I'm new in CVI and i need to know how to print the received data "readBuf" in this function any help please

Thank you

 

 

int CVICALLBACK ReceiveData (int panel, int control, int event, void *callbackData, 

 int eventData1, int eventData2)
{
char readBuf[20] = {0};
int strLen;

switch (event)
 {
case EVENT_COMMIT:
/* Read the characters from the port */
strLen = GetInQLen (COM_PORT);
 ComRd (COM_PORT, readBuf, strLen);
SetCtrlVal (gPanelHandle, PANEL_OUTOUT_STRING, readBuf);
SetActiveCtrl (gPanelHandle, PANEL_INPUT_STRING);
break;
}
return 0;
}

0 Kudos
Message 1 of 32
(4,570 Views)

How do you want to "print" the message you receive? The way you are doing it at present only produces a meaningful output if the message includes printable characters, which is probably not your case.

You could format an output string out of the message and print that one; this code should work:

char  msg[512];

strcpy (msg, "String =");
for (i = 0; i < strLen; i++) { sprintf (msg, "%s 0x%X", msg, readBuf[i]); }


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 2 of 32
(4,548 Views)

Hi Mr 

 

 

Best regards 

Ahmed Kochbati

Electronic engineer 

Download All
0 Kudos
Message 3 of 32
(4,538 Views)

Hi ,

i try with your solution but it doesn't work this is my try and i get this message(d8ed724)TestStand when i call the DLLs/CVI from )TestStand  so i think the problem is in the format 

i need to display the message like this

CAN : Init Vitesse Can1
CAN :->88 55 06 21 C3 3A FA F9
CAN :<-88 55 03 41 9F :OK

i can verify bytes but it's too long solution  i need only to display this received frame 

 

thank you 

 #include "stdtst.h"
#include "tsutil.h"

//==============================================================================
//
// Title:       BX_1008_TEST_CASE
// Purpose:     A short description of the application.
//
// Copyright: VISTEON. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files

#include <rs232.h>
#include "toolbox.h"
#include <utility.h>
#include <formatio.h>
#include <ansi_c.h>
#include <cvirte.h>     
#include "toolbox.h"
//==============================================================================
// Constants

//==============================================================================
// Types

//==============================================================================
// Static global variables


int TD=0, CW=0, error=0, wT;
int Port_CAN=6; 
char Reponse[20], CH[50];
char DEVICE[5]="COM6";;
char ADR[4],ADR1[4]="0",CMD[40],AFF[40];
int i=0, j=0;
//==============================================================================
// 								Static functions

//==============================================================================
// 								Global variables

//***************************************************************************************/
//***************************************************************************************/
//*****************************  MAIN MENU  *********************************************/


/// HIFN The main entry-point function.
int main (int argc, char *argv[])
{
  
    return 0;
}
//*****************************  END MAIN MENU  *****************************************/
//***************************************************************************************/
//***************************************************************************************/

//==============================================================================
// UI callback function prototypes

/****************************************************************************/


/****************************************************************************/



/********   INIT CAN Callback    **************/

int init_can (int Port_CAN)
{
;	
char target[20]={0};
 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);
	   
		strncpy(target,Reponse,20);
	

printf ( "%x\n",target);
		
  	
		
		
		
		
	   
		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 0;
0 Kudos
Message 4 of 32
(4,488 Views)

You are not doing what I suggested: instead of simply calling printf ( "%x\n",target); (which actually prints the address of the variable in hex format) you must:

 

First - create a formatted message:

strcpy (msg, "String =");
for (i = 0; i < 20; i++) {
	sprintf (msg, "%s 0x%X", msg, target[i]);
}
strcat (msg, "\n");

 

Second printf that message

printf (msg);

 

Finally, you should not use strncpy or any string-oriented function in your code. String-oriented functions will terminate writing to the target string at the first NUL-byte found. That is, if your message can include NUL bytes followed by other valid bytes, you will loose all content past the first NUL byte, which in case of strncpy is replaced with NULs. This is clearly indicated in the help for the function. Use 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?
Message 5 of 32
(4,464 Views)

Hi Mr Roberto ,

thank you so much for your help
i think it work properly but i have a problem only with the first and last bytes .

int init_can (int Port_CAN)
{
	
char target[20]={0};
 int state=0;
 char msg[512];
 
	 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]);
			 Delay(1);}
strcat (msg, "\n");
  	
		
	printf(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;
		}  

CAN : Init Vitesse Can1
CAN :->88 55 06 21 C3 3A FA F9
CAN :<-88 55 03 41 9F :OK

 

Best Regards 

Ahmed Kochbati

0 Kudos
Message 6 of 32
(4,457 Views)

Your buffer should be declared as an unsigned char, which holds values 0 ÷ 255. A simple char accepts values from -128 to 127, in this case 0x88 and 0x9f are really negative values (-120 and -97 respectively), hence the strange formatting in output.

 

Beware of the Delay (1) inside the loop: is probably useless and time consuming.



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 7 of 32
(4,451 Views)
Solution
Accepted by ahmedkochbati92

Thank you so much for your help 

0 Kudos
Message 8 of 32
(4,447 Views)

Hi Mr Roberto ,

How can i modify this function to return this received frame 

thank you 

0 Kudos
Message 9 of 32
(4,429 Views)

You must add a char * parameter to the function to return the string; if you want to return the raw CAN message, it is better to add an additional int * parameter to return the number of valid bytes in the message. Next pass the desired message and optional message lenght to those parameters.



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 10 of 32
(4,425 Views)