Is a two byte terminator better than one?

Two ESP32's with HC-12 modules 433MHz.

One way serial traffic. 2400baud. SatNav co-ordinates, sent from a dog collar to a receiving radio, once per second.

The data is a string of ~40bytes, terminated with \r\n

The receiving ESP32 looks for the \r\n as a terminator.

My idea for using two characters, instead of either just \r or \n, is to improve the likelihood of having a 'good' block of data captured.
Since the signal strength can vary from good to zero, having two specific consecutive characters being received correctly as the terminator, is more robust than having just one and (to me) suggests the data integrity of a received block should be better.

Was this a sensible argument?

This has evolved somewhat, into now sending the data three times consecutively and comparing the received data.

Does the same idea of using both \n\r still stack up, (if it ever did) or perhaps now drop to just \r or \n for each block?

I don't have any problems with it not working, just questioning the philosophy.

(Search reveals a lot of posts on serial data issues, but nothing seems to cover this question).

To check data integrity you better use a CRC checksum. It takes all send values into account and if one bit fails the CRC will be different.

A CRC-32 has 4 bytes and would detect almost all errors.

Another step is to see if you must send 40 bytes? Less bytes sent reduces the chance for errors. Drawback might be reduced human readability.

Can you post the schema of the data ?

Additional thought.
Because you mention "\r\n" I assume you're sending text based data. Serial Input Basics - updated might be useful (read the complete topic) to get some ideas. Specifically the example with start- and end marker.

Not really. Unless all your messages are the same length, you can make the first byte supply the count of characters. If they are all the same length, then you will find problems by counting the bytes. In the chance that your special characters may also show up in your data could also cause problems. The byte count or CRCC is the method use for generations of message designs.

Agreed, a CRC is the better fix here. The terminator only tells you where a block ends, it says nothing about whether the bytes inside it are correct. One end byte plus one CRC byte costs the same two bytes of overhead as \r\n, but instead of marginally better framing you get an actual check across the whole payload.

This isn’t new for serial GPS data. NMEA 0183 sentences carry an XOR checksum after the asterisk, covering every character in the sentence, because serial links are prone to bit errors and framing alone was never considered sufficient.

Modbus RTU, on similar noisy async serial links, appends a CRC-16 across the entire frame for the same reason, not a longer stop sequence. A CRC’s structure reliably catches single, double, and burst errors up to its own width across the whole message, which a repeated delimiter can’t do by design, since it only checks two specific bytes.

Given the redundancy already costs three transmissions per reading, a CRC would let you identify which copy is trustworthy instead of relying on majority match, and could eventually let you drop back to two sends since the CRC does the validation work the repetition is currently doing by brute force.

Yes but how much is the received bit error rate actually improved.

CRC code can be found here - GitHub - RobTillaart/CRC: CRC library for Arduino · GitHub

no reason you can't append a checksum to an ascii string terminated with a \n and use readBytesUntil() to read the line of input

get following

rdy
process: HI96
  H 48 - 0x48 ⸮
  I 49 - 0x91 n
  9 39 - 0xca 5
  6 36 - 0x00 ⸮
 pass
process: HI99
  H 48 - 0x48 ⸮
  I 49 - 0x91 n
  9 39 - 0xca 5
  9 39 - 0x03 ⸮
 fail

how much of a checksum do you need for a short string

char s [90];

// -----------------------------------------------------------------------------
void process (
    char *buf)
{
    sprintf (s, "process: %s", buf);
    Serial.println (s);

    unsigned char cs = 0;
    for (unsigned n = 0; n < strlen (buf); n++)  {
        cs += buf [n];

        sprintf (s, "  %c %02x - 0x%02x %c", buf [n], buf [n], cs, ~cs);
        Serial.println (s);
    }
    Serial.println (cs ? " fail" : " pass");            // "HI96"
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (Serial.available ())  {
        char buf [90];
        int  n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        process (buf);
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin   (9600);
    Serial.println ("rdy");
}

unless the checksum equals '\n' ?
chance 1 in 256 ? assuming all 256 values are used?
If only a printable character is allowed there will be 1 in 95 ?

One could write the checksum as ASCII e.g. 0x0425 => "......0425".

One of the most used checksums with readable numbers is the LUHN algorithm, used for verifying bank numbers, credit cards etc. - GitHub - RobTillaart/LUHN: Arduino Library for calculating LUHN checksum · GitHub

With that idea you can not only detect errors but also correct them.

I would use format as this:
4 bytes CRC of following 1..256 chars:
1 byte length of string
0..255 chars string

and after receiving first 5 bytes, I would look at the lenght as first sanity test and will reject anything, witch is not ~40 (depends on you messages say anything under 30 or over 45?), because the data are already mess (invalid lenght is probabelly some other random/defect byte).

Then I would read upto lenght characters (with waiting for next char limited to say 0.1s) and if it would not be the same count as length I would reject it .

Then I would check the CRC and it if does not match I would reject it.

Then I would accept the message as correctly transmitted and I would process it.

the terminator, the '\n' is not returned by readBytesUntil(). The single byte checksum is appended to the string, simply an 8-bit sum of the characters in the string

we tried using checksums to determine the transmit rate: 1/8, 1/4, 1/2, 1 in CDMA phones. I was stunned when we found a case when there was a valid checksum for 3 of the 4 rates. I wouldn't get to carried away with checksum for relatively small amounts of data

So if the CRC equals '\n' == 10 the readUntil() would skip the CRC ...

Good point

Thanks for the feedback.
Some interesting ideas.

I missed some details out. It's been an evolving project, moving from Arduino Pro mini and HC-12 to an ESP32 and nRF25L01, which can only send 32 bytes max per message. So that was the original target. 32 bytes max, sent once per second.

(Because of range issues, I went back to the HC-12 modules, but tried to keep the rest of the code.
The byte count has increased to ~40 as other data has been added).

But.
Each message is of an identical length and only numeric values sent as byte array. (serial.write)

Decimals and other separators are stripped out
IE. Content:- time, date, lat, long

10:03:15 27-05-2026 55.69688 002.50501

Becomes:

0100315270520265569688002505012

The first byte shown here (0) was included as a possible message identifier, in the event of exceeding the 32 byte limit. It is now used for that purpose, identifying first, second & third message, all sent consecutively each second. (Although there is no actual limit on the number of bytes anymore). The final byte shown here (2) contains bit data to flag the polarity of the lat and long values.

Of course, there was no need for any message end identifier when the nRF24L01 was used. It took care of the transmission & reception.

So, back to the HC-12's....
Two bytes added, byte values 13 and 10 for cr/lf (\r\n) end of data identifiers.

Re-construction of the array at the receiver is straightforward. (serial.read).

I considered using crc at one stage, but decided I would like to keep the received data, if at all possible. The problem with using crc is, if its bad, you just toss the message. Hence opting for sending the data 3 times and trying to recover from those, a minimum of lat and long. So that's where the compare is used, on the lat and long data in each of the three messages. If there is some other duff data in each message, which the crc would flag, the message would normally be dumped.

Soooo...
I guess I could use a combination of including crc, and even if all three are bad, still compare the lat and long data and try to recover if there are two the same.

But this doesn't answer the original question, is using \r\n better than just using either \r or \n ?

Ahhhhh...
Now I remember. Because the crc could be a (byte) 10 or (byte) 13, the receive would fail, as someone pointed out.

Thanks.
I think that's answered my question. I do need both \r\n

a terminators helps recognize a complete msg.

but if the msg length is fixed, or can be part of the msg, a preamble maybe to recognize the start of a msg followed by a length byte and the crc aids in determing if both the msg is correct as well as that the start of the msg was recognized correctly.

presumably the problem is synchornizing to the start of the msg.

I did answer that question. See post #6
CRC only tell you if ther is an error, it does not do any error correction, see post #10

Why to reject when you can correct it as indicated in post#10.

Here is the real problem with what you are attempting. By using the RF devices to send/receive, you already have built-in error checking, but you are not using it to identify errors and take care of re-transmission to correct errors. Your devices will send a
NAK when a received message has an error, but you are not checking for that.

Can you give more details on how HC-12 does this?