Arduino Mega with RS323 RFID reader

Hi all,

Thanks in advance for taking the time to read this and helping :slight_smile:

The issue right now is I have a RFID reader, it transmit its data via a RS323 Serial connector. When I hook it up to the computer, every time it reads a tag it shows the tag id in a new line in hyper-terminal like this:

FFFF00000000438A

16 characters long.

So now I plug this into a RS232 to TTL converter board, it converts the RS323 connector into a simple RX and TX format that is suitable for the Arduino Mega. I then wire the TX into the Mega’s TX and RX (either Serial1,2 or 3).

My goal is to store the RFID as a string for further processing, but I am picking up absolutely nothing.

Here is my code (in this case its plugged into the Serial2 on the Mega):

int inByte= -1; // incoming byte from serial RX
char buffer[25]; // string for incoming serial data – a buffe
int bufferPos = 0; // string index counter

void setup()
{
Serial.begin(9600); //USB
Serial1.begin(9600);
Serial2.begin(9600); //RFID
Serial3.begin(9600);

Serial.write(“RFID Test”);
}

void loop() {
if (Serial2.available() > 0 ) {

//inByte = Serial2.read();
while (bufferPos != 20) {
if (Serial2.available() > 0) {
buffer[bufferPos] = inByte;
bufferPos++;
inByte = Serial2.read();
}
}

for (int i = 0; i<bufferPos; i++){
Serial.print(buffer*);*

  • }*

  • Serial.print("\n\r");*

  • for (int c = 0; c < bufferPos; c++) {*

  • buffer*
    ```c

  • = 0;
    }
    bufferPos = 0;
    }
    }

[/quote]

A picture of the setup:

*

```

You are not waiting for data to become available with serial available before reading it.

Oh ops, I had that but I was troubleshooting the board. Thank you for your fast reply.

Before I had something like this, and the above code within the loop:

if (Serial2.available()> 0) {}

And nothing came up.

I also tried to just have this within the loop

int RFID = Serial2.read();
Serial.print(RFID);

And it keeps giving me back "-1" whether or not the RFID is reading anything.

I am not too familiar with C so I am not sure if the data is not coming through, or I am coding it wrong.

Last but not least, I've tried this method:

if (Serial2.available > 0) {
Serial.print("received 2");
}

And I end up starring at a blank screen.

I have made sure the RX and TX pin is receiving and sending signals.

So now I plug this into a RS232 to TTL converter board, it converts the RS323 connector into a simple RX and TX format that is suitable for the Arduino Mega. I then wire the TX into the Mega's TX and RX (either Serial1,2 or 3).

Did you remember to connect a common ground? Are you connecting the TX from the converter board to the RX on the mega?

Quote:
if (Serial2.available()> 0) {}

And nothing came up.

Correct nothing will come up because as long as you have something available you get held in the null braces.

And it keeps giving me back "-1" whether or not the RFID is reading anything.

It will do because you are not waiting.

That last one is the one you want:-

if (Serial2.available > 0) {
 Serial.print("received 2");
}

If you are not getting anything then that means you are not sending anything into the mega. Try disconnecting the input and just taping a wire to ground on the input. That normally provokes things into action. Of course with the code above once you get a character in the buffer it will continuously print the string

This is my code for a rfid reader ID20.

void check_rfid_serial()    
{
   if (Serial1.available() > 0 ) 
  {
    char val = 0 ; int i = 0; 
    byte tmp =0;
    do 
    if (Serial1.available() > 0 ) 
    {
    val = Serial1.read();
    if ( val != 2 )
    {
    IDstring[i]=val;  
    i++ ;
    }  
    }
    while ( val != 3 );

if ( val == 3 )
{
    IDstring[12]=0;

etc 
etc

}

Thanks everyone for the help and input, I’ve looked at all the things you mentioned and both test/incorporated them into my code. I will try the “tap a wire from ground into RX pin” trick later today.

As of right now I am still not receiving anything.

The LED for the output pin, when I print to Serial2 flashes really bright, but the input LED for signal comes into Serial2 flashes really dim, both uses 470 ohms resistors… would that be a sign there is something wrong with the RS323 board?

Also, I don’t think flipping Rx with Tx does anything since the board already flips the Rx and Tx from the RS323 connector, to be thorough I did it as well but nothing is coming through.

Here is a pic of the setup

New code

int inByte= -1; // incoming byte from serial RX
char buffer[25]; // string for incoming serial data – a buffe
int bufferPos = 0; // string index counter

void setup()
{
Serial.begin(9600); //USB
Serial1.begin(9600);
Serial2.begin(9600); //RFID
Serial3.begin(9600);

Serial.write(“RFID Test”);
}

void loop() {
if (Serial2.available() > 0 ) {
inByte = Serial2.read();
while (bufferPos != 20) {
if (Serial2.available() > 0) {
buffer[bufferPos] = inByte;
bufferPos++;
inByte = Serial.read();
}
}

for (int i = 0; i<bufferPos; i++){
Serial.print(buffer*);*

  • }*
  • } *
    }
    [/quote]

Just to check you are seeing the introductory message in the serial monitor?

Just some points about the code:-

  1. You are not resting the value of bufferPos after the first read.
  2. You are storing inByte before you have read it from the buffer
  3. inByte = Serial.read(); should be inByte = Serial2.read();

So you are not looking for the CR/LF or a stop byte, you are just reading from the serial for 20 bytes ?.

What if you get a miss read and only get a part of the rfid code, then on the next pass you get the full rfid tag.

To me , you want to look for a end from the rfid tag instead of just buffering a number of bytes .

Are we looking at the same code? The code in reply #6 has none of the issues you mention.

Grumpy Mike

Thank you Mike for your constant support! Yes, the introductory message shows up fine, but that’s the only thing it shows. I’ve tested the code by reading from Serial0 via Xbee and it grabs the data and puts it into a string just fine.

  1. Thanks for spotting that error, I was playing with the same bit of code transferring values between 2 Xbees and found out that issue.
  2. Again, mistake on my bit.
  3. Again x2, an oversight :stuck_out_tongue:

I have went through and fixed the code now, should have no problem with bufferPos, reading before buffer, or reading from wrong serial.

Peter

That is true. However that is not my concern right now as I am not getting a single reading from the RFID reader :frowning:

PaulS

Sorry, reply #6 is the new code I wrote. However the same error still remains, I am getting no reading from the RFID reader (which I know is working properly because it works fine plugged into my PC com port).

New code

int inByte= -1; // incoming byte from serial RX
char buffer[25]; // string for incoming serial data – a buffe
int bufferPos = 0; // string index counter

void setup()
{
Serial.begin(9600); //USB
Serial1.begin(9600);
Serial2.begin(9600); //RFID
Serial3.begin(9600);

Serial.write(“RFID Test”);
}

void loop() {
if (Serial2.available() > 0 ) {

//inByte = Serial2.read();
while (bufferPos != 20) {
if (Serial2.available() > 0) {
buffer[bufferPos] = inByte;
bufferPos++;
inByte = Serial2.read();
}
}

for (int i = 0; i<bufferPos; i++){
Serial.print(buffer*);*

  • }*

  • Serial.print("\n\r");*

  • for (int c = 0; c < bufferPos; c++) {*

  • buffer*
    ```c

  • = 0;
       }
       bufferPos = 0;    
      }  
    }

[/quote]*

```

Sorry paulS , but what !!!! I can see all the point Mike as said are true .

why 20 byte.

you may have to send the rfid tag twice to full up the 20 byte buffer.

What is the rfid reader you are using ?

Peter, thanks for helping me with this :slight_smile:

The RFID I am using is the Omni Directional one in this pdf below:

http://www.rfidinc.com/pdfs/Extend-A-Read433MHz.pdf

The next bit might sound very noobish.

The data comes in this format:

16charRFID

Therefore I figure if I just grab 20 byte for testing now, the data I grab will be partially within the RFID#.

= 4 byte…? :-[

I can`t see any doc telling you the format from the reader .

But if it is 16charRFID

Which is 18 bytes , can`t you do a DO , WHILE rfid_char != cr

eg loop until the char is not CR and end the loop

CR and LF are 1 byte each

Line Feed = 10 and carriage return = 13 ( DEC )

void check_rfid_serial()    
{
   if (Serial1.available() > 0 ) 
  {
    char val = 0 ; int i = 0; 
    byte tmp =0;
    do 
    if (Serial1.available() > 0 ) 
    {
    val = Serial1.read();
    if ( val != 10 ) // miss the LF
    {
    IDstring[i]=val;  
    i++ ;
    }  
    }
    while ( val != 13 ); // loop until the CR

if ( val == 13 )
{

// I most have got a complete rfid tag work on it

}
}

Thanks Peter, I will put that into my code. Still trying to get the damn thing to read from the reader though, I am getting a feeling it might be the RS323 interface board and its chip that is malfunctioning.

I am going to change my while loop to “!= cr” which I think does exactly what you are doing.

I’m also going to read through your code to understand it better, maybe I can streamline mine some more.

Here is the bit from the datasheet that comes with the reader:

Tag reads are reported to the serial port in the following format whereby the first 4 characters are set as the customer code and the following 12 are unique Tag data:

FFFF123456789ABC

That’s Line Feed 16 ASCII data characters, Carriage Return.

However if using the RS485 addressable Readers, the Reader address will precede the Tag string as such assuming a Reader address of 00:

00FFFF123456789ABC

I am getting a feeling it might be the RS323 interface board

What are you doing converting the RS232 down to TTL levels ?

Hope are not feeding in the RS232 direct.

Hi Peter,

The Serial interface board uses a ICL3232 CP chip to conver the RS232 down to TTL.

The data sheet for the chip is right here:

http://www.intersil.com/data/fn/fn4805.pdf

One more question, on the coding if you dont mind.

Can I just write this in my code?

while ( inByte! = cr ) {}

The arduino tells me cr is undefined, is there a specific way to tell it to stop reading what is in the buffer once it reaches the carriage return?

Thanks again

My last bit of code was set up for your example.

 do
    if (Serial1.available() > 0 )
    {
    val = Serial1.read();
    if ( val != 10 ) // miss the LF
    {
    IDstring[i]=val;  
    i++ ;
    }  
    }
    while ( val != 13 ); // loop until the CR

if ( val != 10 ) // miss the LF

What my code does is ignore LR ( 10 DEC ) and put in the buffer any other chars.

do
{

}
while ( val != 13 ); // loop until the CR[/code]

this keeps reading the chars until it comes around a CR

No you can use CR / LF direct, you need to use

#define LF 10
#define CR 13

then you can use :- while ( inByte != CR ) {}

Thank you Peter! That explains it very clearly.

I will give that a shot tomorrow when I get my parts back. Hopefully the replacement RS232 board comes in as well.