0

我正在做一个需要同时读取六个 ID-12LA RFID 阅读器的项目。

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

但是,我现在正在尝试通过 SoftwareSerial 库模拟从多个串行端口读取。我读过它不能同时阅读,但也许一个循环可以模拟同时收听。我试图通过监听第一个序列,然后初始化readTag(),然后在该函数完成后,开始监听第二个序列,然后初始化第二个函数。

当仅连接RFID阅读器时,该readTag()功能能够自行阅读,所以这不是问题。

下面是代码。

通过循环功能进行和模拟同时阅读的正确方法是什么?

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
}
4

1 回答 1

0

我是否正确,您只是想模拟/模仿来自 RFID 的通信?您想同时读取 2-6 个串行 RFID 阅读器吗?

您应该做的不是使用 ourSerial1.listen(); 常规。这确实不能同时监听2个端口。制作您自己的子程序,轮询各个端口(如果需要,在两者之间切换您的 MUX):

  • 创建一个检查 xx 次的 for 循环(取决于您的设置,在 100 毫秒内?)

    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
        } 
    }
    
于 2017-06-08T12:39:08.470 回答