首页 > 解决方案 > 通过 USB 使用 Raspberry Pi 3 读取多个 arduino

问题描述

我有 3 个带有 RC522 rfid 阅读器的 arduino。它们每个都有自己的电源,并通过 USB 端口连接到树莓派 3。在 python 中运行阅读器时,我得到的结果非常不一致。看起来所有 3 个都在阅读,但循环做了一些奇怪的事情。有时芯片代码在移除芯片后会不断重复,有时它会正常工作。arduino 的奇怪行为似乎也没有任何一致性。

任何帮助将不胜感激!!!

这是 arduino 代码(相同的代码被复制到每个 arduino,但初始 println 表示连接了哪个 arduino)。

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         5          // Configurable, see typical pin layout above
#define SS_PIN          53         // Configurable, see typical pin layout above
#define MOSI_PIN        51
#define MISO_PIN        50
#define SCK_PIN         52

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

String read_rfid;

void setup() {
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

  Serial.println(F("The Toy Maker's Sanctuary RDIF Reader 1 Online."));
  //Serial.println(F("Using the following key:"));
  //printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}

void loop() {

  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! rfid.PICC_IsNewCardPresent())
      return;

    // Select one of the cards
    if ( ! rfid.PICC_ReadCardSerial())
      return;

    dump_byte_array(rfid.uid.uidByte, rfid.uid.size);
    Serial.println(read_rfid);
}

/*
   Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
}

这是python代码

import serial

ser0=serial.Serial("/dev/ttyACM0", 9600, timeout=1)
ser1=serial.Serial("/dev/ttyACM1", 9600, timeout=1)
ser2=serial.Serial("/dev/ttyACM2", 9600, timeout=1)

ser0.baudrate=9600
ser1.baudrate=9600
ser2.baudrate=9600

read_ser0=""
read_ser1=""
read_ser2=""

while True:
    read_ser0=ser0.readline()
    print("0: ",read_ser0)
    read_ser1=ser1.readline()
    print("1: ",read_ser1)
    read_ser2=ser2.readline()
    print("2: ",read_ser2)

    read_ser0=""
    read_ser1=""
    read_ser2=""

标签: pythonarduinoraspberry-pi

解决方案


推荐阅读