Arduino Leonardo Serial Issue..

So I have this code which is somewhat based on the Serial Basics code but a little different… (i may have screwed something up here) … The Board is a Pro Micro Clone (essentially a Leonardo).

Anyway the issue is what I upload the code and then send data to it It will work fine for the first Loop but then lock up and stop working… Mind you it does not lock up the entire Arduino as a loop print function will till work…

The function is under SerialEvent because all of this code works on a Teensy LC just fine which has support for the serialEvent(), since the Leonardo sorest I just put a serialEvent() call in the loop… I have changed the name before so it is not confusing things by having the name as it doesn’t work either way…

So here is the Basic Code…

//------------------------------------   Serial Definitions and Initiaizations   ------------------------------------
  #define SERIALDATAPKG 8                               //actual serial package size in bytes used for processing
  #define SERIALSTARTCHECK 0xFF                         //serial error check character start position (serial2)
  #define SERIALENDCHECK 0x00                           //serial error check end position (serial)
 
  // serial function variables
    byte serialStep = 0;                                // tracks how many processor loops have passed for serial.read
    byte incomingByte;                                  // single byte serial.read variable
    const byte serialBufferSize = SERIALDATAPKG;        // set serialBufferSize and adjust serialBufferSize for SERIALENDCHECK (if needed)
    byte serialBuffer [serialBufferSize];               // incoming serial buffer
    byte serialData [SERIALDATAPKG];                    // serialData package
    const byte endOfPkg = SERIALDATAPKG - 1;            // serial package length (adjustment for 0)

//------------------------------------   Main Program   ------------------------------------
  void setup() {
     Serial.begin( 115200 );
      while (!Serial) 
  {
   Serial.println( "wait for connect"); // wait for serial port to connect. Needed for native USB
  }
      Serial.println( "READY!" );
  }

  void loop() {
      serialEvent();
  }


//------------------------------------   Incomming Serial   ------------------------------------
  void serialEvent() {

    if (Serial.available() > 0) {
        Serial.println("Serial Receive");
//      delayMicroseconds(1500);
      incomingByte = (byte)Serial.read();                          // get a single byte    
      if ((incomingByte == 0xFF) && (serialStep != 1)) { 
        serialStep = 0;
      } 
      if (serialStep != endOfPkg ) {
        serialBuffer [serialStep] = incomingByte;                  // place read Byte in buffer array.
        serialStep = serialStep + 1;                               // step for next processor loop
      } else if (serialStep == endOfPkg) {                         // check for end of package defined by SERIALDATAPKG
        serialBuffer [serialStep] = incomingByte;                  // place read Byte in buffer array.
        serialStep = 0;                                            // reset counter
        if (serialBuffer[endOfPkg] == SERIALENDCHECK) {            // data checksum byte check (error check)
          for (byte c = 0; c < SERIALDATAPKG; c++) {
            serialData[c] = serialBuffer[c];                       // copy serialBuffer to serialData
          }
        }
        for (byte r = 0; r < serialBufferSize; r++) {
          serialBuffer[r] = 0x00;                                    // reset serialBuffer
        }
            Serial.println("Serial Data");
    Serial.print (serialData[0]);Serial.print (" ");Serial.print (serialData[1]);Serial.print (" "); 
    Serial.print (serialData[2]);Serial.print (" ");Serial.print (serialData[3]);Serial.print (" ");
    Serial.print (serialData[4]);Serial.print (" ");Serial.print (serialData[5]);Serial.print (" ");
    Serial.print (serialData[6]);Serial.println ();
      }
    }
  }

Here is the data I am sending to the Arduino in HEX

FF00000303030300FF04000303030300FF00010303030300FF04010303030300FF00020303030300FF04020303030300FF00030303030300FF04030303030300FF00040303030300FF04040303030300FF00050303030300FF04050303030300FF00060303030300FF04060303030300FF00070303030300FF04070303030300FF00000404040400FF04000404040400FF00010404040400FF04010404040400FF00020404040400FF04020404040400FF00030404040400FF04030404040400FF00040404040400FF04040404040400FF00050404040400FF04050404040400FF00060404040400FF04060404040400FF00070404040400FF04070404040400FFFFA00000000000

This is a typical single set of generic data.

When I send it I get what is expected the first time sent:

Start-up:
wait for connect
READY!

Then a a series of :

Serial Receive
Serial Receive
Serial Receive
Serial Receive
Serial Receive
Serial Receive
Serial Receive
Serial Receive
Serial Data
255 0 4 3 3 3 3

Going through every 8 bytes (I didn’t post out all as 32 loops of that gets long)- though for some reason the very LAST “FFFFA00000000000” does not show up so it may be getting locked here. but I haven’t been able to figure it out.

However when I try to send the same data over again I would expect it to redisplay the same thing as before without the Start-up Lines… It Doesn’t, nor does it even seem to be receiving data as the Receive LED does not light…

If anyone has any Ideas or direction to point me to figuring this out. it would be greatly appreciated.

So an Update

It tried using this method for the serial but it was doing less than the first:

    if (Serial.available() > 0 && incomingData == false) {
      incomingByte = (byte)Serial.read();                        // get a single byte    
      if (recvInProgress == true) {
        if (incomingByte != SERIALENDCHECK) {
          serialBuffer[serialStep] = incomingByte;
          serialStep++;
          if (serialStep >= serialBufferSize) {
            serialStep = serialBufferSizen - 1;
          }
        } else {
          serialBuffer[serialStep] = '\0';                      // stop receiving data
          recvInProgress = false;
          serialStep = 0;
          incomingData = true;
        }
      } else if (incomingByte == SERIALSTARTCHECK) {
        recvInProgress = true;
      }
      if (incomingData == true) {
        for (byte c = 0; c < SERIALDATAPKG; c++) {
          serialData[c] = serialBuffer[c];                      // copy serial2Buffer to serialData
        }
        for (byte r = 0; r < serialBufferSizen; r++) {
          serialBuffer[r] = 0;                                    // reset serialBuffer
        }
                    Serial.println("Serial Data");
    Serial.print (serialData[0]);Serial.print (" ");Serial.print (serialData[1]);Serial.print (" "); 
    Serial.print (serialData[2]);Serial.print (" ");Serial.print (serialData[3]);Serial.print (" ");
    Serial.print (serialData[4]);Serial.print (" ");Serial.print (serialData[5]);Serial.print (" ");
    Serial.print (serialData[6]);Serial.println ();
      }  
    }

In that it would only output al Zeros and only one time and then lock up on the second attempt… Im thoroughly confused here…

OK so it turns out the first code works but requires a transmit delay.. So I will have to figure that out with the Software developer that I am am reading from as the Pro Micro doesn't seem to be able to take all the information at full speed without a bit of extra time in there..

So I will have to figure that out with the Software developer that I am am reading from as the Pro Micro doesn't seem to be able to take all the information at full speed without a bit of extra time in there..

The Pro Micro can receive data at a very fast speed, there is no need for any delay if you are receiving it properly.

Unfortunately you are not. That code is way way too complex for what it has to do, and all that serial printing will slow things down tremendously.

As you are only posting snippets may I refer you to this website http://snippets-r-us.com/

    else if (serialStep == endOfPkg)

{ // check for end of package defined by SERIALDATAPKG
      serialBuffer [serialStep] = incomingByte;                  // place read Byte in buffer array.

serialBuffer does not have an element numbered 8.