Browse Source

New function : seek_tag

Pi3rrot 11 years ago
parent
commit
6159c73019
5 changed files with 77 additions and 4 deletions
  1. 6 0
      README.md
  2. 63 0
      lib/SM130/SM130.cpp
  3. 1 0
      lib/SM130/SM130.h
  4. 2 2
      make.sh
  5. 5 2
      src/RFID-Mifare.pde

+ 6 - 0
README.md

@@ -27,6 +27,12 @@ and some Mifare 1K tags :)
 
 Have fun !
 
+Functions :
+===========
+
+send_command => to send a command directrly to the hardware, and print response.
+seek_tag => detect if a tag is in field, check the type, and print the serial number.
+
 Thanks
 ======
 

+ 63 - 0
lib/SM130/SM130.cpp

@@ -79,6 +79,7 @@ bool SM130::read_response(uint8_t *ret, uint8_t *length, uint8_t *cmd) {
 		ret[i] = rfid_read_sync();
 		crc += ret[i];
 
+		Serial.print("0x");
 		Serial.print(ret[i], HEX);
 		Serial.print (" ");
 		i++;
@@ -115,3 +116,65 @@ uint8_t SM130::rfid_read_sync() {
 	while(!rfid.available());
 	return rfid.read();
 }
+
+bool SM130::seek_tag(uint8_t *id, uint8_t *length) {
+	uint8_t cmd, tag_type, tag_len;
+	uint8_t buf[10], buf_len =10, ret[10], len = 10;
+	int i;
+	send_command(0x82, NULL, 0, ret, &len);					//send de la commande 0x82 seek for tag
+	read_response(buf, &buf_len, &cmd);
+	Serial.println("<seek_tag>");
+
+	if(cmd != 0x82) {							//check si c'est bien la bonne commande
+		Serial.print("Bad command 0x");
+		Serial.print(cmd, HEX);
+		Serial.print(". Should be 0x82\r");
+		goto error;
+	}
+
+	tag_type = buf[0];							//on à le type de tag ici
+	switch (tag_type) {								//check de quel type de tag il s'agit (voir datasheet)
+		case 0x01:
+			tag_len = 7;
+			Serial.println("tag_type = Mifare UltraLight");
+			break;
+
+		case 0x02:
+			tag_len = 4;
+			Serial.println("tag_type = Mifare Santard 1K");
+			break;
+
+		case 0x03:
+			tag_len = 4;
+			Serial.println("tag_type = Mifare Classic 4K");
+			break;
+
+		default:							//tag moisi
+			Serial.print("Error : Unknown Mifare tag :");
+			Serial.print(tag_type, HEX);
+			Serial.println();
+			goto error;
+
+	}
+
+	Serial.print("Tag serial = ");
+	for (i = 0; i < tag_len; i++) {
+		id[i] = buf[tag_len - i];
+		Serial.print(id[i], HEX);
+		Serial.print(" ");
+	}
+
+	Serial.println();
+	*length = tag_len;
+	Serial.println("</seek_tag>");
+	return true;
+
+error:
+	while(rfid.available())
+		rfid.read();
+	Serial.println("</seek_tag>");
+	return false;
+
+
+
+}

+ 1 - 0
lib/SM130/SM130.h

@@ -10,6 +10,7 @@ class SM130 {
 		bool send_command(uint8_t cmd, const uint8_t *cmd_args, const uint8_t cmd_args_length, uint8_t *ret, uint8_t *length);
 		bool read_response(uint8_t *ret, uint8_t *length, uint8_t *cmd);
 		uint8_t rfid_read_sync();
+		bool seek_tag(uint8_t *id, uint8_t *length);
 
 	private:
 

+ 2 - 2
make.sh

@@ -1,3 +1,3 @@
-ino build -m atmega328
-ino upload -m atmega328 -p /dev/ttyUSB0
+ino build -m atmega328 -v 
+ino upload -m atmega328 -p /dev/ttyUSB0 
 ino serial -p /dev/ttyUSB0

+ 5 - 2
src/RFID-Mifare.pde

@@ -8,11 +8,14 @@ void setup() {
 	uint8_t ret[10];
 	uint8_t len = 10;
 	SM130.send_command(0x80, NULL,0 ,ret, &len);		//RESET
-//	delay(100);
-//	SM130 send_command(0x81, NULL, ret, &len);		//Read Firmware Version
+	delay(100);
 }
 
 
 void loop() {
+	uint8_t id[10];
+	uint8_t id_len = 10;
 
+	SM130.seek_tag(id, &id_len);
+//	SM130.send_command(0x82, NULL, 0, ret, &len);
 }