AntonioVillanueva

Envio y recepcion de estructuras con Stream y libreria SoftwareSerial arduino

Oct 28th, 2021 (edited)
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. /* Test de la libreria  SoftwareSerial en arduino nano  A.Villanueva
  2.  *  En este caso se envia y se recibe una estructura
  3.  *  utiliza stream sobre SoftwareSerial
  4.  *  
  5.  */
  6.  
  7. #include <SoftwareSerial.h>
  8. #include <Stream.h>
  9.  
  10. //Invertir cables entre los arduinos TX1-->RX2 ,RX1<--TX2    
  11. //#define PIN_TX 11 //D11
  12. //#define PIN_RX 10 //D10
  13. #define PIN_TX 5 //D11
  14. #define PIN_RX 4 //D10
  15. //************************************************************************************
  16. struct Estructura{
  17.   unsigned char num;
  18.   unsigned char txt[256];//Establezco un buffer de 256 bytes
  19. };
  20. //************************************************************************************
  21. SoftwareSerial rs232(PIN_RX, PIN_TX); // RX, TX my serial
  22. unsigned char dato;
  23. Estructura estructuraTX;
  24. Estructura estructuraRX;
  25. //************************************************************************************
  26. void setup() {
  27.  
  28.   pinMode(PIN_TX,OUTPUT);//TX1 --> rx2
  29.   pinMode(PIN_RX,INPUT);//RX1 <-- tx2
  30.  
  31.  
  32.   Serial.begin(9600);//Conexion USB hacia PC
  33.   while (!Serial) { }//Espera conexion usb DEBUG
  34.  
  35.   rs232.begin(9600);//Inicia puerto bis con SoftwareSerial
  36.  
  37. }
  38. //************************************************************************************
  39. void loop() {
  40.   //Emisor TX dato simple
  41.   //TX(dato++);
  42.  
  43.   //Receptor RX dato simple
  44.    //RX();
  45.  
  46.   //Envio y recepcion de structs utilizando stream y SoftwareSerial
  47.   char *txt="algo volo sobre el nido del cuco .Pero podia no volar";
  48.   datosStruct(&estructuraTX,69,txt, strlen (txt)  );//Escribe datos en la estructura  
  49.   Serial.print("Tam estructura ");
  50.   Serial.print (sizeof(estructuraTX));
  51.  
  52.   //TX Estructura
  53.   //size_t enviaStruct(Stream *stream,const void *estructura ,uint16_t tam){
  54.   enviaStruct(&rs232,&estructuraTX ,sizeof(estructuraTX) );
  55.  
  56.   //RX Estructura
  57.   recibeStruct (&rs232,&estructuraRX,sizeof(estructuraRX));
  58.   //Ver Estructura
  59.    verStruct(&estructuraRX,sizeof(estructuraRX));
  60.   delay (8000);
  61. }
  62. //************************************************************************************
  63. //Emision de un dato simple tipo unsigned char
  64. void TX (unsigned char dato){//Escribe en el puerto serie bis
  65.   Serial.print("TX = ");
  66.   Serial.print(dato);
  67.   Serial.print(" , ");
  68.   Serial.print(dato,DEC);
  69.   Serial.print(" , ");
  70.   Serial.println(dato,HEX);    
  71.   rs232.write (dato);//Escribe dato en puerto SoftwareSerial
  72.   delay(2000);//Retardo 2s
  73. }
  74. //************************************************************************************
  75. //Recepcion de un dato simple tipo unsigned char
  76. unsigned char RX (){//Recibe desde el puerto serie SoftwareSerial
  77.   rs232.listen();//Pasa a la escucha  SoftwareSerial
  78.  
  79.   while (rs232.available ()){//Hay datos en SoftwareSerial ?
  80.     Serial.print("RX = ");
  81.     Serial.println (rs232.read());//Lee un dato y envia en el puerto serie USB
  82.   }
  83. }
  84. //************************************************************************************
  85.  
  86.  //Copia datos en la estructura ,esta funcion se debe adaptar a cada estructura
  87. void datosStruct(Estructura *estructura ,unsigned char num,char* txt,size_t tam){
  88.       memcpy(estructura->txt,txt,tam);//Copia txt
  89.       estructura->num=num;  
  90. }
  91.  
  92. //************************************************************************************
  93.  
  94. //Muestra una estructura en el puerto serie USB por defecto de arduino...Serial
  95. void verStruct(Estructura *estructura,size_t tam){
  96.   Serial.println();
  97.   Serial.print(" lenght = ");
  98.   Serial.print (strlen (estructura->txt));//Tamano de la estructura debug
  99.   Serial.print(" num =");
  100.   Serial.print(estructura->num);//Dato num dentro de la estructura
  101.   size_t i=0;
  102.   //Muestra, envia por el Serial USB la parte char* de la estructura
  103.   do{
  104.     Serial.print ((char) estructura->txt[i]);
  105.   }while ( estructura->txt[i++]!=0 &&  (i < tam ));
  106.   Serial.println ();
  107. }
  108.  
  109. //************************************************************************************
  110.  
  111. //Envia estructura , parametros  &SoftwareSerial , La estructura a enviar, y su tamano
  112. size_t enviaStruct(Stream *stream,const void *estructura ,uint16_t tam){
  113.   size_t nbytes = stream->write((uint8_t *) estructura, tam);
  114.   return nbytes;
  115. }
  116.  
  117. //************************************************************************************
  118.  
  119. //Recibe estructura parametros puntero a la estructura y su tamano
  120. size_t recibeStruct (Stream *stream,const void *estructura,uint16_t tam){
  121.    size_t nbytes = stream->readBytes((uint8_t *) estructura, tam);
  122.   return nbytes;
  123. }
  124.  
  125. //************************************************************************************
  126.  
  127. //Mira si hay datos en el rs232 si es asi recupera la estructura RX
  128. void RX_Struct (Stream *stream,const void *estructura,uint16_t tam){
  129.   if (rs232.available()){//Hay datos en SoftwareSerial port ?
  130.     size_t nbytes=recibeStruct (stream,estructura,tam);//Recibe la estructura
  131.     Serial.print("Datos recibidos = ");
  132.     Serial.println(nbytes,DEC);    
  133.   }
  134. }
  135.  
Add Comment
Please, Sign In to add comment