Page 1 of 1

Question on Serial port with hardware Lib (arduino IDE)

Posted: Fri Oct 30, 2020 1:09 pm
by Aldo_Lodos
Hello,
i'm trying a simple sketch to receive date on a 1200 bauds serial port using the hardware.h library
The data sent are a continous serie of 3 bytes espaced by 50 ms of silence
My receive process on the ESP32 (TTGO T-Display) is as follow :

Code: Select all

#include <HardwareSerial.h>

#define SERIAL_PRINTHEX(x)       if (x < 16) Serial.print("0"); Serial.print (x,HEX)

HardwareSerial MySerial1(1);

#define RX1     15
#define TX1     17 

void setup() {
  Serial.begin(115200);
  Serial.println("ESP32 Serial Test");
  MySerial1.begin(1200 ,SERIAL_8N1, RX1, TX1);
}

long m;
int nbr;
byte Buf[100];
int Bufsize;

void loop() {    
  
  nbr = MySerial1.available();
  if (nbr > 100) nbr = 100;
  
  for (int i=0; i<nbr; i++) {
          Buf[i] = MySerial1.read();
          Serial.println();
          Serial.print(millis());
          Serial.print("  ");
          Serial.print(millis()-m);
          m = millis();
          Serial.print("  Receive_on_port_1 ");;
         SERIAL_PRINTHEX(Buf[i]);  
  }  
}
and here what I get on the monitor:

213 47 Receive_on_port_1 11
213 0 Receive_on_port_1 12
213 0 Receive_on_port_1 13
266 53 Receive_on_port_1 11
266 0 Receive_on_port_1 12
266 0 Receive_on_port_1 13
313 47 Receive_on_port_1 11
313 0 Receive_on_port_1 12
313 0 Receive_on_port_1 13
366 53 Receive_on_port_1 11

I would like to read the bytes one by one but instead the library seems to wait for the 3 bytes before returning a non zero value in MySerial1.available()

Is there a kind of tempo used by the library to determine that the reception is finished ?
Is there a parameter to modify to change this behaviour ?
or is there an other library which allow to receive the bytes one by one ?

Thanks for your help

Re: Question on Serial port with hardware Lib (arduino IDE)

Posted: Fri Oct 30, 2020 2:27 pm
by ORSO2001
hi...have you tried a simple:

if(MySerial1.available()){
byte a = MySerial1.read();
Serial.print(a);
}

Re: Question on Serial port with hardware Lib (arduino IDE)

Posted: Fri Oct 30, 2020 7:01 pm
by Aldo_Lodos
Yes I did with this code

Code: Select all

long m;
void loop() {    
          
  if (MySerial1.available()) {
          SERIAL_PRINTDEC(millis()-m);
          m = millis();
          Serial.print("  ");
          Serial.print(MySerial1.available());
          byte x = MySerial1.read();
          Serial.print("  ");
          SERIAL_PRINTHEX(x);  
          Serial.println();
  }  
}
here is the result

Code: Select all

54  3  11
 0  2  12
 0  1  13
46  3  11
 0  2  12
 0  1  13
54  3  11
 0  2  12
 0  1  13

Re: Question on Serial port with hardware Lib (arduino IDE)

Posted: Fri Oct 30, 2020 9:41 pm
by Aldo_Lodos
I tried to send 120 bytes in a row,
MySerial1.available () waits to receive the first 112 bytes before returning a value > 0 (112 in fact),
which means almost 1s delay before to be able to read the first byte !
IS there a way to change this behaviour and to read the bytes as they arrive ?

1 4 11
1 3 12
1 2 13
1 1 11
827 112 12
0 111 13
0 110 11
0 109 12
0 108 13
0 107 11
0 106 12
0 105 13
0 104 11
0 103 12
.....

Re: Question on Serial port with hardware Lib (arduino IDE)

Posted: Sun Nov 01, 2020 12:27 am
by Aldo_Lodos
the explaination is :

The Serial() driver code uses the 128 byte hardware fifo's of the UART. There are two case that trigger the emptying of the Fifo.:

The UART hardware receives 112 or more bytes.
Or, if the UART sees the receive pin inactive for two byte periods it triggers an interrupt to empty the fifo.

The Serial() object only sees the receive queue. So until either of those two cases happen, the data is only in the hardware Fifo.

More infos here : https://gitter.im/espressif/arduino-esp ... 4144909c85

Re: Question on Serial port with hardware Lib (arduino IDE)

Posted: Tue Nov 03, 2020 9:45 am
by ORSO2001
thanks for the info...