Show Posts
Pages: [1] 2
1  Using Arduino / Displays / Sparkfun 16x2 LCD and wire length limit? on: March 08, 2013, 08:51:53 pm
I am using a Arduino Leonardo and sparkfun 16x2 LCD parallel version with the hd44780 controller
would it be safe to go about 6 feet?
2  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 18, 2012, 02:35:11 am
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.


3  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 16, 2012, 09:51:00 pm
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

Code:
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;

}


4  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 16, 2012, 05:15:22 am
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
Code:
while (Serial1.available()==0)
{
  i=0;
}

I guess I could try this,

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

  
















5  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 15, 2012, 08:25:11 am
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.







6  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 15, 2012, 06:52:46 am
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.
7  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 15, 2012, 05:12:04 am
Quote
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)
 


8  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 15, 2012, 04:50:00 am
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

Code:
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

9  Using Arduino / Programming Questions / Re: USB Serial on Leonardo is affecting a sketch on: October 15, 2012, 04:19:02 am
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
Code:
// 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

10  Using Arduino / Programming Questions / USB Serial on Leonardo is affecting a sketch on: October 15, 2012, 01:54:16 am
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
 



11  Using Arduino / Programming Questions / Solved: Reading Serial Data into a byte array results in invalid data on: October 11, 2012, 09:19:08 pm
I did not put a long enough delay after the poll.
Working great now.





12  Using Arduino / Programming Questions / Re: Reading Serial Data into a byte array results in invalid data being received on: October 11, 2012, 06:26:02 am
When I typed the code I missed the 1 for serial1
I am getting all 0 values, which I know is not correct either

For the poll that produces that response, the result will be less or equal to 21 bytes, and any other responses for a poll that I using will be less than or equal to 32 bytes.
There might be some polls that do result in more than 32 bytes, no plans on using them at this time.

I do have other polls that have different number of bytes.
I have another poll that returns 12 bytes but it will always return 12 bytes or less,

I will try the your code suggestions, and see what happens but adjust it for Serial1, sorry about the missing the 1 for Serial1 in my original post on this.

I was thinking the invalid data may be do to a timing issue from the arduino reading usart faster than bytes arrive or did I confuse that with the TX of a byte.


13  Using Arduino / Programming Questions / Reading Serial Data into a byte array results in invalid data being received on: October 10, 2012, 10:40:55 pm
Arduino Leonardo - using hardware serial (TX, RX)

The device connected to the Arduino will only TX after it is polled for the most part.
I am porting code from my PC App to the Arduino because PC UART's do not support 9 data bits and faking it with parity bit creates framing errors and duplicate data
I know what the data should look like from my app and other sources.

Also when I do get single byte response from the device it is correct
I use some of the single byte responses to turn on or off an LED connected to pin 13.
 
problem happens when I read multiple bytes into either byte array or the char array.
 
Example data in hex:         01 70 10 00 00 00 00 00 00 00 01 00 00 00 00 01 50 00 00 82 98
Here is what I get instead: BE 71 0 2 0 C6 D5 5C 0 2 2B D5 44 0 0 C6 D5 5C 0 0 12


Code:
Serial1.begin(19200) //from setup loop
// the following is a code snipet from main loop

byte serData[32]; // more than enough to hold 21 bytes
int count;

UCSR1B = 0b10011101; // turn on 9th bit
Serial1.write(0x01);
UCSR1B = 0b10011100; // turn off 9th bit
Serial1.write(0x70);

for (count=0; count<Serial.available(); count++)
{
  serData[count]=Serial1.read();
}

I try the following and I get "error: invalid conversion from 'byte*' to 'char* why when it is in the reference library as an allowed data type for readBytes function.
If I have to I could use a char array as the buffer then move it to a byte array so I can do the processing I want to do.

Code:
Serial1.readBytes(serData,Serial1.available());



I tried reading into a char array and I still get invalid data.
14  Using Arduino / Programming Questions / Re: I am trying to translate the follwing into C++ for the arduino on: October 10, 2012, 04:31:42 am
Probably not.
C doesn't bounds-check array accesses.
Why not simply return a locally-defined "unsigned int"?
Using globals as a return mechanism is prehistoric.

I was trying to return a unsigned int, but not sure what the code would have looked like to make the returned int had the CRC correct.
then use highbyte and lowbyte to place the 2 bytes in the array
15  Using Arduino / Programming Questions / Re: I am trying to translate the follwing into C++ for the arduino on: October 10, 2012, 04:19:37 am
Does this look like it would work then
I cant figure out how to return a byte array since coding it like C# likes as  byte[] functionname (arguemnts)  did not work
so I changed the code to place the result into globalCRC variable instead.
I would like to pass an array of any size but have not figured out how to do that so I will track the length, I need to test it
so I am going to pass it some arrays that have the proper CRC at the end.


Code:

void ComputeCRC(byte val[32] , int len)
{
  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;
Pages: [1] 2