CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem reading data from an RFID module
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vas57



Joined: 31 Jan 2022
Posts: 29

View user's profile Send private message

Problem reading data from an RFID module
PostPosted: Mon Jan 31, 2022 7:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 31, 2022 7:36 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 31, 2022 7:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 31, 2022 8:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 31, 2022 8:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 01, 2022 6:23 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 01, 2022 6:39 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 12:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 2:16 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 2:58 am     Reply with quote

(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

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 3:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 3:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 4:19 am     Reply with quote

Post your new code where you get the keyfob reply from the buffer
by calling bgetc().
vas57



Joined: 31 Jan 2022
Posts: 29

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 4:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 02, 2022 6:45 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group