How to fill the Serial buffer before send .

Dear All,

I am doing a plataform to adquire an EEG (Electroencephalography) where I use the Arduino plataform as a low cost ADC . After the adquisition I move data to matlab.

I adquire 500 data (each one word type, 8 byte) and then send it via a serial like this.

for (i=0; i<500; i++){
    Serial.println(data[i]);
    i++;
  };

I know this is extremly inefficient since the serial buffer is 64 bytes long and I only send 2 bytes each time.

How can I make this more efficient?

Concatenate as a string and send it it would be also inefficient since I have just 400 ms to send the info.

And increasing the baudrate upper 115200 I think it would make the transmission a little bit unstable. isnt’ it?
23 seconds

As far as I can understand If I increase the serial buffer to 256 bytes long and send it everything in 5 times it would make my day :smiley: but …

How can I fill the serial buffer before send it?

Thanks you in advance!

No, you have it the wrong way around. You are thinking of packets, where a single transmission must include the start and end and a fixed block of data.

Serial just sends bytes. Your code will quite efficiently stuff 1000 bytes into the buffer, pausing when it's full and re-starting when space is available, because the buffer is always transmitting as fast as it possibly can.

500 integers is rather a lot of data for an Arduino. I trust you have checked you aren't overflowing your Arduino's memory with that large array?

I have used 200,000 baud without problems on Arduino MEGA2560. You should try and see if you do get issues when transmitting at higher baud rate instead of thinking that it would result in errors. Even higher rates are possible, I think. Also Arduino Leonardo has some rather high rates IIRC.

Thank you for your answers to both of you :smiley:

I have verified that I don't have memory problems Stack and Heap never collide .

And I push the bit rate into 200.000 bps but it doesn't make sense I need that bitrate.

I only need to move 500X2B=1000B=8000bits in 400ms, so that is a 20.000 bitrate. I know baudios are not bits but I thought with 115200 bps it would be enough but it is not. I have used a serial port analyzer and it takes 600ms (more or less) to send all the array.

So there is something I am misunderstanding. I have to make more test with higher speeds but I would like to understand how it works , not just do it :slight_smile:

Thank you for all your help!!!

Using pintln() sends the decimal value (up to 6 digits) and CR/LF, i.e. up to 8 bytes per value.
Change it to write() to send 2 bytes per value.

Serial transmission adds start and stop bits, i.e 10 bits are transmitted per byte.

The USB-COM port emulation may be another bottleneck. The Serial baudrate only affects the communication between the controller and the USB chip, the bitrate. But the bitrate over USB should be sufficient for your purpose, I think.

Thank you to all your responses, I gave you some Karma for that :wink:

I would us the serial.write() to send the info to Matlab and there I would handle the information, I guess with the serial.write() the transmission speed would be enough for my purpose.

It would be great to have more information about how it works the transmission deep inside, I search that info :smiley:

I make test during the week and hope my problem is solved.

See HardwareSerial.cpp in Arduino\hardware\arduino\avr\cores\arduino, how write() is implemented there.

Essentially a byte is put into the buffer, if there is room for it. Whenever the transmitter becomes idle, the next byte in the buffer is sent. If the buffer is full, wait until another byte has been sent.

madring:
Thank you to all your responses, I gave you some Karma for that :wink:

I would us the serial.write() to send the info to Matlab and there I would handle the information, I guess with the serial.write() the transmission speed would be enough for my purpose.

It would be great to have more information about how it works the transmission deep inside, I search that info :smiley:

I make test during the week and hope my problem is solved.

I once wrote a MATLAB script that took in serial data from an Arduino. I think you'll have more luck with Serial.println().