To paraphrase from the documentation I linked to, omitting non-relevant text, such as the form our OP did not attempt(Serial.write(buf, len)):
Description
Writes binary data to the serial port. This data is sent as a byte or series of bytes...
Syntax
Serial.write(val) Serial.write(str)
...
Parameters
... val: a value to send as a single byte. str: a string to send as a series of bytes.
...
The op attempted: Serial.write(' ,', str); (note the space character preceding the comma)
Which is surely not a single byte, nor is it a C-string (of bytes), and my answer addressed that.
On the technical side, yes you are correct, attempting to do Serial.write(' ,'); is ill advised. You would likely get a multi-character character constant [-Wmultichar] warning.
For the curious minds : multicharacter constants do exist in C and C++. It's legit to write 'XYZ'. A multicharacter constant bears the type int and its value is implementation-defined.
Most compilers (but not all) implement multicharacter constants as specified in the B language β the values of successive characters in the constant initialises successive bytes of the resulting integer.
in ASCII, the space is 0x20 and the comma is 0x2C so the int value of ' ,' is 0x202C
when you call Serial.write(' ,'); you are actually calling Serial.write(0x202C');.
As write does not deal with an int type but handles the byte type, the compiler truncates the integer and only keeps the LSB 0x2C. This is the byte that will be sent out over the Serial port and as this is the ASCII code for the comma, you'll see a comma in the terminal. The space will have disappeared.
Frankly, because I absentmindedly tagged you instead of @GoForSmoke . My only excuse is, it was well past my bedtime, which is inadequate at best. Ugh. Sorry for that!
I don't think the baud rate noted in the one screen print is relevant - the OP was trying to demonstrate what happened at bauds above 9600, which that image does show.
The question is still what was going wrong either at the Arduino end, or the PC end, midway through his code execution. Certainly, the baud rate it was right, then suddenly something went wrong, and garbage ensued. I did suspect a serial buffer or other pointer run amuck, or a string lacking a terminating null, and I was trying to sort out what was wrong with his code when I noted the multi-char literal. Thank you, @J-M-L, for clarifying what would happen in the compiler context with that. I suspect we'll never know if the OP has arrived at a solution, as it's been many hours since he's posted on the thread.
Ahhhh. My error began in my misinterpretation of the note
Serial transmission is asynchronous. If there is enough empty space in the transmit buffer, Serial.write() will return before any characters are transmitted over serial.
There is limited time when making a post that i already push (due to my slowness) and have lost posts by taking too long. But I could copy. save in a text file and come back and instead screwed up! In my own rush I read empty space as null chars in the data!
Please forgive. I got that wrong wrong wrong and
THANK YOU THE CLARITY! Sometimes this is how I learn!
quote:
For the curious minds : multicharacter constants do exist in C and C++. It's legit to write 'XYZ'. A multicharacter constant bears the type int and its value is implementation-defined.
Most compilers (but not all) implement multicharacter constants as specified in the B language β the values of successive characters in the constant initialises successive bytes of the resulting integer.
end quote