Missing Serial bytes when receiving from Arduino

I’m getting serial data from an Arduino over USB, and I seem to keep losing random bytes.

In the Arduino, I’m sending data like this:

void SendLedDataToSerial()
{
  for (int l=0;l<MATRIX_SIZE;l++)
  {
    serialLedData[l*3] = r;
    serialLedData[l*3+1] = g;
    serialLedData[l*3+2] = b;
  }
  Serial.write(serialLedData, MATRIX_SIZE*3);
}

When viewing this on the serial monitor in the Arduino IDE, all the bytes appear. However, in a Serial In DAT in Touch I will randomly be missing bytes in the message that comes through.

The baud rate matches between the output and input, and I’m only sending data once every 1/2 second.

Is there a better way to send data besides an array of bytes?

Thanks

You’re sending binary data from Arduino,
so how are you monitoring them in the Serial In DAT?
By default its set to have one row/callback per line (a stream of bytes ending in \n).
So you’ll only see data as specific values (13 or 10) come in, representing new lines.

You could change the Serial In DAT to be Row/Callback Format: One per Byte
Include the bytes column in the output.
And/or use the bytes column in the callback.
That should get you all the bytes.

However, as its a streaming format, you’ll not know where things start/end.

I’d recommend instead changing the arduino to send a line of ascii values terminated by a newline.

That way other apps will know how to parse things properly.

Here’s some more info on that method:

derivative.ca/wiki099/index … le=Arduino

Cheers.

Thanks for the advice.

Unfortunately, I already tried getting the bytes as One per Byte and One per Message. Both choices resulted in missing bytes.

However, I managed to work around this by a combination of changing the baud rate to 57600, working with a terminator byte, and checking for the number of bytes coming in between terminators.

So I just read in every byte to an array, and then check the array size after receiving a byte of 255. If that array size matches what I’m expecting, I assume it’s valid data. It’s not foolproof, but is working for my situation.

It still doesn’t answer why I was losing bytes, but at least it mostly gets around the problem.

And after checking the page you linked to, it looks like I ended up with a similar solution to the example there, so I guess I was on the right track

Hi all,

I’m just starting out with TouchDesigner, and i’m having almost exactly the same problem as this.

I’m just trying to use buttons to trigger the playing of different audio files.
One button is fine.
Serial DAT shows message/byte of that data fine…

added a second button

[code]// print out the state of the button:
Serial.print(buttonState);
Serial.print(‘\n’);

Serial.print(buttonState2);
Serial.print(‘\n’);[/code]

So now i have a table with the two button states on two rows.

1000ms seems stable, but way too slow for what i’m trying to do.

any faster, and the values randomly flip in the rows. (if that makes sense?)…
Data is being dropped or missed.
same as ajk48n, can watch the Arduino serial monitor and i get the correct stream of button state (0/1) while they are pressed.

Is that just the nature of TD? do i need to work in some error correction?
if so, anyone have any ideas on how i should go about it?

let me know if i’m not really explaining myself… and/or i should be starting a new thread instead…

thanks

Are you using the callbacks in the Serial DAT? You can process with python all messages in the right sequence.

There’s an example in Help → Operator Snippets for the Serial DAT, at least in the new Official.

Thanks Greg,

I checked out the Operator Snippets (Very handy)

I didn’t have to modify the default Serial callbacks.
I just sent all my data on 1 line using a . delimiter and then split the cells with the Convert DAT.
works well.

Out of interest i added another line of data for the Arduino to send, as a test

[code] // print out the state of the button:

Serial.print(“.”);
Serial.print(“A.”);
Serial.print(buttonState);

Serial.print(“.”);
Serial.print(“B.”);
Serial.print(buttonState2);
Serial.print(‘\n’);

Serial.print(buttonState2); //just to test
Serial.print(‘\n’); //just to test
delay(10); // delay in between reads for stability[/code]

And again, my ‘string’ of data goes into row 1
second line of buttonState2 goes into row 2

And they randomly start switching places/rows when you get below 1000ms or so…

so i’ll stick to sending all data on one line and splitting it out with the Convert DAT…
but i’m just wondering if there’s some sort of bug in TD, when receiving more than 1 line into the Serial DAT??

thanks again for they reply and tips…

Hello ajk48n,

I posted a similar question in another discussion, but this could be a better place for it, I have around 20 inputs from potentiometers, toggle switches and buttons coming from my Arduino in the format below. The Arduino serial monitor is stable but I am having the same problem you had of losing data in the transfer to TD. My SerialDAT seems to flicker momentarily which is causing a visual being driven by the data to glitch.

Could you explain to me how you read the incoming Serial, assigned it to an array position, checked that it was the expected length, then ignored it if it wasn’t as expected.

This has been troubling be for a week or so now so any suggestions are welcome. Thank you for any help.

[code]// printing the values, " " for delimination in touchdesigner.
Serial.print (FIFOProcess[0].average);
Serial.print ( " " );
Serial.print (FIFOProcess[1].average);
Serial.print ( " " );
Serial.print (FIFOProcess[2].average);
Serial.print ( " " );
Serial.print (FIFOProcess[3].average);
Serial.print ( " " );
Serial.print (FIFOProcess[4].average);
Serial.print ( " " );
Serial.print (FIFOProcess[5].average);
Serial.print ( " " );
Serial.print (FIFOProcess[6].average);
Serial.print ( " " );
Serial.print (FIFOProcess[7].average);
Serial.print ( " " );
Serial.print (FIFOProcess[8].average);
Serial.print ( " " );
Serial.print (FIFOProcess[9].average);
Serial.print ( " " );
Serial.print (toggleState1);
Serial.print ( " " );
Serial.print (toggleState2);
Serial.print ( " " );
Serial.print (buttonState1);
Serial.print ( " " );
Serial.print (buttonState2);
Serial.print ( " " );
Serial.print (state[0]);
Serial.print ( " " );
Serial.print (state[1]);
Serial.print ( " " );
Serial.print (state[2]);
Serial.print ( " " );
Serial.print (FIFOProcess[10].average);
Serial.print ( " " );
Serial.print (state[3]);
Serial.print ( " " );
Serial.print (state[4]);
Serial.print ( " " );
Serial.print (state[5]);
Serial.print ( " " );
Serial.print (state[6]);
Serial.print ( " " );
Serial.print (state[7]);
Serial.print ( " " );
Serial.print (state[8]);

Serial.print (‘\n’);[/code]