How use serial interrupt?

Hello Everyone!
I'm working with nano 33 IoT for BLE.
My system connects nano 33 IoT through UART.
Nano 33 IoT is getting some data from UART and updates characteristic the data.
My code in loop is like below.

loop()
{
            while(Serial1.available()){
                incomingString1 = Serial1.readStringUntil('\n');    
                incomingString1.toCharArray(receveDataArray1,20);                
                Serial.println(incomingString1);
                infoDataCharacteristic.writeValue((uint8_t*)receveDataArray1, 20); 
                buffCharacteristic.writeValue((byte)0x1); 
            } 
}

It works well but there is some timing delay when I added another functions.
So I want to handle this as an interrupt.
As I know, Nano 33 can't use serialevent().
How can I use Uart interrupt?

Please explain more.

I'm not familiar with the Nano33 IoT, but I'm reasonably sure that Serial1 is already handled by interrupts.

Why use delay?

Can you please post a complete representative code example that exhibits the behaviour?

delay(10) means nothing.

Welcome to the forum.

Please post your code and explain your application a bit to allow us to give you better answers.

As sterretje mentioned the UART is already handled by interrupt. You are talking to a buffer in RAM.

Why is that? The SAMD21 could execute every instruction that could fit into its memory in less than 3ms.

About your code snipped:

  • You are using while. That is usually not a good idea because it can block your code for a random amount of time.
  • Then you follow up with a readStringUntil that can block your code for even longer.

I would recommend you try to

  • make the loop run as often as possible
  • assemble the String character by character, build in some error checking in case no newLine is send
  • you can print every character you receive back to Serial Monitor while they arrive, the same principle of a buffer applies here, you write to a buffer and the USB stack puts everything into packets and sends them when it can
  • if the String is complete update the characteristic
  • if you like you can even avoid using Strings altogether, use a char array instead

This will allow you to extend the sketch with many other things without breaking what you already have. Try to avoid delay, while, waiting for anything ...

May I ask why you use a String characteristic? Many new BLE user do this to create a serial pipe. This is wasting the smart ideas of GATT. BLE was intentionally not designed as a serial replacement. There is no Serial Port Profile in BLE while there is one in Bluetooth Classic.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.