AntonioVillanueva

Test de la bibliothèque série Arduino

Oct 12th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Test de la bibliothèque SoftwareSerial sur Arduino Nano, A.Villanueva
  2. *    Dans ce cas, une structure est envoyée et reçue
  3. *    en utilisant le flux avec SoftwareSerial
  4. */
  5.  
  6. #include <SoftwareSerial.h>
  7. #include <Stream.h>
  8.  
  9. //Inverser les câbles entre les Arduinos : TX1 --> RX2, RX1 <-- TX2.    
  10. //#define PIN_TX 11 //D11
  11. //#define PIN_RX 10 //D10
  12. #define PIN_TX 5 //D11
  13. #define PIN_RX 4 //D10
  14. //************************************************************************************
  15. struct Estructura{
  16.   unsigned char num;
  17.   unsigned char txt[256];//Je mets en place un tampon de 256 octets.
  18. };
  19. //************************************************************************************
  20. SoftwareSerial rs232(PIN_RX, PIN_TX); // RX, TX my serial
  21. unsigned char dato;
  22. Estructura estructuraTX;
  23. Estructura estructuraRX;
  24. //************************************************************************************
  25. void setup() {
  26.  
  27.   pinMode(PIN_TX,OUTPUT);//TX1 --> rx2
  28.   pinMode(PIN_RX,INPUT);//RX1 <-- tx2
  29.  
  30.  
  31.   Serial.begin(9600);//Connexion USB vers un PC.
  32.   while (!Serial) { }//En attente de la connexion USB DEBUG.
  33.  
  34.   rs232.begin(9600);//Démarre le port série virtuel (BIS) avec SoftwareSerial.
  35.  
  36. }
  37. //************************************************************************************
  38. void loop() {
  39.     // Émetteur TX simple de données
  40.     // TX(dato++);
  41.  
  42.     // Récepteur RX de données simple
  43.     // RX();
  44.  
  45. // Envoi et réception de structures en utilisant les flux et SoftwareSerial
  46.   char *txt="TEST TX RX ";
  47.   datosStruct(&estructuraTX,69,txt, strlen (txt)  );//Escribe datos en la estructura  
  48.   Serial.print("Taille struct Tx ");
  49.   Serial.print (sizeof(estructuraTX));
  50.  
  51.   //TX Structure
  52.   //size_t enviaStruct(Stream *stream,const void *estructura ,uint16_t tam){
  53.   enviaStruct(&rs232,&estructuraTX ,sizeof(estructuraTX) );
  54.  
  55.   //RX Structure
  56.   recibeStruct (&rs232,&estructuraRX,sizeof(estructuraRX));
  57.   //Regarder structure
  58.    verStruct(&estructuraRX,sizeof(estructuraRX));
  59.   delay (8000);
  60. }
  61. //************************************************************************************
  62. // Transmission of a simple unsigned char type data
  63. void TX (unsigned char dato){// Écrit sur le port série bis
  64.   Serial.print("TX = ");
  65.   Serial.print(dato);
  66.   Serial.print(" , ");
  67.   Serial.print(dato,DEC);
  68.   Serial.print(" , ");
  69.   Serial.println(dato,HEX);    
  70.   rs232.write (dato);// Écrit la donnée sur le port  SoftwareSerial
  71.   delay(2000);//Retard 2s
  72. }
  73. //************************************************************************************
  74. // Réception d'une donnée simple de type unsigned char
  75. unsigned char RX (){// Reçoit depuis le port série SoftwareSerial
  76.   rs232.listen();// Passe en mode écoute pour SoftwareSerial
  77.  
  78.   while (rs232.available ()){// Y a-t-il des données disponibles sur SoftwareSerial ?
  79.     Serial.print("RX = ");
  80.     Serial.println (rs232.read());// Lit une donnée et l'envoie sur le port série USB
  81.   }
  82. }
  83. //************************************************************************************
  84.  
  85.  // Copie les données dans la structure, cette fonction doit être adaptée à chaque structure
  86. void datosStruct(Estructura *estructura ,unsigned char num,char* txt,size_t tam){
  87.       memcpy(estructura->txt,txt,tam);//Copia txt
  88.       estructura->num=num;  
  89. }
  90.  
  91. //************************************************************************************
  92.  
  93. // Affiche une structure sur le port série USB par défaut d'Arduino... Serial
  94. void verStruct(Estructura *estructura,size_t tam){
  95.   Serial.println();
  96.   Serial.print(" lenght = ");
  97.   Serial.print (strlen (estructura->txt));// Taille de la structure en mode débogage
  98.   Serial.print(" num =");
  99.   Serial.print(estructura->num);// Donnée num à l'intérieur de la structure
  100.   size_t i=0;
  101.   // Affiche, envoie via le port série USB la partie char* de la structure
  102.   do{
  103.     Serial.print ((char) estructura->txt[i]);
  104.   }while ( estructura->txt[i++]!=0 &&  (i < tam ));
  105.   Serial.println ();
  106. }
  107.  
  108. //************************************************************************************
  109.  
  110. // Envoie une structure, avec comme paramètres &SoftwareSerial, la structure à envoyer et sa taille
  111. size_t enviaStruct(Stream *stream,const void *estructura ,uint16_t tam){
  112.   size_t nbytes = stream->write((uint8_t *) estructura, tam);
  113.   return nbytes;
  114. }
  115.  
  116. //************************************************************************************
  117.  
  118. // Reçoit une structure, avec comme paramètres un pointeur vers la structure et sa taille
  119. size_t recibeStruct (Stream *stream,const void *estructura,uint16_t tam){
  120.    size_t nbytes = stream->readBytes((uint8_t *) estructura, tam);
  121.   return nbytes;
  122. }
  123.  
  124. //************************************************************************************
  125.  
  126. // Vérifie s'il y a des données dans le RS232, si c'est le cas, récupère la structure RX
  127. void RX_Struct (Stream *stream,const void *estructura,uint16_t tam){
  128.   if (rs232.available()){// Y a-t-il des données dans le port SoftwareSerial ?
  129.     size_t nbytes=recibeStruct (stream,estructura,tam);// Reçoit la structure
  130.     Serial.print("Données reçues = ");
  131.     Serial.println(nbytes,DEC);    
  132.   }
  133. }
  134.  
  135.  
Add Comment
Please, Sign In to add comment