Serial problems

Hi, I am using my arduino diecimila with an rfid reader (Innovations ID-12) and I'm having issues printing to Serial. Basically it works when I have a delay statement, but when I take out the delay statement it just prints garbage to the screen.

Thanks in advance for any help!

char *getTag()
{
char val = 0;
char IDstring[13];
int i;
if (Serial.available() > 0 )
{
delay(20); //When This line is commented out, it stops working!!!
val = Serial.read();
if ( val == 02 )
{ // look for Start Of Text marker
for ( i = 0; (val = Serial.read()) != 03 ; i++)
{
IDstring = val;

  • }*

  • Serial.print(" IDString["); IDstring[10] = 0x00; // tie off IDstring at the CR-LF*

  • Serial.print(IDstring);*

  • Serial.println("] ");*

  • resetID12(); // reset after a valid read*

  • }*

  • }*
    return IDstring;
    }

Try this code

http://www.arduino.cc/playground/Learning/PRFID

It works, but not with Software Serial (only hardware / rxtx)

There were two examples on that page. The first one looked pretty much like my code, except the delay was at the end of the function instead of inbetween the Serial.available() and Serial.read(). I'm not sure why they want to pause the program for a whole second at the end for no apparent reason, other than they were having the same issue that I was. The second example used softwareserial.h, which I do not want to use due to it's limitations. Does anybody know why I have to include this delay in order for my code to work??? Is there some other way of doing this that doesn't use the delay?

Thanks, Dan

Hi Dan, neither of these may relate to the delay but I can see two problems with the code. It is a function returning a pointer to char array that is on the stack, so it may not be valid when the function returns. You can add the static keyword: static char IDstring[13]; (or move the variable declaration out of the function)

Also the code will return a pointer to the string even if no ID is available.

There were two examples on that page. The first one looked pretty much like my code, except the delay was at the end of the function instead of inbetween the Serial.available() and Serial.read(). I'm not sure why they want to pause the program for a whole second at the end for no apparent reason, other than they were having the same issue that I was. The second example used softwareserial.h, which I do not want to use due to it's limitations. Does anybody know why I have to include this delay in order for my code to work??? Is there some other way of doing this that doesn't use the delay?

Thanks, Dan

I've tested the code and it works fine without a delay. I didn't notice a second sketch had been added, software serial will hang the program which isn't a problem if your Arduino's only purpose is to wait for an RFID reader to respond.