SM130.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <Arduino.h>
  2. #include <SoftwareSerial.h>
  3. #include "SM130.h"
  4. SoftwareSerial rfid(7, 8);
  5. SM130::SM130() {
  6. rfid.begin(19200);
  7. }
  8. bool SM130::send_command(uint8_t cmd, const uint8_t *cmd_args, const uint8_t cmd_args_length, uint8_t *ret, uint8_t *length) {
  9. uint8_t length_cmd = 1 + cmd_args_length; //(1) + les arguments
  10. uint8_t crc = length_cmd + cmd; //taille totale
  11. Serial.print("<command 0x"); //Print de la commande
  12. Serial.print(cmd, HEX);
  13. Serial.print("> Sent !");
  14. Serial.println();
  15. rfid.write((uint8_t)0xFF); //Le header qui fait 0xFF
  16. rfid.write((uint8_t)0x00); //Reserved byte toujours à 0x00
  17. rfid.write((uint8_t)length_cmd); //Lenght of the payload data
  18. rfid.write((uint8_t)cmd); //byte of the instruction command
  19. for (int i = 0; i < cmd_args_length; i++) { //Datas de taille N bytes dans une boucle
  20. rfid.write((uint8_t)cmd_args[i]);
  21. crc += cmd_args[i]; // on incrémente le CRC
  22. }
  23. rfid.write((uint8_t)crc); //on write le CRC
  24. return read_response(ret, length, NULL);
  25. }
  26. bool SM130::read_response(uint8_t *ret, uint8_t *length, uint8_t *cmd) {
  27. uint8_t crc = 0;
  28. uint8_t ret_header, reserved, ret_length, ret_cmd, ret_crc;
  29. Serial.println("<read_response>");
  30. //Header byte
  31. ret_header = rfid_read_sync(); //on recup le 1er byte de réponse qui doit etre 0xFF toujours
  32. if(ret_header != 0xFF) { //et on le test ;)
  33. Serial.print("Bad header: 0x");
  34. Serial.print(ret_header, HEX);
  35. Serial.print(". Must be 0xFF !");
  36. goto error;
  37. }
  38. //Reserved Byte //On incremente pas CRC du header byte d'apres la doc
  39. reserved = rfid_read_sync(); //et on continue !
  40. if(reserved != 0x00) { //on test le byte reserved qui doit être à 0x00
  41. Serial.print("Bad reserved = 0x");
  42. Serial.print(ret_header, HEX);
  43. Serial.print(". Must be 0x00 !");
  44. goto error;
  45. }
  46. crc = reserved; //on oublie pas le $ù%#[[ de CRC
  47. //Length byte
  48. ret_length = rfid_read_sync();
  49. Serial.print("Length = 0x");
  50. Serial.print(ret_length, HEX);
  51. Serial.println();
  52. crc += ret_length;
  53. //Command byte
  54. ret_cmd = rfid_read_sync(); //On est censé récupérer la même commande que le send_command
  55. if(cmd) { //On test
  56. *cmd = ret_cmd;
  57. }
  58. ret_length--;
  59. Serial.print("command = 0x");
  60. Serial.print(ret_cmd, HEX);
  61. Serial.println();
  62. crc += ret_cmd;
  63. //Lecture de la réponse complête
  64. Serial.print("response = ");
  65. int i;
  66. while (i < ret_length) {
  67. ret[i] = rfid_read_sync();
  68. crc += ret[i];
  69. Serial.print("0x");
  70. Serial.print(ret[i], HEX);
  71. Serial.print (" ");
  72. i++;
  73. }
  74. Serial.println();
  75. //On check le CRC
  76. ret_crc = rfid_read_sync();
  77. if (ret_crc != crc) {
  78. Serial.print("Bad CRC 0x");
  79. Serial.print(ret_crc, HEX);
  80. Serial.print(" ! Should be : 0x");
  81. Serial.print(crc, HEX);
  82. Serial.println();
  83. return false;
  84. }
  85. *length = ret_length;
  86. Serial.println("</response>");
  87. return true;
  88. error:
  89. while(rfid.available())
  90. rfid.read();
  91. Serial.println("</response>");
  92. return false;
  93. }
  94. uint8_t SM130::rfid_read_sync() {
  95. while(!rfid.available());
  96. return rfid.read();
  97. }
  98. bool SM130::seek_tag(uint8_t *id, uint8_t *length) {
  99. uint8_t cmd, tag_type, tag_len;
  100. uint8_t buf[10], buf_len =10, ret[10], len = 10;
  101. int i;
  102. send_command(0x82, NULL, 0, ret, &len); //send de la commande 0x82 seek for tag
  103. read_response(buf, &buf_len, &cmd);
  104. Serial.println("<seek_tag>");
  105. if(cmd != 0x82) { //check si c'est bien la bonne commande
  106. Serial.print("Bad command 0x");
  107. Serial.print(cmd, HEX);
  108. Serial.print(". Should be 0x82\r");
  109. goto error;
  110. }
  111. tag_type = buf[0]; //on à le type de tag ici
  112. switch (tag_type) { //check de quel type de tag il s'agit (voir datasheet)
  113. case 0x01:
  114. tag_len = 7;
  115. Serial.println("tag_type = Mifare UltraLight");
  116. break;
  117. case 0x02:
  118. tag_len = 4;
  119. Serial.println("tag_type = Mifare Santard 1K");
  120. break;
  121. case 0x03:
  122. tag_len = 4;
  123. Serial.println("tag_type = Mifare Classic 4K");
  124. break;
  125. default: //tag moisi
  126. Serial.print("Error : Unknown Mifare tag :");
  127. Serial.print(tag_type, HEX);
  128. Serial.println();
  129. goto error;
  130. }
  131. Serial.print("Tag serial = ");
  132. for (i = 0; i < tag_len; i++) {
  133. id[i] = buf[tag_len - i];
  134. Serial.print(id[i], HEX);
  135. Serial.print(" ");
  136. }
  137. Serial.println();
  138. *length = tag_len;
  139. Serial.println("</seek_tag>");
  140. return true;
  141. error:
  142. while(rfid.available())
  143. rfid.read();
  144. Serial.println("</seek_tag>");
  145. return false;
  146. }