No announcement yet.

Reception of buffer not blocking

  • Filter
  • Time
  • Show
Clear All
new posts

  • Reception of buffer not blocking

    Hi everyone,

    I have an application where the 4D Diablo screen communicates through com port 2 with a MCU. The protocol is quite fixed, Diablo send a buffer of 6 bytes and the MCU answered with a 10 bytes message.
    Until now I am using:

    //Config COM 2 to active loads (PA8,PA9)
    com_SetBaud(COM2, 960); //Set baud to 9600 at serial port 1
    com2_Init(rxbuf_2, 10, 0); // set up a rxbuffer for COM1 with START_B as identifer

    //when com is needed in a loop every 5 seconds I use:

    com2_TXblock(str_Ptr(txbuf_2),6); //send to COM2

    It is working fine most of the time, the problem is that sometimes because of errors (noise) the 10 bytes buffer is not arriving complete to the Diablo, so the code is blocked because com2_RXblock.
    I am looking a way of avoiding this blocking of the code.

    Any suggestion?

    Best regards

  • #2
    Hi Rafa,

    You can implement a similar function with a timeout.

    #constant RX_TIMEOUT    1000    // 1000 ms  
    func com2_RXblockTimeout(var ptr, var len)
        // ptr : String pointer to address of buffer to receive the data
        // len : number of bytes to receive into the buffer
        var tmr;
        tmr := sys_T(); // Get initial time
        // Check if not all bytes have been received
        // and Timeout has not been reached
        while ((len > 0) &&(sys_T() - tmr < RX_TIMEOUT))
            if (com2_Count() > 0) // Check if there are bytes available to read
                str_PutByte(ptr++, serin2()); // read the next byte, write it to the buffer and move the pointer
                tmr := sys_T(); // reset the timer used for checking timeouts
                len--; // decrement the number of bytes needed to be received
        // The while loop will end if timeout has been reached
        // or all bytes were received successfully
        return (len == 0); // return true if all bytes are received
    I hope this helps.

    Best Regards,
    Juniel Cruz