9 years, 6 months ago.

Can we add Byte Stuffing to MODSERIAL?

A nice library, solves some issues I was having.

A feature that it would be good to include in the library is Byte Stuffing with selectable Control, and Escape char's. If enabled the defaults should match ethernet standards I think.

escape = 0x7D converts to 0x7D5D in the stream control = 0x7E converts to 0x7D5E in the stream

An extra callback could then be used on Rx to say "control char" received. Although this is already covered with the autoDetectChar it would be preferable to have a discrete callback.

A new function would be needed to "send" a control char. A method to enable and disable Stuffing on the fly would also be desirable. This would allow the port to be used in both Stuffed (cooked) and Unstuffed (raw) modes easily

I have a userland implementation, but it would really be better done between the buffer and the port.

Thanks for a great platform

Question relating to:

Out of curiosity, why would this need a specific function, instead of just telling it to send 0x7D5D for example? (eg, 2 putcs).

Also in general it should be fairly straight forward to make a child class of (MOD)Serial which would implement this.

posted by Erik - 25 Sep 2014

Hi Erik On the Receive side it is easy enough to extend the class with a special function.

    int move_unstuffed(char *s, int max, char end=0x7E) {
        int counter = 0;
        char c;
        while(readable()) {
            c = getc();
            if (c == end) break;
            while ((c == 0x7D) && (counter++ < max)) { // if it is an escape char drop it and grab next char
                c=(getc());
                if ((c == 0x5D) || (c == 0x5E)) c=(c|0x20); // if a valid escaped char convert it.
                else if (counter++ < max) c=(getc()); // otherwise drop, get next char, and loop.
            }
            *(s++) = c;
            if (++counter >= max) break;
        }
        return counter;
    }

On the Tx side, the reverse really should be done by putc (as the data comes out of the buffer into the uart) so that all functions (eg: printf) correctly byte stuff too. Without fully reimplementing putc in an wrapper class (and even then I don't know that printf etc will use it) I can't see any way to do this.

posted by David Godfrey 30 Sep 2014

1 Answer

8 years, 5 months ago.

Hi David,

Just in case someone stumbles upon this post, I developed a frame delimiting class that delimits a data buffer with byte stuffing algorithm.

https://developer.mbed.org/users/Overdrivr/code/FrameDelimiter/

I hesitated a lot on the interface, but in the end I figured it would be cleaner to separate logic from transport. This is not exactly what you were looking for, but it does the job.

For decoding incoming serial data:

[Repo not found]

For encoding outgoing serial data:

[Repo not found]