long String to arduino

Hello,

I know this has been asked a few times but looking for a hack here.
need to send a quite a long string to arduino, about 300 ascii characters.

Im using it to pwm some shiftregisters using the shift-pwm library. GitHub - elcojacobs/ShiftPWM: Arduino Library for software PWM with shift registers

This is not working obviously its too long a string
So heres my code.

Im sending from vvvv.

char packetBuffer[290];

void loop() {
  
if (Serial.available() > 0) 
{
 Serial.readBytes(packetBuffer,numOutputs + 2);// 6 is the packetsize
 
 if( packetBuffer[0]=='<' && packetBuffer[numOutputs + 2 -1]=='>')
 {
   unsigned char val[numOutputs];
   memcpy(val,packetBuffer+1,numOutputs);
///////////Now we have a clean input buffer of 4 bytes to use.

  for ( int i=0; i < numOutputs ; i++ )
    {
     //ShiftPWM.SetRGB(i,val[i*3],val[(i*3)+1],val[(i*3)+2]);
     ShiftPWM.SetOne(i,val[i]);
    }

So my questions
So what is the maximum amount of chars i can send through and what spec of the arduino tells me that.

I dont need an 8 bit resolution really.
Could i just send just the bits i need , and convert in arduino ? maybe four for each value ?

Library is using some timers that i cannot wrap my head around how to convert for mega or Due.
Maybe someone knows ?

Thanks a lot

if (Serial.available() > 0) { Serial.readBytes(packetBuffer,numOutputs + 2);// 6 is the packetsize

this is wrong as you try to read numOutputs+2 bytes while you only know there are more than zero bytes in the buffer (e.g. only 1)

if (Serial.available() >= numOutputs + 2) { Serial.readBytes(packetBuffer,numOutputs + 2);// 6 is the packetsize

would be more robust

why copy the whole packetBuffer into val, you could do

ShiftPWM.SetOne(i,packetBuffer[i+1]);

or somethng like that

The examples in serial input basics should read any length of string as long as you set aside enough space for it.

...R

The examples in serial input basics should do read any length of string as long as you set aside enough space for it.

@PaulS, thank you for your vote of confidence.

...R

thanks for your replys,

i just went through Robins serial post. i think it makes sense.

My install works fine at a lower baurate, but arduino is acting weird when i increase it. I assumed this is overflowing buffer from the big length of the string.

as long as you set aside enough space for it.

what exactly do you mean ?

But what is the issue really, is it arduinos memory not enough to store the 300 char characters in one loop ?

i need to refresh that string 15 times per second or so.

billybouki:

as long as you set aside enough space for it.

what exactly do you mean ?

As written the char array receivedChars[] is 32 chars long. That size is in in numChars. You just need to make it as big as you need.

...R

But what is the issue really, is it arduinos memory not enough to store the 300 char characters in one loop ?

Sure, but loop() can iterate thousands of time a second. Serial data can NOT arrive that fast. The serial buffer is only 64 characters. Another thing to keep in mind.

@charlesdavis my array is char already, or am i not getting you. @PaulS so if i delay the loop to my desired framerate would arduino manage the serial buffer then properly to fit all the characters ?

not clear to me yet how to handle this.

thanks

@PaulS so if i delay the loop to my desired framerate would arduino manage the serial buffer then properly to fit all the characters ?

Don't EVEN think of that. Read and store data as fast as possible. When ALL the data has arrived, by whatever means you use to determine that that has happened, use the data.

but then to my understanding, there is no space to hold the amount of data i need in one serial read. because this is restricted to 64 chars.

So what is the solution exactly? hope im not asking naive questions, its not very clear to me how arduino manages the serial buffer

thanks a lot

billybouki: So what is the solution exactly?

Read Reply #6 which I wrote in response to your specific question.

...R

So what is the solution exactly?

Read the data faster than it arrives. That way, the buffer never fills up.