Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Help needed with RFID card reader

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
I am doing a project of RFID card reader with PIC18F45K22 @ 4 MHz external crystal. I am using UART baudrate of 9600 bps.

My card gives the below data.

[02]1400508931FC[03][02]1400508931FC[03][02]1400508931FC[03][02]1400508931FC[03][02]1400508931FC[03]

So, [02]1400508931FC[03] repeats if card is placed near reader. This I found by connecting the card reader to PC and watching the data on Termite Serial terminal software.

On my card it is printed 0005278001 080,35121

So, according to page 8 of the attached document

0005278001 is UART data and 080,35121 is Wiegand data.

How does 0005278001 translate to tag shown above ?

If [02]1400508931FC[03] is tag then what is the 0005278001 number printed on card ? And why in the document page no. 8 it is mentioned as UART data ?



Also in the tag given out by the card there [02] in the beginning and [03] in the end. Are they Start of text and end of text ? Here it is mentioned that they are STX and ETX.


In mikroC PRO how to check if received data is STX and ETX ?

http://www.asciitable.com/
 

Attachments

Last edited:

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
You have neglected to mention what specific RFID reader you are using?

In any event, the output format of the RFID readers varies widely, therefore knowing the specific output format of an RFID reader is crucial.

Yes, the 0x02 and 0x03 are the ASCII STX and ETX, there is also most likely format, CRC or checksum data included in the output stream as well.

You card number is included in the outputted data stream:

Card Number in Decimal: 0005278001

Card Number in Binary: 10100001000100100110001

RFID Output Stream in Hexadecimal: 1400508931FC

RFID Output Stream in Binary: 101000000000001010000100010010011000111111100

As you can see the last byte is most likely extraneous data, formatting, checksum, CRC, etc.


BigDog
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
I am using this RFID Card Reader.

http://imall.iteadstudio.com/im120618002.html

This is the product Wiki page.

http://wiki.iteadstudio.com/RDM6300

These are the RFID cards and keychains I am using.

http://imall.iteadstudio.com/wireless/rfid/im120628016.html

http://imall.iteadstudio.com/wireless/rfid/im121224001.html

How did you calculate the different numbers ?

Here I am attaching the project I did. It is working fine with my card. I connected Reader to PC and used Proteus COMPIM to connect RFID reader to Proteus PIC18F45K22. I first watched the hex values sent on UART and then made changes to the code (tag) and then it worked but the tag received is different from the data I obtained in Termite. Why ?

The data I obtained when using Proteus is the tag[] inside code.

 

Attachments


bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
The last byte (0xFC) is an XOR checksum generated by XOR'ing the preceding five bytes (0x1400508931):

0x14 XOR 0x00 XOR 0x50 XOR 0x89 XOR 0x31 = 0xFC


According to the RDM630 Specifications Datasheet, the output stream consists of ten ASCII characters followed by the ASCII representation of the XOR Checksum byte, therefore twelve bytes total, not including the STX and ETX characters framing the data packet.


From your posted screen capture:

[02]
31 - 1
34 - 4
30 - 0
30 - 0
35 - 5
30 - 0
38 - 8
39 - 9
32 - 2
43 - C
45 - E
31 - 1
[03]

0x14 XOR 0x00 XOR 0x50 XOR 0x89 XOR 0x2C = 0xE1 CRC

Card in Hexadecimal: 0x140050892C
Card in Decimal: 85904623916
Card in Binary: 0001010000000000010100001000100100101100
Number Printed on Back of Card: 0005277996


Obviously a different card than your original post.


BigDog
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Thanks. That made things clear. I used two cards. One to turn ON LED and another to turn OFF LED. These are printed on the cards.

0005277996 080,35116

0005619000 085,48440

- - - Updated - - -

I have a strange problem. I am testing in both Proteus and hardware. For hardware I am using EasyPIC v7. In Proteus it is working fine that is LED toggles if card read more than one time but in hardware LED turns ON but doesn't turn OFF. What might be the problem. I have posted the project in my previous post.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
I have a problem here. I am attaching my RFID project here. I am using mikroC PRO PIC Compiler and PIC18F45K22 @ 8 MHz external Crystal. The problem is if I read any Card then LED corresponding to that card lights ON but after that if I read the same card or another card then LED doesn't toggle or other LEDs don't turn ON.

I am testing in EasyPIC v7 development board.

@ bigdogguru

In post #2 you mentioned

Card Number in Decimal: 0005278001

Card Number in Binary: 10100001000100100110001

RFID Output Stream in Hexadecimal: 1400508931FC

If I convert dec 5278001 to hex I get 508931. FC is checksum. Where did 1400 came from ?

- - - Updated - - -

Problem solved. I had to turn ON/OFF GIE_bit instead of RC1IE_bit. Now the code works fine in Proteus and also hardware.

- - - Updated - - -

This is the working code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
sbit LED1 at LATD0_bit;
sbit LED2 at LATD1_bit;
sbit LED3 at LATD2_bit;
sbit LED4 at LATD3_bit;
sbit LED5 at LATD4_bit;
 
sbit LED1_Direction at TRISD0_bit;
sbit LED2_Direction at TRISD1_bit;
sbit LED3_Direction at TRISD2_bit;
sbit LED4_Direction at TRISD3_bit;
sbit LED5_Direction at TRISD4_bit;
 
unsigned char tag1[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x32, 0x43, 0x45, 0x31, 0x03};
unsigned char tag2[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x31, 0x32, 0x31, 0x30, 0x41, 0x36, 0x45, 0x03};
unsigned char tag3[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x35, 0x42, 0x44, 0x33, 0x38, 0x43, 0x34, 0x03};
unsigned char tag4[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x42, 0x37, 0x45, 0x38, 0x31, 0x42, 0x03};
unsigned char tag5[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x33, 0x31, 0x46, 0x43, 0x03};
 
unsigned char uartBuffer[14], received = 0, i = 0;
 
void interrupt() {
 
     if(RC1IF_bit) {
            if(OERR1_bit) {
                  OERR1_bit = 0;
                  CREN1_bit = 0;
                  CREN1_bit = 1;            
            }
            
            uartBuffer[i] = UART1_Read();
            
            if(received == 0) {
                 if(uartBuffer[i] == 2) {
                       received = 1;
                       i++;                   
                 }
            }
            else if(received == 1) {
                  if(uartBuffer[i] != 3)
                         i++;
                  else { 
                       received = 2;
                       GIE_bit = 0;                       
                  }
            }
                       
            
            RC1IF_bit = 0;
     }
}
 
void main() {
 
     CM1CON0 = 0x00;
     CM2CON0 = 0x00;
     
     SLRCON = 0x00;
     
     ANSELA = 0x00;
     ANSELB = 0x00;
     ANSELC = 0x00;
     ANSELD = 0x00;
     ANSELE = 0x00;
     
     TRISA = 0xC0;
     TRISB = 0x00;
     TRISC = 0xC0;
     TRISD = 0x00;
     TRISE = 0x00;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     PORTE = 0x00;
     
     LATA = 0x00;
     LATB = 0x00;
     LATC = 0x00;
     LATD = 0x00;
     LATE = 0x00;
     
     
     UART1_Init(9615);
     Delay_ms(200);
     
     RC1IE_bit = 1;
     PEIE_bit = 1;
     GIE_bit = 1;
     
     RC1IF_bit = 0;
     
     LED1_Direction = 0;
     LED2_Direction = 0;
     LED3_Direction = 0;
     LED4_Direction = 0;
     LED5_Direction = 0;
     
     LED1 = 0;
     LED2 = 0;
     LED3 = 0;
     LED4 = 0;
     LED5 = 0;
     
     PWM1_Init(2000);
     PWM1_Set_Duty(255);
     PWM1_Stop();
     
     UART1_Write_Text("Swipe a card\r\n");
               
     while(1) {
     
            if(received == 2) {
                                          
                  if(memcmp(uartBuffer, tag1, 14) == 0) {
                              LED1 = ~LED1;
                              UART1_Write_Text("Tag matched\r\n");                     
                  }
                  else if(memcmp(uartBuffer, tag2, 14) == 0) {
                              LED2 = ~LED2;
                              UART1_Write_Text("Tag matched\r\n");                     
                  }
                  else if(memcmp(uartBuffer, tag3, 14) == 0) {
                              LED3 = ~LED3;
                              UART1_Write_Text("Tag matched\r\n");                     
                  }
                  else if(memcmp(uartBuffer, tag4, 14) == 0) {
                              LED4 = ~LED4;
                              UART1_Write_Text("Tag matched\r\n");                     
                  }
                  else if(memcmp(uartBuffer, tag5, 14) == 0) {
                              LED5 = ~LED5;
                              UART1_Write_Text("Tag matched\r\n");                     
                  }
                  else UART1_Write_Text("Tag doesn't match\r\n");
                  
                  PWM1_Start();                                   
                  Delay_ms(3000);
                  PWM1_Stop();
                                      
                  received = 0;
                  i = 0;
                  memset(uartBuffer, '\0', sizeof(uartBuffer));
                  UART1_Write_Text("Swipe a card\r\n");
                  
                  GIE_bit = 1;
                  RC1IF_bit = 0;                            
            }    
     }
}



- - - Updated - - -

@bigdogguru

I modified the code to check for checksum but it doesn't work. Why ?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
sbit LED1 at LATD0_bit;
sbit LED2 at LATD1_bit;
sbit LED3 at LATD2_bit;
sbit LED4 at LATD3_bit;
sbit LED5 at LATD4_bit;
 
sbit LED1_Direction at TRISD0_bit;
sbit LED2_Direction at TRISD1_bit;
sbit LED3_Direction at TRISD2_bit;
sbit LED4_Direction at TRISD3_bit;
sbit LED5_Direction at TRISD4_bit;
 
unsigned char tag1[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x32, 0x43, 0x45, 0x31, 0x03};
unsigned char tag2[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x31, 0x32, 0x31, 0x30, 0x41, 0x36, 0x45, 0x03};
unsigned char tag3[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x35, 0x42, 0x44, 0x33, 0x38, 0x43, 0x34, 0x03};
unsigned char tag4[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x42, 0x37, 0x45, 0x38, 0x31, 0x42, 0x03};
unsigned char tag5[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x33, 0x31, 0x46, 0x43, 0x03};
 
unsigned char uartBuffer[14], received = 0, i = 0;
int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0;
char buffer[3];
 
void interrupt() {
 
     if(RC1IF_bit) {
            if(OERR1_bit) {
                  OERR1_bit = 0;
                  CREN1_bit = 0;
                  CREN1_bit = 1;            
            }
            
            uartBuffer[i] = UART1_Read();
            
            if(received == 0) {
                 if(uartBuffer[i] == 2) {
                       received = 1;
                       i++;                   
                 }
            }
            else if(received == 1) {
                  if(uartBuffer[i] != 3)
                         i++;
                  else { 
                       received = 2;
                       GIE_bit = 0;                       
                  }
            }
                       
            
            RC1IF_bit = 0;
     }
}
 
void main() {
 
     CM1CON0 = 0x00;
     CM2CON0 = 0x00;
     
     SLRCON = 0x00;
     
     ANSELA = 0x00;
     ANSELB = 0x00;
     ANSELC = 0x00;
     ANSELD = 0x00;
     ANSELE = 0x00;
     
     TRISA = 0xC0;
     TRISB = 0x00;
     TRISC = 0xC0;
     TRISD = 0x00;
     TRISE = 0x00;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     PORTE = 0x00;
     
     LATA = 0x00;
     LATB = 0x00;
     LATC = 0x00;
     LATD = 0x00;
     LATE = 0x00;
     
     
     UART1_Init(9615);
     Delay_ms(200);
     
     RC1IE_bit = 1;
     PEIE_bit = 1;
     GIE_bit = 1;
     
     RC1IF_bit = 0;
     
     LED1_Direction = 0;
     LED2_Direction = 0;
     LED3_Direction = 0;
     LED4_Direction = 0;
     LED5_Direction = 0;
     
     LED1 = 0;
     LED2 = 0;
     LED3 = 0;
     LED4 = 0;
     LED5 = 0;
     
     PWM1_Init(2000);
     PWM1_Set_Duty(255);
     PWM1_Stop();
     
     UART1_Write_Text("Swipe a card\r\n");
               
     while(1) {
     
            if(received == 2) {
                  buffer[0] = uartBuffer[1];
                  buffer[1] = uartBuffer[2];
                  buffer[2] = '\0';
                  
                  val1 = atoi(buffer);
                  
                  buffer[0] = uartBuffer[3];
                  buffer[1] = uartBuffer[4];
                  buffer[2] = '\0';
                  
                  val2 = atoi(buffer);
                  
                  buffer[0] = uartBuffer[5];
                  buffer[1] = uartBuffer[6];
                  buffer[2] = '\0';
                  
                  val3 = atoi(buffer);
                  
                  buffer[0] = uartBuffer[7];
                  buffer[1] = uartBuffer[8];
                  buffer[2] = '\0';
                  
                  val4 = atoi(buffer);
                  
                  buffer[0] = uartBuffer[9];
                  buffer[1] = uartBuffer[10];
                  buffer[2] = '\0';
                  
                  val5 = atoi(buffer);
                  
                  buffer[0] = uartBuffer[11];
                  buffer[1] = uartBuffer[12];
                  buffer[2] = '\0';
                  
                  val6 = atoi(buffer);
                  
                  if(val6 == ((val1) ^ (val2) ^ (val3) ^ (val4) ^ (val5))) {                                          
                        if(memcmp(uartBuffer, tag1, 14) == 0) {
                                    LED1 = ~LED1;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag2, 14) == 0) {
                                    LED2 = ~LED2;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag3, 14) == 0) {
                                    LED3 = ~LED3;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag4, 14) == 0) {
                                    LED4 = ~LED4;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag5, 14) == 0) {
                                    LED5 = ~LED5;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }                  
                  }
                  else UART1_Write_Text("Tag doesn't match\r\n");
                  
                  PWM1_Start(); 
                  Delay_ms(3000);
                  PWM1_Stop();
                                      
                  received = 0;
                  i = 0;
                  memset(uartBuffer, '\0', sizeof(uartBuffer));
                  UART1_Write_Text("Swipe a card\r\n");
                  
                  GIE_bit = 1;
                  RC1IF_bit = 0;                            
            }    
     }
}



- - - Updated - - -

How to convert decimal to hex ?

- - - Updated - - -

How to convert Ascii to Hex ? Like if I have "12AF" it should be converted to 0x12 and 0xAF. How to do this ?

- - - Updated - - -

I did like this and it is working fine.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
sbit LED1 at LATD0_bit;
sbit LED2 at LATD1_bit;
sbit LED3 at LATD2_bit;
sbit LED4 at LATD3_bit;
sbit LED5 at LATD4_bit;
 
sbit LED1_Direction at TRISD0_bit;
sbit LED2_Direction at TRISD1_bit;
sbit LED3_Direction at TRISD2_bit;
sbit LED4_Direction at TRISD3_bit;
sbit LED5_Direction at TRISD4_bit;
 
unsigned char tag1[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x32, 0x43, 0x45, 0x31, 0x03};
unsigned char tag2[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x31, 0x32, 0x31, 0x30, 0x41, 0x36, 0x45, 0x03};
unsigned char tag3[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x35, 0x42, 0x44, 0x33, 0x38, 0x43, 0x34, 0x03};
unsigned char tag4[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x42, 0x37, 0x45, 0x38, 0x31, 0x42, 0x03};
unsigned char tag5[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x33, 0x31, 0x46, 0x43, 0x03};
 
unsigned char uartBuffer[14], received = 0, i = 0;
unsigned int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0;
char buffer[3];
 
 
void interrupt() {
 
     if(RC1IF_bit) {
            if(OERR1_bit) {
                  OERR1_bit = 0;
                  CREN1_bit = 0;
                  CREN1_bit = 1;            
            }
            
            uartBuffer[i] = UART1_Read();
            
            if(received == 0) {
                 if(uartBuffer[i] == 2) {
                       received = 1;
                       i++;                   
                 }
            }
            else if(received == 1) {
                  if(uartBuffer[i] != 3)
                         i++;
                  else { 
                       received = 2;
                       GIE_bit = 0;                       
                  }
            }
                       
            
            RC1IF_bit = 0;
     }
}
 
unsigned int Ascii2Hex(char *s) {
 
      unsigned int num1 = 0, num2 = 0;
      
      if((*s >= '0') & (*s <= '9'))
            num1 = *s - 48;
      else if((*s >= 'A') && (*s <= 'F'))
            num1 = *s - 55;
            
      *s++;
      
      if((*s >= '0') & (*s <= '9'))
            num2 = *s - 48;
      else if((*s >= 'A') && (*s <= 'F'))
            num2 = *s - 55;
            
      return (num1 * 16) + (num2);
}
 
void main() {
 
     CM1CON0 = 0x00;
     CM2CON0 = 0x00;
     
     SLRCON = 0x00;
     
     ANSELA = 0x00;
     ANSELB = 0x00;
     ANSELC = 0x00;
     ANSELD = 0x00;
     ANSELE = 0x00;
     
     TRISA = 0xC0;
     TRISB = 0x00;
     TRISC = 0xC0;
     TRISD = 0x00;
     TRISE = 0x00;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     PORTE = 0x00;
     
     LATA = 0x00;
     LATB = 0x00;
     LATC = 0x00;
     LATD = 0x00;
     LATE = 0x00;
               
     UART1_Init(9615);
     Delay_ms(200);
     
     RC1IE_bit = 1;
     PEIE_bit = 1;
     GIE_bit = 1;
     
     RC1IF_bit = 0;
     
     LED1_Direction = 0;
     LED2_Direction = 0;
     LED3_Direction = 0;
     LED4_Direction = 0;
     LED5_Direction = 0;
     
     LED1 = 0;
     LED2 = 0;
     LED3 = 0;
     LED4 = 0;
     LED5 = 0;
     
     PWM1_Init(2000);
     PWM1_Set_Duty(255);
     PWM1_Stop();
     
     UART1_Write_Text("Swipe a card\r\n");
               
     while(1) {
     
            if(received == 2) {
                  buffer[0] = uartBuffer[1];
                  buffer[1] = uartBuffer[2];
                  buffer[2] = '\0';
                  
                  val1 = Ascii2Hex(buffer);
                  
                                                      
                  buffer[0] = uartBuffer[3];
                  buffer[1] = uartBuffer[4];
                  buffer[2] = '\0';
                  
                  val2 = Ascii2Hex(buffer);
                  
                  buffer[0] = uartBuffer[5];
                  buffer[1] = uartBuffer[6];
                  buffer[2] = '\0';
                  
                  val3 = Ascii2Hex(buffer);
                                    
                  buffer[0] = uartBuffer[7];
                  buffer[1] = uartBuffer[8];
                  buffer[2] = '\0';
                  
                  val4 = Ascii2Hex(buffer);
                  
                  buffer[0] = uartBuffer[9];
                  buffer[1] = uartBuffer[10];
                  buffer[2] = '\0';
                  
                  val5 = Ascii2Hex(buffer);                  
                  
                  buffer[0] = uartBuffer[11];
                  buffer[1] = uartBuffer[12];
                  buffer[2] = '\0';
                  
                  val6 = Ascii2Hex(buffer);
                  
                  if(val6 == ((val1) ^ (val2) ^ (val3) ^ (val4) ^ (val5))) {                                          
                        if(memcmp(uartBuffer, tag1, 14) == 0) {
                                    LED1 = ~LED1;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag2, 14) == 0) {
                                    LED2 = ~LED2;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag3, 14) == 0) {
                                    LED3 = ~LED3;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag4, 14) == 0) {
                                    LED4 = ~LED4;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }
                        else if(memcmp(uartBuffer, tag5, 14) == 0) {
                                    LED5 = ~LED5;
                                    UART1_Write_Text("Tag matched\r\n");                     
                        }                  
                  }
                  else UART1_Write_Text("Tag doesn't match\r\n");
                  
                  PWM1_Start(); 
                  Delay_ms(3000);
                  PWM1_Stop();
                                      
                  received = 0;
                  i = 0;
                  memset(uartBuffer, '\0', sizeof(uartBuffer));
                  UART1_Write_Text("Swipe a card\r\n");
                  
                  GIE_bit = 1;
                  RC1IF_bit = 0;                            
            }    
     }
}



- - - Updated - - -

This is the code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
unsigned int getHex(char s) {
 
      unsigned int num = 0;
      
      if((s >= '0') & (s <= '9'))
            num = s - 48;
      else if((s >= 'A') && (s <= 'F'))
            num = s - 55;
            
      return num;
}
 
unsigned int Ascii2Hex(char *s) {
 
      unsigned int num1 = 0, num2 = 0;
      
      num1 = getHex(*s);
      *s++;
      num2 = getHex(*s);
            
      return (num1 * 16) + (num2);
}

 

Attachments

Last edited:

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
...

How to convert decimal to hex ?

- - - Updated - - -

How to convert Ascii to Hex ? Like if I have "12AF" it should be converted to 0x12 and 0xAF. How to do this ?
An alternative method:

Code:
static char hex(unsigned char x){
//convert 4bits binary to 1 text hex digit
        x &= 0x0f;
        return (x<10) ? x +'0': x-10+'a';
}
static unsigned char _1dig_hex2int(char h){
//convert 1 text hex digit to equivalent binary
	if(h>='0' && h<='9')
		return h - '0';
	else if(h>='a' || h<='f')
		return h - 'a' + 10;
	else if(h>='A' || h<='F')
		return h - 'A' + 10;
	return 0;
}

unsigned hex2uint(const char *hex){
	//priority in portability in stead of speed
	char i;
	unsigned v = 0;
	for(i=0;i<sizeof(v)*2 && hex[i];i++){
		v <<= 4;
		v += (_1dig_hex2int(hex[i]) & 0x0f);
	}
	return v;
}

const char *uint2hex(unsigned num,char *hx){
	//priority in portability in stead of speed
	signed char i,len=0;
	do{
                hx[len++] = hex(num&0x0f);
		num>>=4;
	}while(num);
        hx[len]='\0';
	//reverse order
	for(i=len/2;i>=0;i--){
 		char tmp = hx[i];
		hx[i] = hx[len-i-1];
                hx[len-i-1] = tmp;
	}
	return hx;
}

void main(){
	char s[sizeof(int) * 2 + 1];
	printf("%u = %u\n",0x345a,hex2uint("345a"));
	printf("%x = %s\n",0x345a,uint2hex(0x345a,s));
}
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
@xenos

I saved the page. I will study your code. For me I have to convert two ascii chars to hex.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,361
Helped
4,724
Reputation
9,463
Reaction score
4,492
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
122,540
These are routines I use. they work on 4-bit values so you need to shift the value you are converting the appropriate number of times.

Code:
//*******************************************************************************
//  convert an ascii hex digit to it's equivalent binary value
unsigned char AscToBin(char AsciiChar)
{
	if(AsciiChar > '9') AsciiChar -= 7;
	return((AsciiChar - '0') & 0x0F);
}

//*******************************************************************************
//  convert a binary value to it's equivalent ascii hex character
unsigned char BinToAsc(unsigned char BinValue)
{
	BinValue &= 0x0F;
	if(BinValue > 9) BinValue += 7;
	return(BinValue + '0');
}
Brian.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
@ bigdogguru

In post #2 you mentioned

Card Number in Decimal: 0005278001

Card Number in Binary: 10100001000100100110001

RFID Output Stream in Hexadecimal: 1400508931FC

If I convert dec 5278001 to hex I get 508931. FC is checksum. Where did 1400 came from ?
Reference: EM4200 Transponder Datasheet, Direct Replacement for EM410x and EM400x Series, Section: Manchester 64 (EM4100 compatible), Page: 6

And

Reference: The attached EM4100 Transponder Datasheet, Section: Memory Array for Manchester & Bi-Phase encoding ICs, Page: 5

Bits D00 to D03 and bits D10 to D13 are customer specific identification.
EM410xStorage.JPG

The actual entire binary representation of the RFID stored on the tag consists of five bytes, which your RFID reader appends a CRC checksum byte upon reading and generating the ASCII represenation:

Binary: 0001 0100 0000 0000 0101 0000 1000 1001 0011 0001

Hexadecimal: 0x1400508931

Decimal: 85904623921

Typically the ID printed on the RFID tag is not the entire RFID stored on the RFID tag, EM410x/EM400x compatible transponders set aside five bytes of storage, partitioned into ten nibbles. The first byte/two nibbles (RED) are typically reserved for a field known by several names as the customer, group, or vendor ID, which is typically used to indicate a particular customer, group or vendor as the names imply, therefore the full RFID stored on the RFID tag/card is fact, 0x1400508931 in hexadecimal or 85904623921 in decimal. As previously indicated, RFID tag manufactures often omit the first byte/two nibbles, the customer, group or vendor ID, when printing the tag's number, 0005278001, for as an additional security measure. When developing code for RFID functionality, a list of acceptable customer, group or vendor IDs can be retained and checked against for an initial check of RFID tag validity. Or the entire RFID, including the customer, group or vendor ID, can be stored and checked against for added security measures, as it is typically only known by the business or those who possess a valid card and can read the entire RFID, including the customer, group or vendor ID.



BigDog
 

Attachments

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top