View previous topic :: View next topic |
Author |
Message |
vas57
Joined: 31 Jan 2022 Posts: 29
|
Problem reading data from an RFID module |
Posted: Mon Jan 31, 2022 7:27 am |
|
|
Hello everyone,
I'm trying to use ex_sisr.c to read data (eg UID) from an RFID module that sends me ASCII characters. For example, when you bring a keyfob closer to the reader, it continuously sends the UID and the message OK, like this (seen on Terminal v1.9b):
7EB75F23
OK
7EB75F23
OK
.
.
.
I try to get both UID and the OK message with ex_sisr.c and send them to a buffer [20], but there I only find the OK message. What could be the explanation?
I read the whole forum related to ex_sisr.c but I did not encounter this situation.
My interruption is the same as in the post above and I use PIC18F14K22.
I read the data without interruption, only in the main program and everything worked fine. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Mon Jan 31, 2022 7:36 am |
|
|
Probably line feeds.
How you are reading or displaying the buffer is seeing the line feed after
the hex string, and then reading the OK, and when you look at the data
you are not looking back to the earlier data before the previous line feed.
Down to how your code is parsing the data. |
|
|
vas57
Joined: 31 Jan 2022 Posts: 29
|
|
Posted: Mon Jan 31, 2022 7:59 am |
|
|
Hi,
Thanks for your fast reply.
After buffering the data, I display like this:
putc(buffer[0]);
putc(buffer[1]);
putc(buffer[2]);
.
.
.
.
putc(buffer[19]);
putc(buffer[20]); |
|
|
vas57
Joined: 31 Jan 2022 Posts: 29
|
|
Posted: Mon Jan 31, 2022 8:05 am |
|
|
This is the code (after message[1] and message[2], the module reply with UID and OK):
Code: |
#INT_RDA
void RDA_isr()
{
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
byte bgetc() {
byte c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main()
{
set_tris_a(0xDB);
set_tris_b(0x3F);
set_tris_c(0x14);
while(TRUE)
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
i=0;
while(message1[i] != '\0')
{
putc(message1[i]);
delay_ms(30);
i++;
}
printf("\r");
i=0;j=0;
while(message2[i] != '\0')
{
putc(message2[i]);
delay_ms(30);
i++;
}
printf("\r");
putc(buffer[0]);
putc(buffer[1]);
putc(buffer[2]);
putc(buffer[3]);
putc(buffer[4]);
putc(buffer[5]);
putc(buffer[6]);
putc(buffer[7]);
putc(buffer[8]);
putc(buffer[9]);
putc(buffer[10]);
putc(buffer[11]);
putc(buffer[12]);
putc(buffer[13]);
putc(buffer[14]);
putc(buffer[15]);
}
}
|
Last edited by vas57 on Wed Feb 02, 2022 12:14 am; edited 2 times in total |
|
|
vas57
Joined: 31 Jan 2022 Posts: 29
|
|
Posted: Mon Jan 31, 2022 8:08 am |
|
|
Messages and replies to/from RFID module are in ASCII.
Now it works partially: when you place a keyfob to the module and then power up the module and its serial read circuit, I capture the UID, but if I change the keyfob, I read the same UID. Also, if I don't place any keyfob to the module, power up the circuit and then place a keyfob on the module, I can't read the UID. So the software only reads my UID once, at first.
Where do you think I'm wrong? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 01, 2022 6:23 pm |
|
|
You have posted incomplete code. You receive characters in #int_rda
in the buffer[] array.
But you don't show how you move data from buffer[] to the
message1[] and message2[] arrays. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Feb 01, 2022 6:39 pm |
|
|
Also after you transfer the data, you should zero ('clear') the first buffer, that way you won't have a 'ghost' in the system.... |
|
|
vas57
Joined: 31 Jan 2022 Posts: 29
|
|
Posted: Wed Feb 02, 2022 12:00 am |
|
|
Thanks @temtronic, @PCM programmer,
@PCM programmer: message1[] and message2[] is 2 commands to module, which responds with UID, this is how the module works.
@temtronic: I would like to delete this buffer [16] after each UID reading (in Header File: #Define buffer_size 16), but I do not know how I can do that.
I correct my above code. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Feb 02, 2022 2:16 am |
|
|
Understand that you should not read data from the buffer as you show.
Instead you should be using the bgetc in ex_sisr. Otherwise the buffer
pointers will not be updated. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 02, 2022 2:58 am |
|
|
(Ttelmah, I was typing my reply when you posted. I'm going to post
it anyway).
Here is your basic problem:
You are treating buffer[] as if it's a linear buffer that starts at 0.
But that's not how ex_sisr.c works. It's a circular buffer. This buffer
is used internally by #int_rda. You are not supposed to directly
access the buffer with code. You are supposed to get bytes from the
buffer with bgetc(). |
|
|
vas57
Joined: 31 Jan 2022 Posts: 29
|
|
Posted: Wed Feb 02, 2022 3:08 am |
|
|
I want to read to different keyfob, different UID, but I read for any other keyfob only the first keyfob uid.
I want:
keyfob 1 ---> UID1
keyfob 2 ---> UID2
keyfob 3 ---> UID3
.
.
.
keyfob n ---> UIDn
I have:
keyfob 1 ---> UID1
keyfob 2 ---> UID1
keyfob 3 ---> UID1
.
.
.
keyfob n ---> UID1 |
|
|
vas57
Joined: 31 Jan 2022 Posts: 29
|
|
Posted: Wed Feb 02, 2022 3:37 am |
|
|
OK, but if the module sends, for different keyfobs, different UIDs, I must have different data in buffer. Now I only have the data of first keyfob.
This is my problem.
PS -After message[2], the module send keyfob UID.
-For different keyfobs the module send different UIDs. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 02, 2022 4:19 am |
|
|
Post your new code where you get the keyfob reply from the buffer
by calling bgetc(). |
|
|
vas57
Joined: 31 Jan 2022 Posts: 29
|
|
Posted: Wed Feb 02, 2022 4:55 am |
|
|
About below bgetc(), if I delete it from code, nothing changes in reading UID(only first UID is reading) !!! I can't understand that.
#define bkbhit (next_in!=next_out)
byte bgetc() {
byte c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
} |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Feb 02, 2022 6:45 am |
|
|
We know what bgetc has in it.
The point is you have to _use_ it.
Where you have:
Code: |
putc(buffer[0]);
putc(buffer[1]);
putc(buffer[2]);
putc(buffer[3]);
putc(buffer[4]);
putc(buffer[5]);
putc(buffer[6]);
putc(buffer[7]);
putc(buffer[8]);
putc(buffer[9]);
putc(buffer[10]);
putc(buffer[11]);
putc(buffer[12]);
putc(buffer[13]);
putc(buffer[14]);
putc(buffer[15]);
|
You need each time to be using:
putc(bgetc());
Then the next time you use this code it'll access the next message,
not the first one.,
Currently you are not actually accessing the circular buffer, but just the
start of the physical buffer. Since as far as the buffer is concerned you
have not read the data, it'll stop working once it is full. |
|
|
|