这是我几个月前工作的一个例子。我认为它运作正常。程序重置天线,然后设置工作天线,然后作为实时库存。如果您对arduino有所了解,可以阅读代码。 (它有一些西班牙语评论,因为我在墨西哥)
#include <SPI.h>
#include <SD.h>
File myFile;
byte reset_Message[] = {0xA0, 0x03, 0x01, 0x70, 0xEC };
byte set_WorkingAntenna[] = {0xA0, 0x04, 0x01, 0x74, 0x00, 0xE7 };
byte real_TimeInventory[] = {0xA0, 0x04, 0x01, 0x89, 0x01, 0xD1 };
int cont = 0;
int contSD = 1;
byte tag_ID[21];
String tag_IDInt="";
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 100; // interval at which to blink (milliseconds)
boolean found = false;
void setup() {
// initialize both serial ports:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial2.begin(115200);
Serial.print("Inicializando Memoria SD...");
if (!SD.begin(4)) {
Serial.println("Inicializacion Fallida!");
return;
}
Serial.println("Inicializacion Correcta.");
delay(100);
Serial2.write(reset_Message, sizeof(reset_Message));
delay(500);
Serial2.write(set_WorkingAntenna, sizeof(set_WorkingAntenna));
delay(50);
Serial2.write(real_TimeInventory, sizeof(real_TimeInventory));
tag_IDInt.reserve(200);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial2.write(set_WorkingAntenna, sizeof(set_WorkingAntenna));
delay(50);
Serial2.write(real_TimeInventory, sizeof(real_TimeInventory));
}
// read from port 2, send to port 0:
if (Serial2.available()) {
byte inByte = (byte)Serial2.read();
tag_ID [cont] = inByte;
//Serial.write(inByte);
cont++;
if (cont == 6 && tag_ID[3] == 116){ cont = 0;}
if (cont == 12 && tag_ID[4] == 0 ){ cont = 0;}
if (cont == 21){
for (int i=7; i<19; i++){
tag_IDInt += String(tag_ID[i],HEX);
//Serial.print(tag_ID[i], HEX);
//Serial.print(" ");
}
tag_IDInt += (char)'\r';
Serial.print("Tu ID es: ");
Serial.print(tag_IDInt);
if (lookForID(tag_IDInt)){
Serial.println(", Acceso Permitido");
}else{
Serial.println(", Acceso Denegado");
}
found = false;
tag_IDInt = "";
cont = 0;
}
}
}
boolean lookForID(String read_ID){
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("TAGIDS.csv", FILE_WRITE);
// re-open the file for reading:
myFile = SD.open("TAGIDS.csv");
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available() && !found) {
String line = myFile.readStringUntil('\n');
if (line == read_ID){
//Serial.print(" Found in Line ");
//Serial.print(contSD);
found = true;
contSD = 1;
return true;
}
else
contSD++;
}
// close the file:
myFile.close();
if(!found){
contSD = 1;
return false;
}
} else {
// if the file didn't open, print an error:
Serial.println("Error Abriendo TAGIDS.csv");
}
}