Problem of output buffer in arduino uno when using bitlash

Problem of output buffer when using bitlash
I am trying to use Arduino Uno via bitlash: a command-line terminal Serial Interface.

When I add the bitlash to Arduino IDE library and run the bitlash demo script, it build a COM server inside Arduino, and I could control the pins by sending text command from command line terminal tools (such as HyperTerminal in windows) to the Arduino COM.

For example, to read the digital pin 12:

print d12
it returns the pin 12’s digital readout: 0
0
i=0;pinmode(7,1);while i<=1000{dw(7,1);snooze(1);dw(7,0);snooze(1);i++;};
This command blinks the LED connected to D7.
print 2+2
4

Now I want to use Matlab Serial interface to talk to bitlash COM server inside Arduino.

Obj1 is the COM object.

aStr = ‘print d12’;
fprintf(obj1,’%s\n’,aStr);
% to get the output, run: disp(fgetl(obj1))

disp(fgetl(obj1))
print d12 Note: bitlash always echo the command itself, weird
disp(fgetl(obj1))
0

disp(fgetl(obj1))
Then Matlab was blocked until timeout, because output buffer is empty.

Usually I run many commands which I do not need the output.
After that, I run a command to read digital value, the output was stored in output buffer.
The problem is that to get that results, I need to run many rounds of disp(fgetl(obj1)) to get the old buffer out, then I could get the most fresh results, and then the output buffer was empty.

I tried to call Matlab function flushoutput(obj1), but it has no effect.

So this is very inconvenient for me to get the output I want.
Is there a way to auto-flush the output buffer so that only the last line remains in output buffer, so that I can read it directly.

Call the tline = fgetl(obj1) repeatedly is a bad idea because I cannot tell how much times to call. And when it returns the last fresh line of output buffer, more calling will block the Matlab until timeout, which is very unwanted.

I solved the problem by reading all the buffer:

obj1.BytesAvailable
while obj1.BytesAvailable > 2
fgetl(obj1);
end