USB Serial on Leonardo is affecting a sketch

fdyche:
I said this before I know exactly how many bytes the device will send at all times

Perhaps you know this, but they don't get sent all at once. By its nature serial comms sends one bit after another, at the specified bit rate.

 if (Serial1.available()>0) serByte=Serial1.read();

...

    transferComplete(0);

...

void transferComplete(int dummy)
{
... 
 delay(20);
  for (int i=0; i<21;i++)
  {
    transComplete[0]=Serial1.read();
  }
}

At 19200 baud it will take 1/1920 seconds per byte (520 uS). 21 times 520 uS is 10.92 mS, so clearly you have tweaked your delay there (20 mS) to help the 21 bytes to arrive.

Your code seems to have delays scattered through it, particularly before serial reads. None of these are necessary if you do reading properly. That is, you should check Serial1.available before each one, instead.

I can't get too excited about fixing the "bug" if you don't fix the code first, so it doesn't read data until it knows it is there.


Here, for example, you seem to have forgotten the delay:

void handPay(int dummy)
{
  int i;
  byte temp[24];
  temp[0]=0x01;
  temp[1]=0x1B;
  sendCommand(temp,2);
  for (i=0; i<24; i++)
  {
    temp[i]=Serial1.read();   
  }
}

This isn't right:

 for (i=0; i<Serial1.available(); i++)
  {
    Serial1.read();
  }

After the first byte is read "i" keeps incrementing but Serial1.available() will be decrementing. They will cross over in the middle, and you won't get the correct number of bytes.


Fix the code, then see if it works.