05-10-2017 08:22 AM
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;
}
Solved! Go to Solution.
05-10-2017 10:23 AM
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]); }
05-10-2017 01:47 PM - edited 05-10-2017 01:47 PM
Hi Mr RobertoBozzolo
Thank you for your help .
I'm building a DLL from Lab /CVI to call it in TestStand and it works so well for some functions ,my problem is i want to print the data received for example when i send this frame CAN :->88 55 03 2C F2 i need to read and print CAN6 :<-88 55 05 47 00 00 9F : OK
in the functions. txt file there is some of the functions that i need to create my problem is how to show the data received like a message "print" and how to use it
I hope it's clear.
Best regards
Ahmed Kochbati
Electronic engineer
05-14-2017 12:14 PM
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;
05-16-2017 01:43 AM - edited 05-16-2017 02:10 AM
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.
05-16-2017 04:17 AM
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
05-16-2017 04:37 AM
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.
05-16-2017 05:42 AM
Thank you so much for your help
05-17-2017 02:58 AM
Hi Mr Roberto ,
How can i modify this function to return this received frame
thank you
05-17-2017 05:11 AM - edited 05-17-2017 05:12 AM
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.