LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Mathscript Array -problem

Dear Friends,

I am reading the serial port data data , byte by byte..I used serial port vi and math-script in it ....
And these  serial data are sent in a format like below........

format:  HEADER ,  MSB  , DATA ,  CHECKSUM..

I need to collect these bytes and plot it on the graph.. I have problem with saving these data in an array and processing it....

below is the code,which i had written to do the process

 persistent  ubIndex = 0
 persistent  bHeader = 0
 

ubTemp=input

 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


In the above program,i collected 2nd, 3rd,4th byte in the ubRecieveData array  and i left the 1st byte(which is header)...
and I find the checksum by adding the 2nd,3rd,4th byte to find whether it is zero or not,
if it is zero ,then the received bytes are correct...and it will proceed to take only the data bytes...and i will plot it on the chart...

I have a doubt in the ubrecievedata array..whether it is collecting the 3 bytes data or not......whether array  get's initliazed every time in the script..
since it doesn't get in to the if-loop checksum and loop further.....

I even removed the if-loop checksum ,to collect and process the data inside the loop below if-loop checksum..but it's not working ...

will the above script are correct..... .....?suggest some solutions to get working

i attached my  program vi..for u to get feel of my problem...

regards
rajasekar


0 Kudos
Message 1 of 4
(7,580 Views)
Hello,

The script you posted currently has an error.  It looks like you are trying to use the MathScript persistent keyword like the C static keyword.  Unfortunately, you cannot initialize a MathScript persistent variable at declaration.  See this post for one solution.  A simpler way is to check if the variable is empty, and if so, initialize it.  For example

persistent ubIndex bHeader

if isempty(bHeader)
    % Init variables
end


As for your other question, since your ubReceiveData variable is not declared as persistent, it will be reset to a default state whenever this script runs again.  Another issue is that you are always computing the checksum... even on iterations where you are still reading the necessary bytes from the serial port.  Perhaps you can compute the checksum and perform the necessary operations if it's zero only after you've read all the bytes.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 2 of 4
(7,571 Views)
Hi Grant,

I did as u told u,still unable to process the data and plot it on the chart.....
I attached my program  testing20.vi ....for finding the problem..
could u please see the vi..and suggest some solutions....

As u said  the array should also be persistent.....and my doubt is the other variable like dulen1,dulen2,dulen3,dulen4,...should also be persistent...in order to calculate the parameter
duLength,.......similarly the parameter uwpackagespeed and uwdrumspeed should be persistent.(in my point of view....)

objective of the program is to read the serial port byte and byte ...and plotting it
And the byte are sent in format like

format: Header , MSB ,data,checksum..
             Header, LSB ,data,checksum..

so i need to recogize this format in my mathscript node ...the main thing is ,...... in 4 bytes the data is only of 1byte....
In my program , i am plotting length(x-axis) versus packagespeed and drumspeed(y-axis)..
here the length parameter itself 4 byte of data.....i have to collect it byte by byte of data ,...from the 4 byte format(math script gets called 16 times to collect these 4 data bytes
 from 4 byte format)...        and packagespeed and drumspeed is of 2 byte.......
if u have any questions please ask me...

Eagerly awaiting for ur reply....

Regards
rajasekar





0 Kudos
Message 3 of 4
(7,565 Views)
Hello,

There are still some problems with the persistent variables.  Line 4 of the script reads
persistent   ubRecieveData,ready

While this is valid syntax, it does not do what you want it to do.  The comma tells LabVIEW MathScript to interpret this line as
persistent   ubRecieveData
ready


The implication is that the ready variable should be displayed to the screen.  Since ready is not defined, this results in an error.  You should remove the commas from the list of persistent variables.  However, I also discovered that there is a bug in the implementation of the persistent function.  Unfortunately, you cannot declare multiple variables as persistent on the same line.  You will need to declare each variable on a separate line.  This may be fixed in a future release, but until then, write the commands like
persistent ubRecieveData
persistent ready


There are still some more problems with the script after fixing the persistent variables.  Try creating an error out indicator for the MathScript node.  Unfortunately, in LabVIEW 8.0 and 8.2, the MathScript node does not implement automatic error handling like other nodes in LabVIEW.  If you don't create an error out indicator, these errors are silently ignored.  This should help you identify the remaining problems with the script.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 4 of 4
(7,555 Views)