我正在尝试将 Sparkfun 的 SM130 RFID 阅读器示例代码转换为一个封装得很好并且可以在多个 arduino 草图中使用的类/库。
Sparkfun 示例代码:https ://github.com/sparkfun/RFID_Evaluation_Shield/blob/master/Firmware/RFID_Eval_13_56MHz.ino
大部分都进行得很顺利,直到我尝试将软件串行通信作为成员变量——我不断收到如下错误:
/Users/scottnla/Dropbox/arduino/libraries/SM130/SM130.cpp: In constructor 'SM130::SM130()':
/Users/scottnla/Dropbox/arduino/libraries/SM130/SM130.cpp:8: error: no matching function for call to 'SoftwareSerial::SoftwareSerial()'
/Applications/Arduino.app/Contents/Resources/Java/libraries/SoftwareSerial/SoftwareSerial.h:83: note: candidates are: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t, bool)
/Applications/Arduino.app/Contents/Resources/Java/libraries/SoftwareSerial/SoftwareSerial.h:48: note: SoftwareSerial::SoftwareSerial(const SoftwareSerial&)
我发现了一篇似乎解决了这个问题的 stackoverflow 帖子(为 Arduino 创建库),但他们的解决方案似乎对我不起作用。IDE 的错误消息似乎表明我只能在构造函数中使用“基本初始化程序”;但这不是我想要的。处理我在代码中遇到的问题的最佳方法是什么?
谢谢!
下面的代码:
SM130.h
#ifndef SM130_h
#define SM130_h
#include<Arduino.h>
#include <WConstants.h>
class SM130 {
public:
SM130();
void connect(int RX, int TX);
void check_for_notag();
void halt_tag();
void parse_tag();
void print_serial();
void seek_tag();
void set_flag();
private:
int rfid_flag;
int data[11];
SoftwareSerial rfid;
};
#endif
SM130.cpp
#include <WProgram.h>
#include <SoftwareSerial.h>
#include "SM130.h"
SM130::SM130() {
rfid_flag = 0;
}
void SM130::connect(int RX, int TX) : rfid(RX, TX) {
if(!Serial.available()) {
Serial.begin(9600);
}
Serial.println("Connecting to SM130 RFID Reader...");
//rfid = SoftwareSerial(RX, TX);
rfid.begin(19200);
if(rfid.available()) {
rfid.println("Connected to SM130 RFID Reader!");
}
delay(10);
}
void SM130::check_for_notag() {
seek_tag();
delay(10);
parse_tag();
set_flag();
if(rfid_flag == 1) {
seek_tag();
delay(10);
parse_tag();
}
}
void SM130::halt_tag() {
rfid.write(0xFF);
rfid.write((byte)0x00); //manual typecasting needed for 0x00 to differentiate it from null pointer -- silly c compilers!
rfid.write(0x01);
rfid.write(0x93);
rfid.write(0x94);
}
void SM130::parse_tag() {
while(rfid.available()) {
if(rfid.read() == 0xFF) {
for(int i = 1; i < 11; i++) {
data[i] = rfid.read();
}
}
}
}
void SM130::print_serial() {
if(rfid_flag == 1) {
Serial.print(data[5],HEX);
Serial.print(data[6],HEX);
Serial.print(data[7],HEX);
Serial.print(data[8],HEX);
Serial.println();
}
}
void SM130::seek_tag() {
//insert hex tags here
rfid.write(0xFF);
rfid.write((byte)0x00); //manual typecasting needed for 0x00 to differentiate it from null pointer -- silly c compilers!
rfid.write(0x01);
rfid.write(0x82);
rfid.write(0x83);
}
void SM130::set_flag() {
if(data[2] == 6) {
rfid_flag++;
}
if(data[2] == 2) {
rfid_flag = 0;
}
}
我的arduino草图:
#include <SM130.h>
void read_serial();
SM130 rfidReader;
void setup() {
//Connect to computer
Serial.begin(9600);
//connect to SM130
rfidReader.connect(7,8);
}
void loop() {
read_serial();
}
void read_serial() {
rfidReader.seek_tag();
delay(10);
rfidReader.parse_tag();
rfidReader.set_flag();
rfidReader.print_serial();
delay(100);
}