Serial write lag problem

Hi all,

my sketch is reading same analog values, calulate things and the results are send out with serial write.
In general this is not a problem.
I measured the time for runnig the loop and found out that it takes some ms like 3,7,4 9,2... but than I read 782ms and again low values and after that again one high value again and so on.
I'm wondering why.

My idea was to write my data every 300ms, speed is 115200 and the message size is 20920 bit.

I only need this data every 300ms written to serial but I need the loop running without this 780ms lag because of a secure function where I measure somthing and if the value is too high to switch of a relay. With these lag's it feels not good for me.

Here a peace of my sketch:

skend = millis() - skstart; //At Loop start skastart = millis();  to get the time for the loop till serial write
static unsigned long writedelay = millis();
  
if (millis() - writedelay >= 300) {  //Serial write every 300ms

    //Data to transmit to Raspberry PI4
  State state = {
    .pF = PF,
    .pR = PR,
    .vs = VS,
    .ad = AD,
    .ef = ef,
    .te = te,
    .ba = ba,
    .vd5 = VD5,
    .vb = VB,
    .vd12 = VD12,
    .vdc3 = VDC3,
    .ps = ps,
    .fa = fa,
    .at = at,
    .er = er,
    .skend = skend
  };


Serial.write("STAT:");
  int size = sizeof(State);
  char* st = (char*) malloc(size);
  memmove(st, &state, size);
  Serial.write(st, size);
  Serial.write("\n");
  free(st);
  Serial.flush();
  writedelay = millis();
} //End of Loop

Any ideas?

Please post the full sketch so that we can see what else might be holding things up

Sorry not possible, the sketch is part of a project and I don't want to make this code public.

It is like this in ms:
3
7
782
5
4
788
3
8
778
5
3
781
etc.

For me it seems like writing two time fast and 3rd time it needs time to do what ever.

So if my code shown above is not the problem and serial is not the problem with delay in general after or before writing I can take this part as good and search on the sketch before writing.
Of course I did this, but didn't find a delay.

Write a simple demo sketch that demonstrates the problem.

Be aware that Serial.write and Serial.print are basically blocking functions; your statements store data in a buffer that is emptied in the background. If you write faster to the buffer than it can be emptied, your sketch will slow down.

Why? Are we spending our time making you rich? I'm very much prepared to sign a NDA and next help you (at a price).

@sterretje

Why? Are we spending our time making you rich? I'm very much prepared to sign a NDA and next help you (at a price).

Not really friendly ?!

No it is for a private project and this is 90% technical, I spend years and money for prototyping and shared my documentation, PCB etc.. Working on that code was hard, I'm technican and never learnd coding. So I dont want to share everything for other peoply in Russia and China to copy my work again and earning money with my ideas.

If asking for help means that I have to publish a complete project and not only the part where I think there could be a problem, I'll will find out on my own. Maybe with much more time, but this is also ok. Thanks!

You couldn't even be bothered to tell us what State is, so why are you surprised?

Do you print anything anywhere else in the code? A full serial send buffer would definitely slow things down.

The code seems very convoluted. Allocating space for the temporary buffer implies the size of State is unknown, but you are declaring a variable of type State and assigning values as if you know the structure of State.

As already mentioned, it's perfectly acceptable for you to have someone sign an NDA before showing them your code. Then, you can PAY THEM to help you.

An alternative would be to post a complete MRE. Then, someone may be willing to help you for free.

This is an open source forum, so why are you bothering to post here?

Hire a consultant for your top secret and/or commercial product. In fact, you can do so here, by asking the moderator to move your post to the Jobs and Paid Consultancy forum section.

20920 bits is 2615 bytes. Assuming your serial configuration is 115200 8N1 then each byte of data is 10 bits to transmit. Therefore you are really transmitting 26,150 bits. At 115200 that takes about 227 ms since Serial.write() is blocking and you use Serial.flush() to wait for the transmission to complete. Leaving about 73ms for other processing.

Without seeing a representative sketch and knowing which Arduino you are using it is difficult to diagnose the issue.

As said, write a small demo that exhibits the problem.