# Serial communication speed issue? how to delay buffer

**URL:** <https://forum.arduino.cc/t/serial-communication-speed-issue-how-to-delay-buffer/1164999>\
**Category:** Programming\
**Created:** [September 3, 2023, 11:10pm UTC](https://forum.arduino.cc/t/serial-communication-speed-issue-how-to-delay-buffer/1164999 "2023-09-03T23:10:51Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![darcy\_d](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darcy\_d](https://forum.arduino.cc/u/darcy_d)\
**Post date:** [September 4, 2023, 12:49am UTC](https://forum.arduino.cc/t/serial-communication-speed-issue-how-to-delay-buffer/1164999/21 "2023-09-04T00:49:38Z")

</div>

Nevermind i realised i had char instead of unsigned char

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [September 4, 2023, 12:58am UTC](https://forum.arduino.cc/t/serial-communication-speed-issue-how-to-delay-buffer/1164999/22 "2023-09-04T00:58:09Z")

</div>

> [@darcy\_d](#):
>
> This is why i am here i dont understand what is wrong with the code?

millis returns an unsigned long..  
don't try to read all the bytes at once, keep track of how many and trigger when you have received what you expect..

try this untested sorry..

```arduino
unsigned char Buffer[9];
unsigned long CurrentTime = millis();
unsigned long LastTime = 0;
const int DelayTime = 1000;
byte recvCount = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
  Clock();
  RecvData();
}

void Clock() {
  CurrentTime = millis();
  if (CurrentTime - DelayTime > LastTime) {
    LastTime = CurrentTime;
    Serial.print("Clock Data: ");
    SendData();
  }
}

void RecvData() {
  if (Serial1.available() )
  {
    unsigned char achar = Serial1.read();
    if (recvCount == 0 && achar != 0x5A) return;
    if (recvCount == 1 && achar != 0xA5) {
      recvCount = 0;
      return;
    }
    if (recvCount == 2 && achar != 0x06) {
      recvCount = 0;
      return;
    }
    if (recvCount == 3 && achar != 0x83) {
      recvCount = 0;
      return;
    }
    Buffer[recvCount] = achar;
    recvCount++;
    if (recvCount == 9) {
      //got all bytes..
      Serial.print("Button Press Data: ");
      SendData();
      recvCount = 0;
    }
  }
}

void SendData() {
  Serial.print("Current Time: ");
  Serial.print(CurrentTime);
  Serial.print(" Buffer: ");
  Serial.print(Buffer[0], HEX);
  Serial.print(", ");
  Serial.print(Buffer[1], HEX);
  Serial.print(", ");
  Serial.print(Buffer[2], HEX);
  Serial.print(", ");
  Serial.print(Buffer[3], HEX);
  Serial.print(", ");
  Serial.print(Buffer[4], HEX);
  Serial.print(", ");
  Serial.print(Buffer[5], HEX);
  Serial.print(", ");
  Serial.print(Buffer[6], HEX);
  Serial.print(", ");
  Serial.print(Buffer[7], HEX);
  Serial.print(", ");
  Serial.print(Buffer[8], HEX);
  Serial.println();
}

```

reads one byte at a time..  
checks for proper header..

good luck.. ~q

---

<div class="post-metadata">

**Author:** ![darcy\_d](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darcy\_d](https://forum.arduino.cc/u/darcy_d)\
**Post date:** [September 4, 2023, 1:03am UTC](https://forum.arduino.cc/t/serial-communication-speed-issue-how-to-delay-buffer/1164999/23 "2023-09-04T01:03:38Z")

</div>

> [@qubits-us](#):
>
> ```arduino
> unsigned char Buffer[9];
> unsigned long CurrentTime = millis();
> unsigned long LastTime = 0;
> const int DelayTime = 1000;
> byte recvCount = 0;
> 
> void setup() {
> // put your setup code here, to run once:
> Serial.begin(115200);
> Serial1.begin(115200);
> 
> }
> 
> void loop() {
> // put your main code here, to run repeatedly:
> Clock();
> RecvData();
> }
> 
> void Clock() {
> CurrentTime = millis();
> if (CurrentTime - DelayTime > LastTime) {
> LastTime = CurrentTime;
> Serial.print("Clock Data: ");
> SendData();
> }
> }
> 
> void RecvData() {
> if (Serial1.available() )
> {
> unsigned char achar = Serial1.read();
> if (recvCount == 0 && achar != 0x5A) return;
> if (recvCount == 1 && achar != 0xA5) {
> recvCount = 0;
> return;
> }
> if (recvCount == 2 && achar != 0x06) {
> recvCount = 0;
> return;
> }
> if (recvCount == 3 && achar != 0x83) {
> recvCount = 0;
> return;
> }
> Buffer[recvCount] = achar;
> recvCount++;
> if (recvCount == 9) {
> //got all bytes..
> Serial.print("Button Press Data: ");
> SendData();
> recvCount = 0;
> }
> }
> }
> 
> void SendData() {
> Serial.print("Current Time: ");
> Serial.print(CurrentTime);
> Serial.print(" Buffer: ");
> Serial.print(Buffer[0], HEX);
> Serial.print(", ");
> Serial.print(Buffer[1], HEX);
> Serial.print(", ");
> Serial.print(Buffer[2], HEX);
> Serial.print(", ");
> Serial.print(Buffer[3], HEX);
> Serial.print(", ");
> Serial.print(Buffer[4], HEX);
> Serial.print(", ");
> Serial.print(Buffer[5], HEX);
> Serial.print(", ");
> Serial.print(Buffer[6], HEX);
> Serial.print(", ");
> Serial.print(Buffer[7], HEX);
> Serial.print(", ");
> Serial.print(Buffer[8], HEX);
> Serial.println();
> }
> 
> ```

That works perfectly , thankyou very much now to go through and learn what you did.

Much appreciated!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 2, 2024, 1:04am UTC](https://forum.arduino.cc/t/serial-communication-speed-issue-how-to-delay-buffer/1164999/24 "2024-03-02T01:04:24Z")

</div>

Closed on March 2, 2024, 1:04am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

[Previous page](https://forum.arduino.cc/t/serial-communication-speed-issue-how-to-delay-buffer/1164999.md?page=1)
