AnswerBun.com

Wait between UART frames

Arduino Asked by Martin.G on September 25, 2021

I have to reproduce a signal (of 6 UART frames) which have 950 µs delay between each of the frames. Than, a delay of 37ms at the end of those frames.

Everything going right except the delay between each frame…The baud rate is at 9600.

Here is my current code :

  Serial1.write(sw_binary_train_1);
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_2);
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_3);
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_4);
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_5);
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_6);
  delay(37);

2 Answers

It's not strange at all. Serial doesn't block while it sends. When you call write that pushes the byte into the send buffer and that's all. Then the code moves on to the next instruction, the delay(950) and during that time the serial data is actually being sent. It takes about 1 millisecond for that to happen.

This solution works as long as the send buffer doesn't have anything else in it that's slowing you down and as long as you don't change the baud rate. If either of those things happen then you need to recalculate that time.

A better solution would be:

  Serial1.write(sw_binary_train_1);
  Serial1.flush();
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_2);
  Serial1.flush();
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_3);
  Serial1.flush();
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_4);
  Serial1.flush();
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_5);
  Serial1.flush();
  delayMicroseconds(950);
  Serial1.write(sw_binary_train_6);
  Serial1.flush();
  delay(37);

The flush blocks until the send buffer is empty again. That way you don't have to account for that time and all you need the delay for is the time between transmissions.

Even better would be if those things were in an array so you didn't have to keep repeating yourself. Anytime you catch yourself putting numbers on variable names what you really want is an array so that you can use those numbers in the program:

for (int i=0; i<6; i++){
  Serial1.write(sw_binary_train[i]);
  Serial1.flush();
  delayMicroseconds(950);
}
delay(37)

looks much better.

Answered by Delta_G on September 25, 2021

set delayMicroseconds(1900) do a delay of 950µs between each frames...Strange but works !

Answered by Martin.G on September 25, 2021

Add your own answers!

Related Questions

IMU (MPU9250) and Ticker on ESP32

1  Asked on December 10, 2020 by r-ho

     

Where this function is defined?

1  Asked on December 7, 2020 by ravi-s

 

Ultrasonic Sensor(HC-SR04) recording random distance

1  Asked on December 1, 2020 by shubham-bajaj

         

How to save data in arduino when it’s power goes off

3  Asked on December 1, 2020 by user63158

     

Max7219 7-segment display not working

0  Asked on November 29, 2020 by elayer213

   

Unresolved reference to public function from library

1  Asked on November 6, 2020 by omar_hussein

 

SG 90 servos randomly spinning

0  Asked on October 25, 2020

       

void loop() stops running after an hour or so

1  Asked on October 22, 2020 by imrobert7

 

Multiple sensors I2C stop working suddenly

0  Asked on October 6, 2020 by zaffresky

   

Is it possible to exit a loop on the change of an input state?

1  Asked on September 15, 2020 by jabawokjayuk

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir