Clear socker input buffer < SOLVED


Hi all,

  1. My question:

I’m using a socket in an AIR project to receive serial data from an arduino.
However, my arduino spits out the data too fast, so the input buffer keeps filling up.

I know the OUTput buffer can be cleared with the flush() command.
But how can I clear the INput buffer ?

  1. Why exactly ?

I’m using an arduino to do spectrum frequency analyses on music and this should be visualized in my air app.
Arrays of frequency levels are coming into my air app over serial and bars should display the level.
If the buffer fills up with too many arrays to be processed, the visualization gets behind.

Thanks for reading !



I found a solution:

completely clear the INput buffer:
(Not a good idea because you get: “Error: Error #2030: End of file was encountered.”)

if(socket.bytesAvailable > 1500){
   var foo = socket.readUTFBytes(socket.bytesAvailable)

Partially clear the INput buffer:
(and keep 500 bytes)

if(socket.bytesAvailable > 1500){
   var foo = socket.readUTFBytes(socket.bytesAvailable - 1000)

From the as3 reference:

readBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void
Reads the number of data bytes specified by the length parameter from the socket.

Following code reads 4 bytes from the buffer, “takes them out of the queue”
(the queue is “first come, first served / first arrived first taken out”)
So for example:


Takes / removes 4 bytes from the buffer