LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

SinPattern error

Hello, i got this problem:

 

When i send informatiob to rs 232 (with loopback), i get it information back, but when i try to plot with that information i get this error

 

 

NON-FATAL RUN-TIME ERROR: "myfile.c", line 348, col 3, thread id 0x00000DC0: Function SinePattern: (return value == -20003 [0xffffb1dd]). The number of samples must be > 0.

 

 

What is the problem?

 

if i draw sinus signal with sinus formula, everything is ok, but with this funtcion it is not...

0 Kudos
Message 1 of 17
(3,316 Views)

This error is apparently quite obvious: you are passing 0 as numberOfElements parameter in SinePattern function.

But since this function is used to generate an aray of samples that contains a sinusoidal wave, and is not strictly related to plotting data, could you please explain a little bit more what you are trying to do? Some lines of code will be of help in understanding how this condition arises..



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 2 of 17
(3,312 Views)

Well i want to get information from other functions so i use this code:

 

 

send appropriate data

int CVICALLBACK SendCallBack (int panel, int control, int event,
                              void *callbackData, int eventData1,
                              int eventData2)
{
    if (event == EVENT_COMMIT)
        {
            SendGraph ();

        RS232Error = ReturnRS232Err ();
        if (RS232Error)
            DisplayRS232Error ();
       // SetCtrlAttribute (panel_handle, SERIAL_BYTES, ATTR_DIMMED, 0);
       // SetCtrlVal (panel_handle, SERIAL_BYTES, bytes_sent);
        }
    return 0;
}

 send to port

 

// sending code to port // kodo siuntimas i porta
void SendGraph (void) 
{
		char send_code[50]; 
    
	GetCtrlVal (mainpnl, MAINPNL_SIG1AMP , &sig1amp);
	GetCtrlVal (mainpnl, MAINPNL_SIG1PHASE, &sig1phase); 
	GetCtrlVal (mainpnl, MAINPNL_NUMPOINTS, &numpoints);
	GetCtrlVal (mainpnl, MAINPNL_CYCLES, &cycles);

	   send_code[0] = sig1amp;
	   send_code[1] = sig1phase;
	   send_code[2] = numpoints;  
	   send_code[3] = cycles; 



	   
	   
    stringsize = StringLength (send_code);
    bytes_sent = ComWrt (comport, send_code, stringsize);
}

 then i get parameters and plot sinus graph,

 

 

 

int CVICALLBACK Gavimas (int panel, int control, int event,
                              void *callbackData, int eventData1,
                              int eventData2)
{
	double wave2[2048];
	int numpoints2;
	double sig1amp2;
	double sig1phase2;
	double cycles2;
	
    switch (event)
        {
        case EVENT_COMMIT:
            read_data[0] = '\0';

            read_term = 13;
    
   if (read_term)
                bytes_read = ComRdTerm (comport, read_data, read_cnt, read_term);

   
   //************************************
   
   sig1amp2 = read_data[0];
   sig1phase2 = read_data[1];
    numpoints2 = read_data[2]; 
	 cycles2 = read_data[3]; 
   
   
   
   SinePattern(numpoints2,sig1amp2,sig1phase2,cycles2,wave2);   

  

    /* Update UIR */
    DeleteGraphPlot (mainpnl, MAINPNL_SIGGRAPH_2, -1, VAL_DELAYED_DRAW);
 

    plot2 = PlotY (mainpnl, MAINPNL_SIGGRAPH_2, wave2, numpoints2,
                   VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID,1, VAL_WHITE);
   

 And i get that error... it seems that i cannot get good parameters.... something bad with sending information to the port?

0 Kudos
Message 3 of 17
(3,310 Views)

Yes, you are getting wrong parameters... or better you are sending wrong parameters!

 

In your SendGraph function there is no informations about the type of the parameters retrieved with GetCtrlVal, but in send_code[0] = sig1amp; and following lines it is assumed that they are all char (i.e. 1-byte values!).

Next in your Gavimas callback you try to retrieve those values in variables of int size (i.e. 4 bytes). You cannot get anything good this way!

 

Why not formatting a string with values (assuming the are all INTs, otherwise the appropriate formatting code will need to be used):

sprintf (send_code, "%d; %d; %d; %d", sig1amp, sig1phase, numpoints, cycles);

 

and scanning back values from received string? (again, the scanning code will need to be adapted to actual data received)

Scan (read_data, "%d[x]%d[x]%d[x]%d", &sig1amp2, &sig1phase2, &numpoints2, &cycles2);

 Additionally, in SendGraph have you tested how many bytes are you sending? Remember that when sending packed data like you are using, you cannot use string operators like StringLenght as they will stop at the first NUL byte found! You are very likely to have stringsize = 0.

 

Only after you are sure the correct parameters are formatted into the string and sent through RS232 and are received and scanned you can try plotting the signal.

 

Really, it appears as you are missing some vary basic concepts about programming: size and type of variables, string formatting and scanning are concepts you cannot avoid to study if you want to produce good, working and robust code.

 



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 4 of 17
(3,305 Views)

 

 

EDIT,: i read your reply, will edit and see how it is going.

0 Kudos
Message 5 of 17
(3,304 Views)

 Additionally, in SendGraph have you tested how many bytes are you sending? Remember that when sending packed data like you are using, you cannot use string operators like StringLenght as they will stop at the first NUL byte found! You are very likely to have stringsize = 0.  

 


So what operators should i use?

I am new at this but i am eager to learn.

0 Kudos
Message 6 of 17
(3,299 Views)

No operators can be used! If you want to pack informations in raw format you must keep an independent count of the bytes to transfer.

In your case you could dump the ints into the string but given the limited number of informations to transfer it will unnecessarily add complexity to the code; I suggest you to go through the format/scan way before as I explained in my previous post.



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 7 of 17
(3,290 Views)

In my previous posts I erroneously told that you are reading data back into variables of type int, while you are correctly using doubles as they are needed by SinePattern function.

 

Given this, the formatting string must be corrected accordingly, e.g. this way:

sprintf (send_code, "%.3f; %.3f; %d; %.1f", sig1amp, sig1phase, numpoints, cycles);

(you may want to modify the number of decilam digits put into the resulting string on double parameters according to your actual needs).

 

Similarly the scanning formula must treat doubles when required.



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 8 of 17
(3,287 Views)

So you are saying that i should use not string?

that what to write in : bytes_sent = ComWrt (comport, ch, ????);

 

 

/*---------------------------------------------------------------------------*/
/* Send to port                                         */
/*---------------------------------------------------------------------------*/
void SendGraph (void) 
{
		char ch[70]; 

	
	sprintf (ch, "%.3f; %.3f; %d; %.1f", sig1amp, sig1phase, numpoints, cycles);
												
	   
	   
    stringsize = StringLength (ch);
    bytes_sent = ComWrt (comport, ch, 70);
}




/*---------------------------------------------------------------------------*/
/* Get graph                                      				         */
/*---------------------------------------------------------------------------*/
int CVICALLBACK Gavimas (int panel, int control, int event,
                              void *callbackData, int eventData1,
                              int eventData2)
{
	double wave2[2048];
	int numpoints2;
	double sig1amp2;
	double sig1phase2;
	double cycles2;
	
    switch (event)
        {
        case EVENT_COMMIT:
            read_data[0] = '\0';

            read_term = 13;
    
   if (read_term)
                bytes_read = ComRdTerm (comport, read_data, read_cnt, read_term);

   
	Scan (read_data, "%f[x]%f[x]%d[x]%f", &sig1amp2, &sig1phase2, &numpoints2, &cycles2);
      
   SinePattern(numpoints2,sig1amp2,sig1phase2,cycles2,wave2);   

    /* Update UIR */
    DeleteGraphPlot (mainpnl, MAINPNL_SIGGRAPH_2, -1, VAL_DELAYED_DRAW);
 

    plot2 = PlotY (mainpnl, MAINPNL_SIGGRAPH_2, wave2, numpoints2,
                   VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID,1, VAL_WHITE);
            break;
        case EVENT_RIGHT_CLICK :
            break;
        }
    return 0;
}

 

0 Kudos
Message 9 of 17
(3,284 Views)

Forget it! I was saying that you may transmit some data in "raw" format" (that is, the raw memory content related to each variable, 8 bytes per doubles, 4 per integers) but in this case you cannot use strlen or StringLenght as thay could be misleaded by nul bytes.

If you are using sprintf as in your last code StringLenght is perfectly safe, the same as strlen.



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 17
(3,278 Views)