Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème avec multiple SoftwareSerial

SoftwareSerial.h

    12 juillet 2019 à 14:00:02

    Bonjour à tous,

    j'ai un soucis avec mon code. Je veux lire plusieurs tag RFID 125KHZ avec un seul uno.

    Voici mon code :

    #include <SoftwareSerial.h> //for serial communication on any digital pins. 
           
    // Tag test : 2B004545C6ED
    // Tag reine :
    // Tag fou :
    // Tag tour :                            
    // Tag cavalier :             
    
    //Stored_Tag_ID1="";
    
                               
    const int RFID1_TX_Pin = 2;  // the TX pin that the RDIF Sensor is attached
    const int RFID2_TX_Pin = 4;  // the TX pin that the RDIF Sensor is attached
    const int RFID3_TX_Pin = 6;  // the TX pin that the RDIF Sensor is attached
    const int RFID4_TX_Pin = 8;  // the TX pin that the RDIF Sensor is attached
    String Parsed_Tag_ID1,Parsed_Tag_ID2,Parsed_Tag_ID3,Parsed_Tag_ID4, Stored_Tag_ID;
    char c;
    
    SoftwareSerial RFID1(RFID1_TX_Pin , 3);  // RX, TX for serial communication on any digital pins.
    SoftwareSerial RFID2(RFID2_TX_Pin , 5); 
    SoftwareSerial RFID3(RFID3_TX_Pin , 7); 
    SoftwareSerial RFID4(RFID4_TX_Pin , 9); // RX port : 2 -- TX port : 255 (do not need any TX port)
     
    void setup()  
    {  
      //Setup serial
      Serial.begin(9600);
    
      //Setup RFID serial
      RFID1.begin(9600);  
      RFID2.begin(9600); 
    //  RFID3.begin(9600); 
    //  RFID4.begin(9600); 
    }
    //The setup END
    ///////////////////////////////////////////////////////////////////////////
     
    ///////////////////////////////////////////////////////////////////////////
    //The main LOOP BEGIN
    void loop(){
      
    RFID1.listen();
       
    if ( RFID1.isListening() ) { //Tests to see if requested software serial port is actively listening.  
        while( RFID1.available() > 0 ){ //Get the number of bytes (characters) available for reading from a software serial port. 
                                       //This is data that's already arrived and stored in the serial receive buffer. 
          c=RFID1.read(); //Reads one char/byte at a time
          Parsed_Tag_ID1 += c; //Store the char into the Parsed_Tag_ID string
          if ( Parsed_Tag_ID1.length() == 14 ) { //The TAG ID has 14 chars in total
            if ( (Parsed_Tag_ID1[0]==2) && (Parsed_Tag_ID1[13]==3) ) { //If the first char is 2 and the last one is 3 then ...
              Parsed_Tag_ID1 = Parsed_Tag_ID1.substring(1,13); //Delete the 1st and the 13th (last) char
            Serial.println(Parsed_Tag_ID1);  
            }
            
           
          }
        }
    }
    
    
    
    if (Parsed_Tag_ID1=="2B004545C6ED" ){
    Serial.println("Test en place");
    }   
    else
    {
    Serial.println("Test enlevé");
    } 
     
    Parsed_Tag_ID1="";
    
    RFID2.listen();
       
    if ( RFID2.isListening() ) { //Tests to see if requested software serial port is actively listening.  
        while( RFID2.available() > 0 ){ //Get the number of bytes (characters) available for reading from a software serial port. 
                                       //This is data that's already arrived and stored in the serial receive buffer. 
          c=RFID2.read(); //Reads one char/byte at a time
          Parsed_Tag_ID2 += c; //Store the char into the Parsed_Tag_ID string
          if ( Parsed_Tag_ID2.length() == 14 ) { //The TAG ID has 14 chars in total
            if ( (Parsed_Tag_ID2[0]==2) && (Parsed_Tag_ID2[13]==3) ) { //If the first char is 2 and the last one is 3 then ...
              Parsed_Tag_ID2 = Parsed_Tag_ID2.substring(1,13); //Delete the 1st and the 13th (last) char
            Serial.println(Parsed_Tag_ID2);  
            }
            
           
          }
        }
    }
    
    
    
    if (Parsed_Tag_ID2=="2B004531D986" ){
    Serial.println("Test en place");
    }   
    else
    {
    Serial.println("Test enlevé");
    } 
     
    Parsed_Tag_ID2="";
    
     
    }

    Il ne fonctionne pas. Avec un seul reader et la ligne RFID2.begin(9600); en commentaire, nickel ! Dès que je décommente, plus aucune lecture de mes tags ...

    Je ne vois pas où est le problème.

    Vous avez une idée ?

    Merci d'avance,

    Thibaut

    • Partager sur Facebook
    • Partager sur Twitter
      18 juillet 2019 à 16:28:52

      Comme tu n'as pas le contrôle sur le moment où les modules RFID envoient des données sur le port série, il faudrait pouvoir lire tous les ports en même temps en permanence, pour ne pas perdre de données, ce que ne permet pas de faire SoftwareSerial.

      Tu pourrais essayer AltSoftSerial comme suggéré sur la page de SoftwareSerial.

      • Partager sur Facebook
      • Partager sur Twitter

      Problème avec multiple SoftwareSerial

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown