Increase UART Rx buffer

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
splitfile
Posts: 9
Joined: 11 Jul 2019 14:04

Increase UART Rx buffer

Post by splitfile »

Hi!

I'm reading power meter data from my power meter (RJ12 P1 port). The meter sends data every 10 seconds.
I however think that I lose packets (dropped packets). Data is received at baud rate 115200. I have a feeling that Z-Uno are unable to keep up reading data from the Rx-buffer before it is overwritten..

Every burst of data is approximately 500-1000 bytes. Is there any way to increase the UART Rx buffer?

This is my code.

Code: Select all

#define DEBUG       1 // 1=On, 0=Off

char incomingByte = 0;   // for incoming serial data
int numberOfByte = 0;   // count number of bytes received
char buffer[500];


void setup() 
{
#ifdef DEBUG
  Serial.begin();
  Serial.println("Inside setup");
#endif

Serial1.begin(115200);

}

void loop()
{
  //delay(100); //sleep 1s
    
  while (Serial1.available() > 0)
  {
    // read the incoming byte:
    incomingByte = (char)Serial1.read();
    
    buffer[numberOfByte] = incomingByte & 127;
    numberOfByte++;
  }

  if (numberOfByte > 0)
  {
    Serial.println("");
    Serial.print(numberOfByte, DEC);
    Serial.println(":RECEIVE:");
    
    for (int i=0; i<numberOfByte; i++)
    {
      //Serial.print(buffer);
      Serial.print(buffer[i]);
    }
    numberOfByte = 0; //reset
    //Serial1.flush();
  }
}
User avatar
PoltoS
Posts: 7601
Joined: 26 Jan 2011 19:36

Re: Increase UART Rx buffer

Post by PoltoS »

Z-Uno uses DMA for UAR, but the memory is pretty limited. There is no 1000 bytes of SRAM for that ;(
splitfile
Posts: 9
Joined: 11 Jul 2019 14:04

Re: Increase UART Rx buffer

Post by splitfile »

Thanks for answering!

Is it possible to increase the buffer a little bit?

Is my code looking ok? Do you see any areas of improvement?

I mean, shouldn't Z-uno be able to read fast enough from UART Rx without getting dropped packages? It is more likely that I am doing something wrong :-)

This is an example message that comes every 10 seconds:

Code: Select all

/ELL5\253833635_A

0-0:1.0.0(201020085222W)
1-0:1.8.0(00001605.055*kWh)
1-0:2.8.0(00000000.131*kWh)
1-0:3.8.0(00000003.642*kvarh)
1-0:4.8.0(00000185.707*kvarh)
1-0:1.7.0(0006.000*kW)
1-0:2.7.0(0000.000*kW)
1-0:3.7.0(0000.200*kvar)
1-0:4.7.0(0000.470*kvar)
1-0:21.7.0(0003.172*kW)
1-0:41.7.0(0000.441*kW)
1-0:61.7.0(0002.386*kW)
1-0:22.7.0(0000.000*kW)
1-0:42.7.0(0000.000*kW)
1-0:62.7.0(0000.000*kW)
1-0:23.7.0(0000.000*kvar)
1-0:43.7.0(0000.200*kvar)
1-0:63.7.0(0000.000*kvar)
1-0:24.7.0(0000.222*kvar)
1-0:44.7.0(0000.000*kvar)
1-0:64.7.0(0000.247*kvar)
1-0:32.7.0(234.4*V)
1-0:52.7.0(233.3*V)
1-0:72.7.0(235.1*V)
1-0:31.7.0(013.6*A)
1-0:51.7.0(002.0*A)
1-0:71.7.0(010.2*A)
!80FF
Thanks!
MagistrDEV
Posts: 1
Joined: 01 Sep 2021 20:29

Re: Increase UART Rx buffer

Post by MagistrDEV »

you can use the following UART settings

Serial.begin(speed, RX_pin, TX_pin, *buffer, len) // to set your buffer

Note that the buffer must be larger than the received packet.
In addition, for guaranteed reception, packets of 1024 bytes should not arrive faster than 350ms.

your code will look like this:

Code: Select all

#define DEBUG       1 // 1=On, 0=Off

char incomingByte = 0;   // for incoming serial data
int numberOfByte = 0;   // count number of bytes received
char uart_buffer[1024]; // buffer for UART DMA
char buffer[1024];


void setup() 
{
  #ifdef DEBUG
    Serial.begin();
    Serial.println("Inside setup");
  #endif
  Serial1.begin(115200, RX1, TX1, &uart_buffer, sizeof(uart_buffer));//set our buffer
  bzero(buffer,sizeof(buffer));
}

void loop()
{
  //delay(100); //sleep 1s
   int ff = 0;
  while (Serial1.available() > 0)
  {
    // read the incoming byte:
    buffer[numberOfByte] = ((char)Serial1.read() & 127);
    numberOfByte++;
  }
  buffer[numberOfByte] = 0;
  if (numberOfByte > 0)
  {
    Serial.println("");
    Serial.print(numberOfByte, DEC);
    Serial.println(":RECEIVE:");
    Serial.println(buffer);
    numberOfByte = 0; //reset
  }
}
}
Post Reply