Hey all.
I try to create a multi-arduino project that also communicates with up to three external rs232 targets (via a max232-sop) to perform various control tasks via serialized control words. The first three targets are running on serial1-3 on a mega. For the second remote controlled arduino (probably a micro) I would start a SoftwareSerial connection from two more mega pins. This as background. Now:
I am trying to understand the .readBytes behaviour of the streamclass. My design would be:
-a char array ser_buffer instiated with fixed size
-serial 1-4 (nr 4 is software serial)
The Serial.readBytes man page defines "A 0 means no valid data was found". Which 0? ASCII \0 (the NUL char) or ASCII 0x30 (the zero char)? And if I read to a fixed size array, from what side is it filled if the size of the read characters is less then the size of the array? And what happens to the rest of the destination array? Will it just contain previous values or will the higher indexes receive 0x00 or 0x30? Or is this undefined?
TLDR:
readbytes(buffer,6), buffer[6] contains "abcdef":
read 4 bytes "00OK" =>
a) buffer "00OK\0\0" or
b) buffer "00OKef" or
c) buffer "\0\000OK" or
d) buffer "ab00OK" or
e) buffer "00OK00"?
I suggest that all your data starts with a start-character and ends with an end-character.
Example 3 from The Serial input basics from robin2 use
"<" as the start-character
and ">" as the end-character
So a message looks like this < HELLO >
or your example < abcdef >
If you use start/end-characters they offer some checking if a valid message was received
(of course not the same as CRC)
The length of the message can vary because there is the start/end-character to indicate just what their name says
Thank you, but I also have to communicate with foreign hardware I have no control off, only a rough documentation of their status words. That words use an termination char, but not a start char. I will keep your tutorial however in mind. My workaround would have been to always readbytes to the end char, but I am confused about readbytes behaviour when target array is larger then stream buffer. (And I guess the same question is for readBytesUntil). Is the array overwritten from index 0 to termination of readbytesuntil and the rest of the array is left on the previous values?
Furthermore, as you seem to have experience with the serial com implementation, do you know what data type are the serial ports? I try to iterate through the three hardware serials, but would like to pass the serial ports then to functions, so my thought was to iterate through an array either storing the serial1,2..objects directly or pointers to them after initializing them with the begin. But of course "Serial" is not a class and the parent Stream is an abstract class, so the array cannot be typed stream.
"Abstract class" means that you can't define object with it, but you can use a Stream as a type of pointer to Serial for arrays and function parameters. Many Arduino libraries use Stream as a type of data members, where both Hard and Soft Serial can be used
Personally I wouldn't recommended to use readbytes() and readbytesUntil() at all, because its blocking behaviour. The both functions will stuck until not received a defined number of bytes or for timeout. On busy systems it could be an issue, so I prefer to read the data byte by byte via read() function and process the data immediately.