我正在从事一个项目,该项目需要同时从六个ID-12LA RFID读者阅读。
我尝试通过Sparkfun模拟/数字MUX突破板(CD74HC4067(设置六个频道的读数,但没有运气。我不知道它是否能够串行交流,即使我在bildr.org上读过。
但是,我现在正在尝试通过软件库从多个串行端口绘制阅读。我读到它不能同时阅读,但是也许循环可以同时聆听。我试图通过收听第一个串行,然后初始化readTag()
,然后在该功能完成后,开始收听第二个序列,然后初始化第二个功能。
readTag()
函数能够单独读取时,仅在RFID读取器上连接时,这不是问题。
以下是代码。
通过循环函数同时读取的正确方法是什么?
void setup() {
Serial.begin(9600);
ourSerial1.begin(9600);
ourSerial2.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
}
void loop() {
ourSerial1.listen();
readTag1();
ourSerial2.listen();
readTag2(); // Only this function works right now, because it is the last serial that was initiated in setup.
}
void readTag1() {
char tagString[13];
int index = 0;
boolean reading = false;
while (ourSerial1.available()) {
int readByte = ourSerial1.read();
if (readByte == 2) reading = true; // Beginning of tag
if (readByte == 3) reading = false; // End of tag
if (reading && readByte != 2 && readByte != 10 && readByte != 13) {
//store the tag
tagString[index] = readByte;
index ++;
}
}
checkTag(tagString); // Check if it is a match
clearTag(tagString); // Clear the char of all value
resetReader(); // Reset the RFID reader
}
void readTag2() {
char tagString[13];
int index = 0;
boolean reading = false;
while (ourSerial2.available()) {
int readByte = ourSerial2.read();
if (readByte == 2) reading = true; // Beginning of tag
if (readByte == 3) reading = false; // End of tag
if (reading && readByte != 2 && readByte != 10 && readByte != 13) {
//store the tag
tagString[index] = readByte;
index ++;
}
}
checkTag2(tagString); // Check if it is a match
clearTag(tagString); // Clear the char of all value
resetReader(); // Reset the RFID reader
}
我是否要纠正您只想从RFID嘲笑/模仿通信?您想同时读取2-6个串行RFID读者?
您应该做的是不使用ureserial1.listen((;常规。这确实不能同时在两个端口上听。制作自己的子例程,那该端端口(如果需要的话,将您的MUX介入之间(:
-
创建一个检查xx时间的循环(取决于您的设置,在100ms期间?(
for (int i = 0, i < 1000, i++) //first part of loop { if (Serial.available()) { // do your decoding of port 1 here } } for (int i = 0, i < 1000, i++) //second part of loop { if (Serial1.available()) //other port { // do your decoding of port 1 here } }