0

I have connected a phone to the computer by serial port and I am using matlab to control it(the phone).

s = serial('COM8');
s.BytesAvailable %this returns 0

after using some fwrite AT command s.BytesAvailable returns number bigger than 0 (in my case it returns 54, but the number is irrelevant here).

So can I reset the s.BytesAvailable to get it to 0 again? I tried fopen(s), but nothing (seems to have) happened.

A Petrov
  • 417
  • 1
  • 9
  • 23
  • Sounds like the phone is sending you back some data. If you don't need the data, why don't you just ignore it? What problem is it causing? – wakjah Jun 09 '13 at 13:16
  • @wakjah, I do need the data for a moment (to display a message) then I need it to disappear. Now, how do I make it disappear? I'm using `fgets(s)` to read the data, but that doesn't reset the `s.BytesAvailable` to 0. – A Petrov Jun 09 '13 at 13:22
  • 1
    To read all the available data on the serial input buffer you can use either use `fscanf` (for text formatted data) or `fread` (for binary data) according to [**Writing and Reading Data**](http://www.mathworks.com/help/matlab/matlab_external/writing-and-reading-data.html). Note that in both cases `BytesAvailable` will be reset to `0`. `fgets` reads only one line of text data, it is probable that your device returns more than a line. – p8me Jun 09 '13 at 15:57

1 Answers1

0

You can try flushinput(Serial_Port_obj) function in matlab. This function clears all the data that are stored in the input buffer.

Otherwise you can use fread(Serial_Port_obj,Serial_Port_obj.BytesAvailable). The latter just reads instantly all the data stored again in the input buffer.

But as soon as the buffer clears out, data from the phone will start again to flow in the computers buffer. Simply think it like this:

PhoneDataToTransmitOnThePhone -> SoftwareBufferOnThePhone -> HardwareBufferOnThePhone -> HardwareBufferOnThePC -> SoftwareBufferOnThePC -> PhoneDataToTransmitOnThePC

If you also want to delete data that are stored on the phone buffer I can't help you with that.

Note that all the buffer are FIFO (First In -> First Out).

2nisi
  • 394
  • 3
  • 8