2017-06-05 95 views
0

我正在研究一个需要同时从六个ID-12LA RFID阅读器读取的项目。如何通过循环中的Softwareserial库模仿读取多个RFID阅读器?

我试过通过Sparkfun模拟/数字MUX分线板(CD74HC4067)设置六个通道的读数,但没有运气。我不知道它是否能够进行串行通信,即使我在Bildr.org上阅读过该文件。

但是,我现在试图模拟通过SoftwareSerial库从多个串行端口读取数据。我已经读过,它不能同时读取,但也许一个循环可以模拟同时收听。我试图通过听第一个串口,然后初始化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 
} 

回答

0

我是否正确地嘲笑/模仿RFID通信? 您想同时读取2-6个串行RFID阅读器吗?

你应该做的是不使用ourSerial1.listen();常规。 这实际上不能同时在2个端口上侦听。使自己的子程序,该民意调查的各个端口(如果需要的话,插图中切换您MUX):

  • 创建一个for循环,检查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 
        } 
    }