USB Serial on Leonardo is affecting a sketch

I have a sketch that only does I/O to Serial1 on the Leonardo and one LED

if the Leonardo is plugged into a PC then the sketch runs without any real issues unplg it from USB port on computer and power by another power supply and communications between the Leonardo and another device break down The only time I need output to a PC is to debug the sketch on the Leonardo

I have 2 versions of the sketch one that outputs events to the PC (if running the Serial Monitor or another serial terminal programam another with all the output to PC code has been stripped or commented out. I have compiled and uploaded the code a couple times just in case.

I have removed all references to Serial from my sketch and have only initialized and use Serial1. do I have to adjust some of my timings

I have 2 versions of the sketch

Sadly you haven’t posted either sketch.

Simply why would a sketch that does not initialize, open, read, or write to Serial (Leonardo USB port) behave differently when connected to the PC (I am sure the PC initializes the PC portion) than when it is not connected.
which makes no sense to me.

The device I am interfacing with uses fixed size responses and does not send output unless it is polled.

I have documentation on the sizes or use a particular byte to set a limit.
yes I am doing some 9 bit manipulation in the code.
NO PC version

// No PC sketch

byte globalCRC[2];

void setup()
{
  Serial1.begin(19200);
  pinMode(led, OUTPUT);
}

void loop()
{

  int i;    
  byte serByte;
  byte PC;
  byte lastPoll;
  byte bonus[9]={
    0x01, 0x8A, 0x00, 0x05, 0x00, 0x00, 0x00, 0x2E, 0xDD                                      };
  int led = 13;
  int len;
  boolean mainDoorStatus;
  boolean cashDoor;
  boolean cashBox;
  boolean standDoor;
  boolean changeLight;

  UCSR1C = 0b00000110;
  UCSR1B = 0b10011101;  // turn 9th bit on
  Serial1.write(0x80);
  delay(1);
  UCSR1B = 0b10011101;  
  Serial1.write(0x81); 
  delay(1);
  UCSR1B = 0b10011100;  // turn 9th bit off
  delay(50);

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

  switch (serByte)
  {
  case 0x00:
    break;
  case 0x01:
    break;
  case 0x11:
    digitalWrite(led,HIGH);
    break;
  case 0x12:
    digitalWrite(led,LOW);
    break;
  case 0x13:
    digitalWrite(led,HIGH);
    break;
  case 0x14:
    digitalWrite(led,LOW);
    break;
  case 0x19:
    digitalWrite(led,HIGH);      
    break;
  case 0x1A:
    digitalWrite(led,LOW);
    break;
  case 0x1B:
    digitalWrite(led,HIGH);
    break;
  case 0x1C:
    digitalWrite(led,LOW);
    break;
  case 0x3D:
    getTicketInfo(0);
    break;
  case 0x51:
    handPay(0);
    break;  
  case 0x57:
    printTicket(0);   
    break;
  case 0x67:    
    redeemTicket(0);
    break;
  case 0x68:
    transferComplete(0);
    break;
  case 0x71:
    if (changeLight==false)
    {
      digitalWrite(led,HIGH);
      changeLight=true;
    }
    break;
  case 0x72:
    if (changeLight==true)
    {
      digitalWrite(led,LOW);
      changeLight=false;     
      sendCommand(bonus,9);
    }
    break;
  default:
   // Do nothing with other events
    break;
  }
  delay(50);
}

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();   
  }
}

void getTicketInfo(int dummy)
{ 
  int i;
  byte getVal[5]={
    0x01, 0x4D, 0x00, 0xC2, 0xAC                                        };
  sendCommand(getVal,5);
  delay(50);
  for (i=0; i<Serial1.available(); i++)
  {
    Serial1.read();
  }    
}

void printTicket(int dummy)
{
  int i;
  byte sysVal[13];
  sysVal[0]=0x01;
  sysVal[1]=0x57;
  sendCommand(sysVal,2);
  delay(50);
  for (i=0; i<10;i++)
  {
    sysVal[i]=Serial1.read();
  }      
  sysVal[1]=0x58;
  sysVal[2]=0x01;
  for (i=10; i>5; i--)
  {
    sysVal[i]=sysVal[i-3];
  }
  for (i=3;i<6;i++)
  {
    sysVal[i]=0x00;
  }
  computeCRC(sysVal,11);

  sysVal[11]=globalCRC[0];
  sysVal[12]=globalCRC[1];
  sendCommand(sysVal,13);
  delay(20);
  for (i=0; i<5; i++)
  {
    sysVal[i]=Serial1.read();
  }
}

void redeemTicket(int dummy)
{
  int i;
  byte ticket[21];
  ticket[0]=0x01;
  ticket[1]=0x70;
  sendCommand(ticket,2);
  delay(20);
  for (i=0;i<21; i++)
  {
    ticket[i]=Serial1.read();     
  }
  ticket[1]=0x71;
  for (i=4; i<9; i++)
  {
    ticket[i]=ticket[i+10]; 
  }    
  computeCRC(ticket,19);
  ticket[19]=globalCRC[0];
  ticket[20]=globalCRC[1];
  sendCommand(ticket,21);   
  delay(20);
  for (i=0; i<21; i++)
  {
    ticket[i]=Serial1.read();
  }  
}

void transferComplete(int dummy)
{
  byte transComplete[6]={
    0x01, 0x71, 0x01, 0xFF, 0x1F, 0xD0                                        };
  sendCommand(transComplete,6);
  delay(20);
  for (int i=0; i<21;i++)
  {
    transComplete[0]=Serial1.read();
  }
}

void sendCommand(byte temp[], int len)
{
  UCSR1B = 0b10011101;
  delay(1); 
  Serial1.write(temp[0]);
  UCSR1B = 0b10011100;
  delay(1);
  for (int i=1; i<len;i++)
  {
    Serial1.write(temp[i]);
    delay(1);
  }
}

void computeCRC(byte val[], int len)
{
  globalCRC[0]=0;
  globalCRC[1]=0;
  long crc;
  long q;
  byte c;
  crc = 0;
  for (int i = 0; i < len; i++)
  {
    c = val[i];
    q = (crc ^ c) & 0x0f;
    crc = (crc >> 4) ^ (q * 0x1081);
    q = (crc ^ (c >> 4)) & 0xf;
    crc = (crc >> 4) ^ (q * 0x1081);
  }
  globalCRC[0]=crc & 0xff;
  globalCRC[1]=crc >> 8;

}  // End of ComputeCRC

Second sketch – PC logging version
I had to trim my case statements to fit here.
Both work as long the Leonardo USB to Serial Adapter is connected to a PC.

PC logging version

int led = 13;
byte globalCRC[2];


void setup()
{
  Serial.begin(19200);
  Serial1.begin(19200);
  pinMode(led, OUTPUT);
}

void loop()
{
  int i;
  int len;
  boolean mainDoorStatus;
  boolean cashDoor;
  boolean cashBox;
  boolean standDoor;
  boolean changeLight;
  byte curCredits[2] = {0x01, 0x1A};
  byte serByte;
  byte PC;
  byte lastPoll;
  byte bonus[9]={
    0x01, 0x8A, 0x00, 0x05, 0x00, 0x00, 0x00, 0x2E, 0xDD                            };
  byte gDisable[4]={
    0x01, 0x01, 0x51, 0x08                    };
  byte gEnable[4]={
    0x01, 0x02, 0xCA, 0x3A                  };

  if (Serial.available()>0) 
  {
    PC=Serial.read();
    switch(PC)
    {
    case 0x44:
      Serial.print("Disable machine requested by tech\n\r");
      sendCommand(gDisable,4);
      break;
    case 0x45:
      Serial.print("Enable machine requested by tech\n\r");
      sendCommand(gEnable,4);
      break;
    default:
      break;
    }
  }

  UCSR1C = 0b00000110;
  UCSR1B = 0b10011101;  // turn 9th bit on
  Serial1.write(0x80);
  //delay(1);
  //UCSR1B = 0b10011101;  
  Serial1.write(0x81); 
  delay(1);
  UCSR1B = 0b10011100;  // turn 9th bit off
  delay(10);
  serByte=0;
  if (Serial1.available()>0) serByte=Serial1.read();
  if (serByte>0)
  {
    if (serByte != lastPoll)
    {
      lastPoll = serByte;
    }
    else serByte=0;
  }  
  
  // debugging code
  // if (serByte>0 && serByte!=0x1F)
  // {    
  // Serial.print("RX: ");
  // Serial.print(serByte,HEX);
  // Serial.print("\n\r");
  // }
  

  switch (serByte)
  {
  case 0x00:
    break;
  case 0x01:
    Serial.print ("0x01 Ack\n\r"); 
    break;
  case 0x11:
    if (mainDoorStatus==false)
    {
      digitalWrite(led,HIGH);
      Serial.print("0x11 Main door opened\n\r");
      mainDoorStatus=true;
    }
    break;
  case 0x12:
    if (mainDoorStatus==true)
    {
      digitalWrite(led,LOW);
      Serial.print("0x11 Main door closed\n\r");
      mainDoorStatus==false;
    }
    break;
  case 0x13:
    if (standDoor==false)
    {
      digitalWrite(led,HIGH);
      Serial.print("0x13 Stand door opened\n\r");
      standDoor=true;
    }
    break;
  case 0x14:
    if (standDoor==true)
    {
      digitalWrite(led,LOW);
      Serial.print("0x14 Stand door closed\n\r");
      standDoor=false;
    }
    break;
  case 0x19:
    if (cashDoor==false)
    {
      digitalWrite(led,HIGH);
      Serial.print("0x19 Cashbox door opened\n\r");
      cashDoor=true;
    }
    break;
  case 0x1A:
    if (cashDoor==true)
    {
      digitalWrite(led,LOW);
      Serial.print("0x1A Cashbox door closed\n\r");
      cashDoor=false;
    }
    break;
  case 0x1B:
    if (cashBox==false)
    {
      digitalWrite(led,HIGH);
      Serial.print("0x1B  Cashbox was removed\n\r");
      cashBox=true;
    }
    break;
  case 0x1C:
    if (cashBox==true)
    {
      digitalWrite(led,LOW);
      Serial.print("0x1C  Cashbox was installed\n\r");
      cashBox=false;
    }  
    break;
  case 0x1F:
    break;
  case 0x20:
    Serial.print("0x20  General tilt\r\n");
    break;
  case 0x26:
    Serial.print("0x26  Player initiated cash out\r\n");

  case 0x3D:
    Serial.print("0x3D  A cash out ticket has been printed\r\n");
    getTicketInfo(0);
    break;
  case 0x3F:
    Serial.print("0x3F  Validation ID not configured.\r\n");
    break;
  case 0x51:
    handPay(0);
    break;  
  case 0x57:
    Serial.print("0x57 System Validation requested\n\r"); 
    printTicket(0);   
    break;
  case 0x66:
    Serial.print("0x66 Cash out button pressed\n\r");
  case 0x67:    
    Serial.print("0x67 Ticket Inserted.\n\r");
    redeemTicket(0);
    break;
  case 0x68:
    Serial.print ("0x68  Ticket transfer complete\n\r");
    transferComplete(0);
    break;
  case 0x71:
    if (changeLight==false)
    {
      Serial.print("0x71 Change light on.\n\r");
      digitalWrite(led,HIGH);
      changeLight=true;
    }
    break;
  case 0x72:
    if (changeLight==true)
    {
      Serial.print("0x72 Change light off.\n\r");
      digitalWrite(led,LOW);
      changeLight=false;     
      sendCommand(bonus,9);
      delay(10);
      sendCommand(curCredits,2);
      for (int i=0; i<9; i++)
      {
       serByte=Serial1.read(); 
      }
      
    }
    break;
  case 0x7C:
    Serial.print("0x7C Legacy bounus pay has occured\n\r");
    break;
  default:
    break;
  }
  delay(30);
}

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();   
  }
  Serial.print("0x57 Handpay is pending\n\r");
}

void getTicketInfo(int dummy)
{ 
  int i;
  byte getVal[5]={
    0x01, 0x4D, 0x00, 0xC2, 0xAC                              };
  sendCommand(getVal,5);
  delay(50);
  for (i=0; i<Serial1.available(); i++)
  {
    getVal[0]=Serial1.read(); // read the data and discard it. 
  }    
}

void printTicket(int dummy)
{
  int i;
  byte sysVal[13];
  sysVal[0]=0x01;
  sysVal[1]=0x57;
  sendCommand(sysVal,2);
  delay(50);
  for (i=0; i<10;i++)
  {
    sysVal[i]=Serial1.read();
  }      
  sysVal[1]=0x58;
  sysVal[2]=0x01;
  for (i=10; i>5; i--)
  {
    sysVal[i]=sysVal[i-3];
  }
  for (i=3;i<6;i++)
  {
    sysVal[i]=0x00;
  }
  computeCRC(sysVal,11);

  sysVal[11]=globalCRC[0];
  sysVal[12]=globalCRC[1];
  Serial.print("Printed cashout ticket.\n\r");
  sendCommand(sysVal,13);
  delay(20);
  for (i=0; i<5; i++)
  {
    sysVal[i]=Serial1.read();
  }
}

void redeemTicket(int dummy)
{
  int i;
  byte ticket[21];
  ticket[0]=0x01;
  ticket[1]=0x70;
  Serial.print("0x67 Ticket Inserted.\n\r");
  Serial.print("     Ticket redeemed.\n\r");
  sendCommand(ticket,2);
  delay(50);
  for (i=0;i<21; i++)
  {
    ticket[i]=Serial1.read();     
  }
  Serial.print("\n\r");
  ticket[1]=0x71;
  for (i=4; i<9; i++)
  {
    ticket[i]=ticket[i+10]; 
  }    
  computeCRC(ticket,19);
  ticket[19]=globalCRC[0];
  ticket[20]=globalCRC[1];
  sendCommand(ticket,21);   
  delay(50);
  for (i=0; i<21; i++)
  {
    ticket[i]=Serial1.read();
  }  
}

void transferComplete(int dummy)
{
  byte transComplete[6]={
    0x01, 0x71, 0x01, 0xFF, 0x1F, 0xD0                              };
  sendCommand(transComplete,6);
  delay(20);
  for (int i=0; i<21;i++)
  {
    transComplete[0]=Serial1.read();
  }
}

void sendCommand(byte temp[], int len)
{
  UCSR1B = 0b10011101; 
  Serial1.write(temp[0]);
  delay(1);
  UCSR1B = 0b10011100;
  for (int i=1; i<len;i++)
  {
    Serial1.write(temp[i]);
    delay(1);
  }
}

void computeCRC(byte val[], int len)
{
  globalCRC[0]=0;
  globalCRC[1]=0;
  long crc;
  long q;
  byte c;
  crc = 0;
  for (int i = 0; i < len; i++)
  {
    c = val[i];
    q = (crc ^ c) & 0x0f;
    crc = (crc >> 4) ^ (q * 0x1081);
    q = (crc ^ (c >> 4)) & 0xf;
    crc = (crc >> 4) ^ (q * 0x1081);
  }
  globalCRC[0]=crc & 0xff;
  globalCRC[1]=crc >> 8;

}  // End of ComputeCRC

I have a sketch that only does I/O to Serial1 on the Leonardo and one LED

How are you doing serial IO to the LED?

PaulS:

I have a sketch that only does I/O to Serial1 on the Leonardo and one LED

How are you doing serial IO to the LED?

I am doing I/O to Serial1 and a LED (output only and not serial)

  for (i=0;i<21; i++)

{
    ticket[i]=Serial1.read();   
  }

Perhaps you should check if serial data is available first? 21 bytes of it in this case.

I said this before I know exactly how many bytes the device will send at all times if it has nothing to respond with when I do the poll near the beginning of loop it responds with 0 or 1F if needs the arduino (a host or server) to handle an event it will send a single byte if that event requires more data from the device it will send a specific poll for the data then the data is either processed or discarded (some of the data I do not care about) But that data is always a set number of bytes (per the docs I am referencing to code this) if I need to deal with a unknown number of bytes then I will code for it those events that cause it.

Like I said the both work as long as the Leonardo see's a PC connected!! unplug PC and use a USB power adapter or AC adapter connected to the power jack. I expected the PC one to fail no serial connected, but the no PC version also fails it has to be the Serial one interrupts are changing something when PC is connected via USB and not connected.

I just copied the PC version over the none PC version, then edited it to eliminate Serial.begin(19200); and every Serail.print("......"); I triple checked the code by using find Serial. it functions correctly as long I use the USB port to power it, yes it even drives a Max232, device serial is RS232 and yes both the USB pwer adapter and the AC adapter have enough current to drive Leonardo and the Max232 but when I try to cause one of the events on the device the only code that seems to function is the digitalWrite portions.

and the non pc version fails to handle events but I reconnect the pc and it does just fine.

Think about it: Serial output can affect the time it takes to complete portions of your code. I have seen this mask timing bugs like the one Nick points out above on many occasions. When the output is turned off, the bug appears.

Even if you know there are 24 chars in the packet, unless you check to see whether all 24 have arrived when you read them, you run the risk of stuffing your read buffer full of "-1" as you read past the end of what has actually arrived so far.

Could this explain the results you are seeing?

-br

I do not think so it takes a bit over 10ms to get about 20 bytes at 19200 the device is required to respond to poll within a 20ms time frame which it does on every poll. the 1 ms delay I used are for inter byte delay prevent reading a byte before it is there. so a 20 byte read takes 40-50ms

After 20ms I am allowed to poll another device if I had one connected.

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.

I changed the one with the serial.available() in the for statement
For some reason the device likes the delays

For some reason the Leonardo didn’t like something
it kept disappearing in device manager or the IDE when trying to open the serial monitor and even tried putty to see if it was arduino IDE
both said the COM4 was in use by another program.

I tried this

while (Serial1.available()==0)
{
  i=0;
}

I guess I could try this,

i=0
while (i<21)
{
  if (Serial1.available>0)
  {
     serData[I]=Serial1.read();
     i++;
  }
}

fdyche: For some reason the device likes the delays better than trying this to stall the reads until the data arrives

You say "stall until data arrives" as if it were some sort of bad thing. What else are delays doing, except in a more mindless way?

Your second attempt above, although riddled with syntax and other problems, is closer to what I had in mind.

I had something strange happen while fixing the code
the Leonardo after I had the Arduino IDE compile and upload the pc com4 disappeared from device manager.
it didn’t like all my case statements or the multiple Serial.print lines so I trimmed them out what I figured was important.
where is compiler placing all text lines do they become constant character arrays or what?

The PC version works only when connected to PC which I expect
I tried it using a USB power adapter and 12V adapter to the DC input jack (not the headers)
The no pc version fails as well unless it is connected to the PC

int led = 13;
int len;
boolean mainDoorStatus;
boolean cashDoor;
boolean cashBox;
boolean standDoor;
boolean changeLight;

byte globalCRC[2];

void setup()
{
  Serial1.begin(19200);
  pinMode(led, OUTPUT);
}

void loop()
{
  int i;    
  byte serByte;
  byte bArray[50];
  int bCount;
  byte PC;
  byte lastPoll;
  byte legacyBonus[9]={
    0x01, 0x8A, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0F, 0x8A  };
  byte gDisable[4]={
    0x01, 0x01, 0x51, 0x08                        };
  byte gEnable[4]={
    0x01, 0x02, 0xCA, 0x3A                      };

  UCSR1C = 0b00000110;
  UCSR1B = 0b10011101;
  Serial1.write(0x80);
  delay(1);
  UCSR1B = 0b10011101;  
  Serial1.write(0x81); 
  delay(1);
  UCSR1B = 0b10011100;
  delay(10);

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

  if (serByte>0)
  {
    if (serByte != lastPoll)
    {
      lastPoll = serByte;
    }
    else serByte=0;
  }  

  switch (serByte)
  {
  case 0x11:
    if (mainDoorStatus==false)
    {
      digitalWrite(led,HIGH);
      mainDoorStatus=true;
    }
    break;
  case 0x12:
    if (mainDoorStatus==true)
    {
      digitalWrite(led,LOW);
      mainDoorStatus==false;
    }
    break;
  case 0x13:
    if (standDoor==false)
    {
      digitalWrite(led,HIGH);
      standDoor=true;
    }
    break;
  case 0x14:
    if (standDoor==true)
    {
      digitalWrite(led,LOW);
      standDoor=false;
    }
    break;
  case 0x19:
    if (cashDoor==false)
    {
      digitalWrite(led,HIGH);
      cashDoor=true;
    }
    break;
  case 0x1A:
    if (cashDoor==true)
    {
      digitalWrite(led,LOW);
      cashDoor=false;
    }
    break;
  case 0x1B:
    if (cashBox==false)
    {
      digitalWrite(led,HIGH);
      cashBox=true;
    }
    break;
  case 0x1C:
    if (cashBox==true)
    {
      digitalWrite(led,LOW);
      cashBox=false;
    }  
    break;
  case 0x1F:
    break;
  case 0x3D:
    getTicketInfo(0);
    break;
  case 0x51:
    handPay(0);
    break; 
  case 0x57:
    printTicket(0);
    break;
  case 0x67:    
    redeemTicket(0);
    break;
  case 0x68:
    transferComplete(0);
    break;
  case 0x71:
    if (changeLight==false)
    {
      digitalWrite(led,HIGH);
      changeLight=true;
    }
    break;
  case 0x72:
    if (changeLight==true)
    {
      digitalWrite(led,LOW);
      changeLight=false;     
      sendCommand(legacyBonus,9);      
    }
    break;
  case 0x7C:
    break;  
  default:    
    break;
  }
  delay(80);
}

void handPay(int du)
{
  int i;
  byte hp[24];
  hp[0]=0x01;
  hp[1]=0x1B;
  sendCommand(hp,2);
  i = 0;  
  while (i<24)
  {
    if (Serial1.available()>0)
    {
      hp[i]=Serial1.read();
      i++;
    }
  }
}

void getTicketInfo(int dummy)
{ 
  int i;
  int nByte;
  byte reByte;
  byte getVal[5]={
    0x01, 0x4D, 0x00, 0xC2, 0xAC                                  };
  sendCommand(getVal,5);
  i=0;
  
  while (i<35)
  {
    if (Serial1.available()>0)
    {
      reByte=Serial1.read();
      i++;
    }
  }    
}

void printTicket(int dummy)
{
  int i;
  byte sysVal[13];
  sysVal[0]=0x01;
  sysVal[1]=0x57;
  sendCommand(sysVal,2);
  i = 0;
  while (i<9)
  {
    if (Serial1.available()>0)
    {
      sysVal[i]=Serial1.read();
      i++;
    }
  }      
  sysVal[1]=0x58;
  sysVal[2]=0x01;
  for (i=10; i>5; i--)
  {
    sysVal[i]=sysVal[i-3];
  }
  for (i=3;i<6;i++)
  {
    sysVal[i]=0x00;
  }
  computeCRC(sysVal,11);

  sysVal[11]=globalCRC[0];
  sysVal[12]=globalCRC[1];
  sendCommand(sysVal,13);
  i=0;
  while (i<5)
  {
    if (Serial1.available()>0)
    {     
      sysVal[i]=Serial1.read();
      i++;
    }
  }
}

void redeemTicket(int dummy)
{
  int i;
  byte ticket[21];
  ticket[0]=0x01;
  ticket[1]=0x70;
  sendCommand(ticket,2);
  i = 0;
  while (i<21)
  {
    if (Serial1.available()>0)
    {
      ticket[i]=Serial1.read();
      i++;
    }
  }
  ticket[1]=0x71;
  for (i=4; i<9; i++)
  {
    ticket[i]=ticket[i+10]; 
  }    
  computeCRC(ticket,19);
  ticket[19]=globalCRC[0];
  ticket[20]=globalCRC[1];
  sendCommand(ticket,21);   
  i=0;
  while (i<21)
  {
    if (Serial1.available()>0)
    {
      ticket[i]=Serial1.read();
      i++;
    }
  }

  byte transComplete[6]={
    0x01, 0x71, 0x01, 0xFF, 0x1F, 0xD0                                  };
  sendCommand(transComplete,6);
  i=0;
  while (i<21)
  {
    if (Serial1.available()>0)
    {
      transComplete[0]=Serial1.read();
      i++;
    }
  }  
}

void transferComplete(int dummy)
{
  int i;
  byte transComplete[6]={
    0x01, 0x71, 0x01, 0xFF, 0x1F, 0xD0                                  };
  sendCommand(transComplete,6);
  i=0;
  while (i<21)
  {
    if (Serial1.available()>0)
    {
      transComplete[0]=Serial1.read();
      i++;
    }
  }  
}

void sendCommand(byte temp[], int len)
{
  UCSR1B = 0b10011101; 
  Serial1.write(temp[0]);
  delay(1);
  UCSR1B = 0b10011100;

  for (int i=1; i<len;i++)
  {
    Serial1.write(temp[i]);
    delay(1);
  }
}

void computeCRC(byte val[], int len)
{
  globalCRC[0]=0;
  globalCRC[1]=0;
  long crc;
  long q;
  byte c;
  crc = 0;
  for (int i = 0; i < len; i++)
  {
    c = val[i];
    q = (crc ^ c) & 0x0f;
    crc = (crc >> 4) ^ (q * 0x1081);
    q = (crc ^ (c >> 4)) & 0xf;
    crc = (crc >> 4) ^ (q * 0x1081);
  }
  globalCRC[0]=crc & 0xff;
  globalCRC[1]=crc >> 8;

}

Do not know what I did but I got the thing to finally work as standalone (no PC connected)

it was tweaking this and that with the help on here and another forum that fixed it but I learned a thing or two as well. Now to apply this to a version that I coded for Windows with .NET in C#.

now if could fix SerialPort class in .NET I would be happy.

Not sure how to mark this solved.

Not sure how to mark this solved.

Modify your first post. Change the Subject line, to include something like "[Solved]".