11 years, 6 months ago.

Serial Communication String Length Problem

I am trying to send strings across a serial connection but the strings (char arrays) are different lengths which causes me an issue as the receiver does not know how many bits to read.

I was trying to change the length into a character and send that as the first char to be read which would then tell the receiver how many more bits were coming, however my array was 30 chars long which as a char is a record separator. The receiver then waited until the buffer was full before printing to the screen. (buffer is 100).

Is there a problem in my theory or is there a better way of sending this data?

1 Answer

11 years, 6 months ago.

The end of a string is usually recognized by sending a '0x00' byte. That is an unused character code. Alternatively you send '\n' to indicate the end. The receiver somehow needs to check for the maximum number of chars when it has to store the data and somehow it has to deal with overruns (e.g. skip everything until the next 0x00).

Accepted Answer