Arduino Micro as Serial (COM) to SPI translator

Hello folks,

i'm trying to use the Arduino Micro for the above mentioned purpose. My Serial Software on the PC sends out data in packages of 30 characters to the Arduino, makes a short break (20ms), and then sends the next 30 characters and so on. My goal is, to forward these 30 characters to an AVR daisy chain each time they arrive at the Arduino but i'm a bit confused on how to code that. Do I have to empty the serial buffer from time to time, to avoid getting into trouble? Can anyone give me a hint on the problem?

Thank you very much in advance.

Regards

Do I have to empty the serial buffer from time to time, to avoid getting into trouble?

Yes.

Hi and thanks for your reply michinyon,

how do i actually clear the buffer? is it enough to just read it like this for example:

if (Serial.available() > 0)
    {
        var = (Serial.read());
    }

If you want to throw away everything in the incoming serial buffer then you can do this:

while(Serial.available()) {
  Serial.read(); //take one char off the incoming buffer and throw it away
}

This sort of thing is not usually required if you stay in sync with the incoming data and interpret it properly as it arrives.

Hello and thanks MorganS,

actually i don’t want to throw away any of the incoming data before processing it. I have some code working more or less, but at some point after a certain amount of characters received from the PC, the Arduino seems to “crash” or something.

thats the code:

void loop()
   {
      if (Serial.available() > 0)
         {
            data = (Serial.read());
            Serial.println(data);   //just for testing puposes with the serial monitor
            if(recchar == 0)
               {
                  digitalWrite(SSPin, LOW);
                  delay(5);
               }
            SPI.transfer(data);
            recchar++;
            if(recchar == 30)
               {
                  digitalWrite(SSPin, HIGH);
                  recchar = 0;
               }
         }
   }

Prizefighter: Do I have to empty the serial buffer from time to time, to avoid getting into trouble?

What sort of trouble?

actually i don't want to throw away any of the incoming data

So you don't want to empty the serial buffer?

Actually i don't know if i have to empty the buffer.

This is my understanding of the serial interface (but maybe i completely misunderstand something):

The arriving serial data (characters) from the PC is written to the serial input buffer of the Arduino. When the Arduino reads a character from the buffer (serial.read()), this single character is cleared from the buffer and can be written with a new incoming character.

I'm not sure about the last part... does it happen like that, or do i have to free the bytes in the buffer manually? Can the serial buffer of the Arduino be seen as a shift register?

The serial buffer is a circular buffer. As long as you read from it faster than data arrives, nothing will be lost.

No, you don't need to free the bytes manually. How would that be useful?

x = Serial.read();
Serial.free();  // free the space used by that byte

What would that serve? Of course, once you read from the buffer, that place is freed.

Okay, so basically I imagine the serial buffer is like a conveyor belt and on one end the PC puts characters on the belt and on the other end the Arduino grabs the characters. If the Arduino is not fast enough in grabbing the characters, they will drop down at the end of the belt. Is this model correct?

More like, the belt is full, so new ones won't be added. You don't lose the head of the queue, the tail is not added to. It's like a queue at a movie theatre. If it is too long, people walk away (they don't join the queue). Naturally people at the head of the queue, who are about to get their ticket, stay there.

Alright, now I understand the mechanism. For me it’s way easier to catch things when they are explained in pictures, thanks for that Nick! :slight_smile: