Random symbols at start of serial buffer?

Hi All,

I have a weird bug in my code where I get a bunch of random symbols at the start of my serial buffer. From Processing I write to serial using the following method:

void sendData(char type, float[] data) {
    write(3);
    String send = "###" + type + ";" + join(str(data),";") +";";
    s.write(send);
    println(send);
  }

And then in Arduino

while(Serial.available() > 0) {
	Serial.readBytesUntil('\n',bytes,60);
}
for(int i=0;i<60;i++) {
	Serial.write(bytes[i]);
}

I am running the Arduino via Processing, and get back this from the Arduino (from the code above):

I pasted this as an image because for some reason Processing won't let me copy the output. Anyways, normally I don't put the ### but tried to use it as a remedy. It makes no sense to me that all these other random characters are at the beginning of the buffer and even separates the #s. Anyone have any idea why this could be? I do Serial.flush after reading the bytes each time. Note: the numbers are correct after the third #

Try giving send a type char.

Well write wouldn't accept char[] so I tried converting it to byte[] as shown, removing the ### this time:

String sendS = type + ";" + join(str(data),";") +";";
    byte[] send = sendS.getBytes();
    s.write(send);
    println(send);

Unfortunately this didn't work. Processing thinks its sending this array of bytes:

[0] 80
[1] 59
[2] 45
[3] 48
[4] 46
[5] 57
[6] 54
[7] 49
[8] 56
[9] 56
[10] 51
[11] 53
[12] 52
[13] 59
[14] 49
[15] 46
[16] 48
[17] 51
[18] 53
[19] 57
[20] 52
[21] 55
[22] 51
[23] 59
[24] 45
[25] 48
[26] 46
[27] 57
[28] 49
[29] 53
[30] 57
[31] 57
[32] 57
[33] 55
[34] 59

Which is indeed what the number should be (note this number isn't necessarily the same as in the first post). However, from Arduino I now receive:

I've googled a lot on this issue and haven't found anything that has helped (as in I've tried what was mentioned but nothing has changed, including using Serial.flush())

I've googled a lot on this issue and haven't found anything that has helped (as in I've tried what was mentioned but nothing has changed, including using Serial.flush())

Processing sends something. The Arduino gets that, and sends something back. What you get back is not what you expect, so you assume the problem is with the Processing snippet. So, why are you asking about that here?

If you think that the problem is with the Arduino code, where the hell is that code?

I didn't say that I assumed it was Processing, I honestly have no idea what it is. The Arduino code that is important and is outputting the line I mentioned is in my first post... I can post more code but posting all of it would be unnecessary and a lot of lines to read through. Here's how I setup:

void setup() {
  Serial.begin(9600);
}

And then within the loop at this moment is is processing these lines of code for this output:

char bytes[60];
while(Serial.available() > 0) {
	Serial.readBytesUntil('\n',bytes,60);
}
for(int i=0;i<60;i++) {
	Serial.write(bytes[i]);
}

Clearly processing thinks it is sending what it should, and Arduino is the one receiving strange symbols, so I thought it may be an Arduino problem.

The Arduino code that is important

In your opinion. Which is not the same as ours. Ours is that you need to post ALL of your code (or live with your problem).

Often, though, all that is required is to add a small delay after the Serial.begin() call.

I know, I'm just hesitant since it is for research and I'm not sure if I'd get in trouble about IP. Anyways, I actually figured out the issue. Changing

char bytes[60];

To

char bytes[60] = {0};

Removed all the random characters. The fact that the numbers were processed incorrectly was a decimal issue (i.e. 10.34 is 110 + 01 + 30.1 + 40.01) and I fixed that too. Thanks for the advice.