title: Reading between serial ports using the Bytes Available function in Matlab
body: <p>I am trying to build a <strong>serial port device in Matlab</strong>. I have 2 devices 'COM1' and 'COM2'. 'COM1' <strong>ASYNCHROUNOUSLY</strong> writes data into the serial port 'COM2'. I have alternative names for 'COM1' and 'COM2', as follows:</p>
  
  <pre><code>global serialcom
  serialcom=serial('COM1'); %Serial Communication portal COM 1
  global testdummy
  testdummy=serial('COM2'); %Serial Communication portal COM 2
  </code></pre>
  
  <p>The number of Bytes in the input buffer of <strong>testdummy</strong> which triggers the testdummyfunction is 2, and this is specified using the testdummy.BytesAvailableFcnCount field (belo).</p>
  
  <pre><code>testdummy.BytesAvailableFcnMode = 'Byte';
  testdummy.BytesAvailableFcnCount = 2;
  testdummy.BytesAvailableFcn = @testdummycomfunction;
  </code></pre>
  
  <p>I have a function <strong>"testdummyfunction"</strong> on the <strong>testdummy</strong> side which is triggered using the BytesAvailable callback property in Matlab.The structure of this function is as follows:</p>
  
  <pre><code>function testdummyfunction(testdummy,BytesAvailable)
  % TESTDUMMYFUNCTION(testdummy,...BytesAvailable)
  % INPUTS:
  % TESTDUMMY:refers to the serial port testdummy
  % BYTESAVAILABLE:Refers to the callback function 'BytesAvailablefunction'
  
      global serialcom;
      data_string=fscanf(serialcom); %Reads the data sent form serialcom
  
  end
  </code></pre>
  
  <p>Now, suppose I print a string whose length is greater than 2 bytes (say 10 bytes) ASYNCHRONOUSLY into the testdummy side from the serialcom side. Although I know that testdummyfunction is triggered when there are 2 bytes in the input buffer of testdummy, will the <strong>data_string</strong> also contain a string that is 2 bytes long ALWAYS? <strong>If not, how can I ensure that it only reads 2 bytes of data?</strong></p>