Return value for SerialEvent

hey everybody,
is it possible to return an Array from a Serialevent() to another function() in a librarie.
Thx for any help :slight_smile:

RFID.cpp (11.3 KB)

RFID.h (660 Bytes)

Not to return it. But you can fill an array in the SerialEvent and set a flag for another function to use that array.

If you find this answer to cryptic, add more into. See How to use the forum.

thy for the quick Answer. can u give me an Example how to do it :)

pispa: thy for the quick Answer. can u give me an Example how to do it :)

Yes, of course - right here

i mean how to set a flag for another function to use that array.

Here you go:

const byte serialInputBufferLength = 64;  // serialInputBufferLength - 1 is the maximum length of string that can be received
char serialInputBuffer[serialInputBufferLength];
bool serialDataAvailable = false;

void setup() {

void loop() {
  if (serialDataAvailable == true) {
    serialDataAvailable = false;  // reset the flag variable

void serialEvent() {
  const byte bytesRead = Serial.readBytesUntil('\0', serialInputBuffer, serialInputBufferLength - 1);
  serialInputBuffer[bytesRead] = '\0'; // add terminator to the read string
  serialDataAvailable = true; // set flag variable to indicate data is available

hey everybody,
can someone tell me if it possible to Return an Array from a serialevent() to another function in a CPP file, if so, how ?
Thanking you in anticipation

int RFID::Read_Block()
Senden(RB_Nutzbytesanzahl,RB_Nutzbyte1, RB_Nutzbyte2);
digitalWrite(Send_Recievepin, LOW);
while (Serial.available()>0){
unsigned char;
if (Ackresponse == 0x77 ){
// Serial.println(“Ackresponse, HEX”);
// Serial.println(Ackresponse, HEX);
return Ackresponse;
else {
Byte B =SerialEvent();

byte* serialEvent()

unsigned char empf[RB_Empfangsbyteslaenge];

while ( == 0xAA){

Serial.println(“Serialevent tritt ein”);
empf[0]= 0xAA;
for (int i=0; i<RB_Empfangsbyteslaenge; i++){
Serial.println(empf*, HEX);*

  • delay(1);*

  • } *

  • }*
    *return empf; *


i just wanted to explain it clearlier sorry

That's fine, you can do so here. But don't start a new topic ;)

I think pert made a more than generous example for the amount of info you give.

i want to return it to the Read_block() function. ist a bit complex to do it in a librarie

Start here

Try to spend more then just 2 lines on describing your problem :)

Also, please read How to use the forum.

Please take some time to study the example sketch I provided. I think that demonstrates what you need to do well enough. If you have any specific questions about it, then you need to take the time to ask them in a detailed manner and we'll try to help you out. Being super vague is only going to make it take longer for you to find a solution.

ok fine , i try to read values from my RFID sensor.

the sensor return all the time 0x77 as lond no Tag is detected. if one is detected, it return me an Array of Value biginning with 0xAA. i want to obatai this Array and return it to a function of the RFID class, to wit int RFID::Read_Block(). so i have to declare the Array so that i can use it in both function (RFID::Read_Block() and Serialevent()). i hope i could explain it nearlier

The forum also has a German language section.

Alright, couple of things:

  • Serial is pretty slow
  • Serial has NO clue what is send and will just receive char by char. Has NO clue when the data is "done"
  • Aka, a single SerialEvent probably will not receive all the data.
  • So you have to somehow check it's done. Until then, just keep adding data to the array. Set flag when done.

With the knowledge of this, try to make a [url]MCVE[/url] and we will go from there.

can u Show me please how u would do it in a cpp file based on my example. i have no idea how to realize it. thx again for your efforts

You don’t need a return value, you just need a state machine.

so i have to declare the Array so that i can use it in both function (RFID::Read_Block() and Serialevent()).

That’s why I made the serialInputBuffer array global in my example:

What you need to understand is that there is no magic to serialEvent. There is some code that runs each time loop() exits that essentially does this:

if (Serial.available() > 0) {

That’s it. It might seem like some mysterious thing because you don’t see serialEvent() getting called, but it’s really pretty stupid and pointless. I believe that many years ago serialEvent() was a called from an ISR, but then they got rid of that. If you have a long delay in loop, then you will have a long delay before serialEvent() gets called. If your code makes it so that loop() never exits, then serialEvent() never gets called. It’s quite likely that serialEvent() is not appropriate for what you’re trying to do.