8 years, 11 months ago.

Why is abort_read() not working?

Hello Everybody!

I am making a program that receives Serial data from the computer, however, it only receives a few bytes, but i dont know how many. I want to make something that stops the microcontroler from reading more than a 'x' number of bytes. I tryed to use the code bellow, but I received the folowing message:

Error: Class "mbed::Serial" has no member "abort_read" in "main.cpp", Line: 46, Col: 8

Abort_Serial

/*****************************************************************/
/*         FAST WAY TO RECEIVE MULTIPLE BYTES FROM PC            */
/*****************************************************************/
// The computer is going to send a start byte, which is 0x7E
// Then, it will send 3 more bytes(OR MORE). 

#include "mbed.h"

DigitalOut myled(LED1);
Serial pc(USBTX,USBRX);

/*******Variáveis*******/

uint8_t t;
uint8_t BUFFER[10]; // Buffer that contains the comand sent from the computer.
                    // Buffer[0...(n-1)] = Bytes sent
                    // Buffer[n] = Check sum
uint8_t* pbuf=&BUFFER[0];
uint8_t cont = 0;   // Receives a temporary byte before passing to the BUFFER[];
uint8_t Size = 0;   // Size of the Package Received
uint8_t num = 0;    // Position of the buffer

/*********Flags*********/
bool rec;   //Sets when the buffer receives more data

/********Funções********/
void HandlerRx(void);

int main() {
    myled=0;
    pc.baud(9600);
    pc.attach(&HandlerRx, pc.RxIrq);
    while(1) {}
    
}
void HandlerRx(void){
    uint8_t* tempbuf = pbuf;
    uint32_t counter = 0;
     pc.putc(12);//Debug
     
    do{
    *tempbuf = pc.getc();  //Receives the byte, Mudar para Ponteiro.
    tempbuf++;
    counter++;
    }while(pc.readable()&&(counter<(4)));//Maximum number of bytes that may be received;
    pc.abort_read();
    
    tempbuf = pbuf;
   
    pc.putc((*tempbuf)+1); 
    

    
    return;
    }

1 Answer

8 years, 11 months ago.

abort_read is not an existing function. Or it might be new function introduced for new async API, but that is not yet supported by pretty much any board.

But there is no reason to call the function in the first place, it is not required. Just stop your loop when you want to stop reading (do make sure you only call getc() if readable is true, since otherwise it will block until something is received. But it seems you already do this).

Accepted Answer

As Erik pointed, abort_read() is for asynch api

posted by Martin Kojtal 28 May 2015

Hello Eric, Thanks for the quick answer! It seems to me that Handler Rx will be called, until all bytes are read from HandlerRx function. At least thats is whats happening with me...

posted by Thiago . 28 May 2015

Yes that is correct, it will keep being called until it is done. If you dont want that you need to disable the interrupt.

posted by Erik - 28 May 2015