Serial / UnbufferedSerial changes to .read() between mbed 5 and mbed 6

Hi,

I am performing an upgrade from mbed-os 5.8.3 to 6.2.0 and I notice through the API deprecation that

int read ( uint8_t * buffer, int length, const event_callback_t & callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH )

has now become a private member function of the SerialBase class, and UnbufferedSerial now uses just two parameters: buffer and size.

My question is: in 6.2.0, and moving forwards, what is considered the best practice for achieving a callback upon some event, i.e. just like the previous function? How could I still use this function?

Any insights greatly appreciated.

Thanks

1 Like

I’d also like to know how this is meant to be achieved now.

I’m trying to set up a callback that’s called for each new line, so that user-sent messages on the serial bus can be handled one line at a time. As far as I can tell, the only way to do this now is one character at a time (getc in USBSerial or read 1 byte) and by processing of arbitrary chunks with the new read() functions.

Is BufferedSerial meant to now be managed from a thread or at some timer interval?

For example

BufferedSerial BufSerial(USBTX,USBRX,115200);         
void serialInputThread()
{
	BufSerial.set_blocking(false); //read bytes in large chunks less often
	char buf[512];
	ssize_t read_status;
	
	while(true)
	{
		read_status = BufSerial.read(buf, 512);
		
		if(read_status > 0)
		{
			//Handle potentially large chunk of text
			//eg call handler every time special character is encountered
			//Alternatively, use strtok and split on the special character and then handle each token
			for(int i=0; i < 512; i++)
			{
				//...process input...
			}
     
            //echo chars back (note: on Windows will carriage return but not line feed)
            serialInterface->write(buf, read_status);
    	}
		
		ThisThread::sleep_for(5ms);
	}

}

It seems like there’s some intended smart way to do serial input/output but I don’t see any examples or hints in the documentation.

edit1: I’ve noticed some File related methods size(), seek(), rewind(), etc. The default rx/tx circular buffers size in mbed_lib.json is 256 bytes, so at that size needs to be checked fairly often. Are these methods actually useful? I’m confused as to how they fit in with the read() and write() methods.