Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: Ramon_W on Jan 17, 2019, 09:09 pm

Title: Serial.Write - accessing the ringbuffer
Post by: Ramon_W on Jan 17, 2019, 09:09 pm
Hi all,

I'm currently building a MIDI controller with internal Clock to control some Synthesizers and Drummachines. For that I'm using the Serial Ports of the Arduino Mega.

I guess the Serial Output is also stored in a ringbuffer and I was wondering if there is a way to store one byte in front of the ringbuffer even if there are still some bytes which need to get send?

I want to make sure that the Clock messages always gets send directly and not get delayed by other MIDI messages which are not so timing important.

Thanks for any help,

Ramón
Title: Re: Serial.Write - accessing the ringbuffer
Post by: PieterP on Jan 17, 2019, 09:14 pm
The internals of the HardwareSerial class are protected.
https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/HardwareSerial.h (https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/HardwareSerial.h)

You could inherit from it, or write your own class entirely.

You'll have to decide what to do when the buffer is full. Maybe send the clock messages immediately?

Pieter
Title: Re: Serial.Write - accessing the ringbuffer
Post by: Robin2 on Jan 17, 2019, 09:19 pm
I guess the Serial Output is also stored in a ringbuffer and I was wondering if there is a way to store one byte in front of the ringbuffer even if there are still some bytes which need to get send?
Wouldn't it be simpler to organize things so the buffer is always empty  or has at most 1 byte by limiting the rate at which stuff is put into it.

What baud rate are you using?

...R
Title: Re: Serial.Write - accessing the ringbuffer
Post by: Ramon_W on Jan 17, 2019, 09:59 pm
Thanks for the fast reply.
I'll have a look into the HardwareSerial-Class, but not sure if my programming skills are good enough for that.
How big is the size of the HardwareSerial ringbuffer?

I'm using baud rates between 31250 and 250000 depending on the Synthesizer. The Rates are normed and I can't change them.
Some of the faders I build are controlling a lot of parameters at once. That means one fader movement sends a lot of messages, especially if the Fader is moved fast. I could probably try to limit these fast movements and skip some of the messages.
Just wanted if there might be a faster way to set the Clock-byte always at the first write position.

Title: Re: Serial.Write - accessing the ringbuffer
Post by: Robin2 on Jan 17, 2019, 10:34 pm
How big is the size of the HardwareSerial ringbuffer?
64 bytes

Quote
That means one fader movement sends a lot of messages, especially if the Fader is moved fast. I could probably try to limit these fast movements and skip some of the messages.
You could write your code so it checks availableForWrite() to make sure it is 64 before sending the next byte of every message.

If that would reduce the performance of the ordinary messages too much then it seems to me that the real problem is that you are trying to send too much data.

...R