I want to receive a message of variable length which is encapsulated in 
0xAA bytes.  Here's my current method, which repeatedly calls SerReceive 
asking for 1 character.  Is there a better/more efficient way? (I'm 
guessing there probably is)

       Michael

        #define bufSize 200

        Err err;
        Byte buf[bufSize];
        Byte cTemp;
        UInt iTemp;
        UInt numChars = 0;
        Long timeout = SysTicksPerSecond();

        err = SerOpen (SerialLibRefnum, 0/*port*/, 9600/*baud*/);
        if ( err == serErrAlreadyOpen ) {
                err = SerClose (SerialLibRefnum);  // we don't want to share or disrupt
                iTemp = StrPrintF(debugBuf, "Error:  Serial port already in use...");
                WinDrawChars(debugBuf, iTemp, 10, 70);
                return -1;
        }

        // receive the first character (should be 0xAA)
        iTemp = SerReceive(SerialLibRefnum, &cTemp, 1, timeout, &err);
        if ( err == serErrLineErr ) {
                SerClearErr (SerialLibRefnum);
        }
        if (err != 0)  {
                return -1;
        }
        else if (cTemp != 0xAA)         // only continue if 1st byte was 0xAA
                return -1;
        while (1)  {
                iTemp = SerReceive (SerialLibRefnum, &cTemp, 1, timeout, &err);
                if ( err == serErrLineErr ) {
                        SerClearErr (SerialLibRefnum);
                }
                if (err != 0)  {
                        return -1;
                }
                if (cTemp == 0xAA)  {
                        break;
                }
                if (numChars >= bufSize)  {
                        return -1;
                        break;
                }
                buf[numChars++] = cTemp;
        }
        // at this point, numChars == the size of the message, which is stored in 
buf

Reply via email to