如何解决Arduino编译错误:'TagInfo'未在此作用域中声明?
RFID项目编译错误:'TagInfo' was not declared in this scope 问题解决
问题描述
基于Arduino Uno R3、W5100以太网盾和MFRC522读卡器开发RFID项目,编译时出现错误:Compilation error: 'TagInfo' was not declared in this scope,目标是将RFID标签UID发送到NodeJS服务器。相关代码如下:
#include <SPI.h> #include <MFRC522.h> #include <Ethernet.h> #include "MFRC522Extended.h" // Include the extended library header #define RST_PIN 9 // Reset pin for MFRC522 #define SS_PIN 10 // Slave Select pin for MFRC522 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance //Ethernet settings byte mac[] = { 0x60, 0x3e, 0x5f, 0x86, 0xd8, 0xc9 }; // MAC address of Ethernet shield IPAddress server(127, 0, 0, 1); // IP address of your server (localhost) int port = 3001; // Port number of your server // Initialize the Ethernet client EthernetClient client; // Create an instance of MFRC522Extended MFRC522Extended mfrc522Extended; void setup() { Serial.begin(9600); // Initialize serial communications SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 module // Initialize Ethernet with DHCP if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // Try with a static IP if DHCP fails: Ethernet.begin(mac, IPAddress(192, 168, 1, 177)); } else { Serial.print("Ethernet configured via DHCP, IP address: "); Serial.println(Ethernet.localIP()); } Serial.println("RFID Ready"); } void loop() { // Look for new RFID cards if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { // Show UID on serial monitor Serial.print("UID tag: "); String content = ""; for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte[i], HEX)); } content.toUpperCase(); Serial.println(); // Perform extended functionality TagInfo tagInfo; // Declaration of TagInfo byte sendData[10]; // Example data byte backData[10]; // Example data byte sendLen = 10; byte backLen = 10; MFRC522::StatusCode status = mfrc522Extended.TCL_Transceive(&tagInfo, sendData, sendLen, backData, &backLen); if (status == MFRC522::StatusCode::SUCCESS) { // Process successful operation Serial.println("Extended operation successful"); } else { // Handle error Serial.println("Error in extended operation"); } // Send UID to the server via Ethernet if (client.connect(server, port)) { client.println("UID: " + content); // Optionally, send extended data as well // client.println("Extended Data: " + String(backData[0])); // Example client.stop(); } delay(1000); // Delay for readability } }
错误原因分析
- TagInfo作用域未明确:
TagInfo是MFRC522Extended类的内部结构体,直接声明TagInfo tagInfo会导致编译器无法识别,必须指定类作用域。 - MFRC522Extended实例初始化错误:扩展库实例依赖已初始化的
MFRC522对象,空实例MFRC522Extended mfrc522Extended无法正常调用扩展功能。
解决方法
步骤1:修正MFRC522Extended实例初始化
将空实例声明改为依赖基础MFRC522对象:
MFRC522Extended mfrc522Extended(mfrc522);
步骤2:指定TagInfo的类作用域
将TagInfo tagInfo;改为:
MFRC522Extended::TagInfo tagInfo;
步骤3:确保库兼容性
确认MFRC522Extended库已正确安装,且与基础MFRC522库版本兼容。
修正后的完整代码
#include <SPI.h> #include <MFRC522.h> #include <Ethernet.h> #include "MFRC522Extended.h" // Include the extended library header #define RST_PIN 9 // Reset pin for MFRC522 #define SS_PIN 10 // Slave Select pin for MFRC522 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance //Ethernet settings byte mac[] = { 0x60, 0x3e, 0x5f, 0x86, 0xd8, 0xc9 }; // MAC address of Ethernet shield IPAddress server(127, 0, 0, 1); // IP address of your server (localhost) int port = 3001; // Port number of your server // Initialize the Ethernet client EthernetClient client; // Create an instance of MFRC522Extended with the base MFRC522 instance MFRC522Extended mfrc522Extended(mfrc522); void setup() { Serial.begin(9600); // Initialize serial communications SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 module // Initialize Ethernet with DHCP if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // Try with a static IP if DHCP fails: Ethernet.begin(mac, IPAddress(192, 168, 1, 177)); } else { Serial.print("Ethernet configured via DHCP, IP address: "); Serial.println(Ethernet.localIP()); } Serial.println("RFID Ready"); } void loop() { // Look for new RFID cards if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { // Show UID on serial monitor Serial.print("UID tag: "); String content = ""; for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte[i], HEX)); } content.toUpperCase(); Serial.println(); // Perform extended functionality MFRC522Extended::TagInfo tagInfo; // Declaration with class scope byte sendData[10]; // Example data byte backData[10]; // Example data byte sendLen = 10; byte backLen = 10; MFRC522::StatusCode status = mfrc522Extended.TCL_Transceive(&tagInfo, sendData, sendLen, backData, &backLen); if (status == MFRC522::StatusCode::SUCCESS) { // Process successful operation Serial.println("Extended operation successful"); } else { // Handle error Serial.println("Error in extended operation"); } // Send UID to the server via Ethernet if (client.connect(server, port)) { client.println("UID: " + content); // Optionally, send extended data as well // client.println("Extended Data: " + String(backData[0])); // Example client.stop(); } // Halt the PICC to allow new cards to be detected mfrc522.PICC_HaltA(); // Stop encryption on PCD mfrc522.PCD_StopCrypto1(); delay(1000); // Delay for readability }
额外说明:修正后的代码添加了mfrc522.PICC_HaltA()和mfrc522.PCD_StopCrypto1(),确保每次读取后正确释放标签,避免重复读取同一标签的问题。
内容的提问来源于stack exchange,提问作者Krutika Renake
相关产品推荐
相关产品推荐

