read RFID tag into single line

How to read the RFID tag number into a single line
i tried

int i;
void setup()
{
Serial.begin(9600); // start serial to PC 
Serial1.begin(9600);

}
void loop()
{
 if (Serial1.available())
 { 
   while(Serial1.available()){
      i = Serial1.read();}
   Serial.println(i);
 }
}

it gives in separate lines.

Maybe change Serial.println(i); to Serial.print(i);

But how can i get it to the next line at the second reading

You add a Serial.println("") after the while loop but within the scope of the if.

It doesn't work.

Andre_Leonardo:
It doesn't work.

What doesn't work. That is a most useless answer. Have you read this?
How to use this forum

What you need to do is to post your code with the changes you made. You probably didn't do what I said.

Andre_Leonardo:
It doesn't work.

Maybe you need some Viagra.

Andre_Leonardo:
It doesn't work.

Post the code you changed with Grumpy_Mike's addition and explain why it does not work and/or include example output.

These are the codes i tried and their respective outputs.

int i;
void setup()
{
Serial.begin(9600); // start serial to PC 
Serial1.begin(9600);

}
void loop()
{
 if (Serial1.available())
 { 
   while(Serial1.available())
   {
      i = Serial1.read();
      
      }
   Serial.print(i);
   Serial.println("");
 }
}

Output:

0
105
52
167

Second code

byte i;
String msg;
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop()
{
  if (Serial1.available())
  {
  while(Serial1.available())
  {
    i=Serial1.read();
    
    msg += i;  
    }
  }
  if(msg.length()==10){  
    Serial.println(msg.length());
    Serial.println(msg);
    msg="";
    } 
}

Output
10
0105521670

This prints only once. For the second time tag number doesn't get printed.

Third code

byte i;
String msg;
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop()
{
  if (Serial1.available())
  {
  while(Serial1.available())
  {
    i=Serial1.read();
    
    msg += i;  
    }
  }
  if(msg.length()>=10)
  { 
    if(msg.length()==10){
    Serial.println(msg);   
    msg="";
    }
    if(msg.length()==11)
    {
      msg="";
      }
      if(msg.length()==12)
      {
        msg="";
        }
    } 
}

Output:

0105521670
0105521670

Issue with this output is that this number 0105521670 is not the number printed in the tag. I have to convert the numbers into hex 3 by 3 and convert it to decimal to take the correct tag number.
Secondly after the first reading it takes 3 or four reading to display the number again.

So what is the solutions for these issues for not giving the correct tag number and printing and converting it correctly

Like I said, you didn't do what I told you for the first one. You just put the two print statements next to each other.

However your overall problem is that you are processing the data way faster than it is comming in. So you get one character in the serial buffer, enter the while Avaliable loop, read one character then the buffer is empty so you fall out of the initial if and the loop function gets called again.

Soloution - how many bytes are you expecting? The first if should only be true if you have that number or greater bytes in the buffer.

Same results for this code also, Separation using println("") doesn't work.

int i;
void setup()
{
Serial.begin(9600); // start serial to PC 
Serial1.begin(9600);

}
void loop()
{
 if (Serial1.available())
 { 
   while(Serial1.available())
   {
      i = Serial1.read();
      Serial.print(i);
      }
   
   Serial.println("");
 }
}

Output:

0
104
248
112

And readings from Wiegand library gives correct outputs.

Wiegand HEX = 68F870, DECIMAL = 6879344,
Wiegand HEX = 6934A7, DECIMAL = 6894759,
Wiegand HEX = 619AE1, DECIMAL = 6396641,
Wiegand HEX = 6C3A5F, DECIMAL = 7092831
Wiegand HEX = 6BF284, DECIMAL = 7074436
Wiegand HEX = 65D376, DECIMAL = 6673270,

Now main issue is what is the relationship between these Hex value and the decimal. Normal Hexa to decimal conversion doesn't give the correct value.

Same results for this code also, Separation using println("") doesn't work.

That is because you have not addressed what I said in the last post about you processing data too fast. A simple but not recommended solution is to put in a delay so that there is time for the next byte to arrive:-

int i;
void setup()
{
Serial.begin(9600); // start serial to PC
Serial1.begin(9600);

}
void loop()
{
 if (Serial1.available())
 {
   while(Serial1.available())
   {
      i = Serial1.read();
      Serial.print(i);
      delay(3);
      }
  
   Serial.println("");
 }
}

Now main issue is what is the relationship between these Hex value and the decimal.

This is correct:-
Wiegand HEX = 68F870, DECIMAL = 6879344

So what is incorrect?

1 Like

Untested but does this help?

uint32_t serialNo = 0;

void setup(){
  Serial.begin(9600); // start serial to PC
  Serial1.begin(9600);
}

void loop(){
  if (Serial1.available() > 3){
    serialNo = 0;
    for (uint8_t x = 0; x < 4; x++){
      serialNo = serialNo << 8;
      serialNo = serialNo | Serial1.read();
    }
    Serial.println(serialNo,HEX);
  }
}

Output:

0
104
248
112
is in terms of hex:-
0
68
F7
70
put it into variables:-
v1 = 0x0
v2 = 0x68
v3 = 0xF7
v4 = 0x70

So to convert that into a number it is:-
tag = (v1 << 24) | (v2 << 16) | (v3<< 8 ) | v4
That can be printed out as a decimal or hex value

Which is what that code above does on the fly.

Grumpy_Mike:
This is correct:-
Wiegand HEX = 68F870, DECIMAL = 6879344

So what is incorrect?

Normal Hex to decimal conversion doesn't give the 6879344 decimal output. If these numbers are considered only as Dwords, conversion matches. So how do i read or set these Hex values as Dwords and convert to Dword decimal.

I require this conversion because tag numbers appear in Arduino is as decimal (tag output 0104248112 = hex 68F870).

Grumpy_Mike:
Output:

0
104
248
112
is in terms of hex:-
0
68
F7
70
put it into variables:-
v1 = 0x0
v2 = 0x68
v3 = 0xF7
v4 = 0x70

So to convert that into a number it is:-
tag = (v1 << 24) | (v2 << 16) | (v3<< 8 ) | v4
That can be printed out as a decimal or hex value

Which is what that code above does on the fly.

thanx i'll try this out.

Basically you do not convert a number from decimal to hex.

Numbers are stored in the computer as variables in binary. There is only the concept of conversion when you print out or otherwise display this binary bit pattern.

So you can not CONVERT a number into decimal or into hex, you can only display it as such.

Normal Hex to decimal conversion doesn't give the 6879344 decimal output.

What do you consider "normal", please post the code where you are failing to display the number correctly.

Edit, missed that last post before I wrote this, it was on a page boundary and I didn't spot it.

Grumpy_Mike:
Output:

put it into variables:-
v1 = 0x0
v2 = 0x68
v3 = 0xF7
v4 = 0x70

So to convert that into a number it is:-
tag = (v1 << 24) | (v2 << 16) | (v3<< 8 ) | v4
That can be printed out as a decimal or hex value

Which is what that code above does on the fly.

Can u explain how to put it into variables as v1=0x0. I tried to use the strtoul but it was not successful. And the rest of code how does it work.

Can u explain how to put it into variables as v1=0x0.

That is just an illustration, simply use the variables you have received.

And the rest of code how does it work.

It simply ORs together ( the | symbol ) each of the four bytes you receive. Each byte is shifted into the right place in the 32 bit filed by using the << ( shift left ) operator.

I tried it the way i understand but it was not successful, can u explain what i have done wrong.

byte i;
String msg;
byte msg1;
void setup()
{
Serial.begin(9600); // start serial to PC
Serial1.begin(9600);

}
void loop()
{
 if (Serial1.available())
 {
   while(Serial1.available())
   {
    msg1 =(Serial1.read(),HEX);
    convert(msg1);
      delay(3);
      }
 }
}

void convert(byte catchv)
{
  Serial.println(catchv);
  byte j[3];
  for (int m=0;m<4;m++)
{
  j[m]=catchv;
}

byte tag = (j[0] << 24) | (j[1] << 16) | (j[2]<< 8 ) | j[3];
  Serial.print(tag);
}