LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

mathscript variable doubt-help me

Dear Experts and friends,

I reading a serial port one by one character at a time...(i specified in " bytes to read in serial port read node)
The receiving data are sent in a fixed format..and so i need to extract  the data from these format..by using a math script node...and i need to plot it on the xychart...
my problem is with initializing a variable by only once..............and the variable should not re-initialized again and again since program keeps on reading the serial port data....

I need to intialize a these 2 variable  given below only once,while i am start executing.but not again and again..........(meaning that when i start executing for the first time these 2 variable should be intialized,...but when i doing for the 2nd time it should not be initialized again--since i am reading it using while loop.........and these parameters are my reference parameters to find out the data......

ubIndex = 0
bHeader = 0


--------------------------------------------------------------------------
here is math script code which i have used.......

ubIndex = 0
bHeader = 0
ubTemp=input    //input is the variable i used as input to the mathscript node in char string format

 if bHeader ==0
                 if ubTemp == 89
                        ubIndex= 0
                        bHeader = 1                              
                   end

else
              ubIndex= ubIndex +1
              ubReceivedata(ubIndex) = ubTemp      
               if  ubIndex == 3
                       ubIndex = 0
                       bHeader = 0               
                 end
end      

 ubChecksum = ubRecieveData(1) + ubRecieveData(2) + ubRecieveData(3)
 
if   ubChecksum == 0

               if ubRecieveData(1) == 76
                        duLen1 = ubRecieveData(2)

                             elseif   ubRecieveData(1) == 69
                                    duLen2 = ubRecieveData(2)

                             elseif   ubRecieveData(1) == 78
                                       duLen3 = ubRecieveData(2)
                              
                               elseif ubRecieveData(1) == 71
                                            duLen4 = ubRecieveData(2)
                                            duLength = duLen1 * 16777216 + duLen2 * 65536 + uwLen3 * 256 +  ubLen4 
       
                              elseif   ubRecieveData(1) == 80
                                           uwTemp1 = ubRecieveData(2);

                               elseif  ubRecieveData(1) == 66
                                            ubTemp2 = ubRecieveData(2);                 
                                            uwpackageSpeed = uwTemp1 * 256 + ubTemp2;

                                 elseif  ubRecieveData(1) == 68
                                          uwTemp1 = ubRecieveData(2);

                                 else  ubRecieveData(1) == 65
                                          ubTemp2 = ubRecieveData(2);                 
                                         uwDrumSpeed = uwTemp1 * 256 + ubTemp2;

                   end

end

//output variables are duLength ,uwpackagespeed,uwdrumspeed
-----------------------------------------------------------------------------------------------------
bHeader and  bIndex should not be re-intialized again...

please see the vi file i attached for better understanding of my problem...........(vjnewsamp.vi)
Kindly suggest some solutions to overcome this problem ..........

regards
rajasekar
0 Kudos
Message 1 of 2
(6,601 Views)
Hello,

One way to accomplish this is to use persistent variables in the MathScript Node.  A persistent variable will retain its value in-between invocations of the node.  Simply declare your variables using the persistent keyword.
persistent ubIndex bHeader

Then, you can pass in an initialize boolean that corresponds to the first iteration of the loop.  In your code, you can initialize the variables the first time using
if (init)
   ubIndex = 0;
   bHeader = 0;
end


The simplest way to pass in the initialize boolean is to wire the Equal To 0? primitive to the loop iteration terminal.  Then wire its output to a boolean input variable on the MathScript Node.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 2 of 2
(6,592 Views)