View Raw SPL
/*****************************************************************************
*                                                                            *
*   RTREAD.SPL   Copyright (C) 1998 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Example of reading data in real time                        *
*                                                                            *
*   Revisions:   25 Sep 1998  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_RTREAD

    RTREAD

    Purpose: Reads real time data from a file

    Syntax:  RTREAD()

    Returns: Places new series in W1

    Example:
             Start first DADiSP to simulate a real time data source:

             Rttinit("rtwrite")


             Start second DADiSP to read real time data:

             Rttinit("rtread")


             The series generated by the first DADiSP is read
             synchronously by the second DADiSP.  The real time data
             appears in W1 of the second DADiSP.


    Remarks:
             The first DADiSP writes a binary series to the file
             named RTDATA.DAT.  An ASCII value of 1.0 is also written
             to the text file GATE.TXT to indicate new data is
             available.  New data is not written to RTDATA.DAT until
             an ASCII value of 0.0 is detected in GATE.TXT

             The second DADiSP polls GATE.TXT.  If the value is 1.0,
             the file RTDATA.DAT is read and the result is placed in
             W1.  A value of 0.0 is then written to GATE.TXT to
             indicate that the new data was read.

             Both the RTREAD and RWRITE are perfomed in the
             background via RTTINIT.

    See Also:
             Ascale
             Rttinit
             Rttterm
             Rtwrite
#endif



/* real time read data from a file */
rtread()
{
        local a, b, gfile, dfile;

        /* gate file */
        gfile = getmiscpath(1, 1) + "gate.txt";

        /* data file */
        dfile = getmiscpath(1, 1) + "rtdata.dat";

        a = reada(gfile);
        
        /* if gate value is 1, we have new data */
        if (a[1])
        {
                /* read data and set sample rate */
                b = readb(dfile, double);
                setdeltax(b, 1 / 1000);

                /* set w1 to new data, 0 means don't autoscale */
                setwseries(W1, b, 0);
                
                if (strlen(getlabel(w1)) == 0)
                {
                        /* only label window once */
                        label(w1, "Real Time Data");
                }

                /* indicate that we are all done reading the data */
                writea(gfile, {0}, 1);
        }
        
        return(1);
}