How to Flush the Buffer of Serials in MATLAB

Written by chris daniels
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Flush the Buffer of Serials in MATLAB
(Thinkstock Images/Comstock/Getty Images)

MATLAB is a program used for mathematical analysis and data acquisition. It contains functions that let you record data from serial ports on your computer. As data is recorded from the serial port, it is put into a temporary buffer until your MATLAB program can transfer the data into a variable or data file. Flushing the buffer removes all data from the buffer for use. It is usually used after the recording has terminated or an error has occurred.

Skill level:
Moderately Easy

Other People Are Reading

Things you need

  • Computer with a serial port
  • MATLAB software

Show MoreHide

Instructions

  1. 1

    Create a serial port object. This example will use the serial port COM1. This is normally the first serial port, although your system configuration may differ. It is similar to a file handle and provides information for writing to and reading from the serial port. The code would read:

    serial_port_object = serial('COM1');

  2. 2

    Configure the serial port. Two common parameters to set for reading data are the size of the input buffer in bytes and the baud rate of the instrument you are reading data from. The latter number will be available in your instrument documentation. Use code similar to the following:

    serial_port_object.InputBufferSize = 50000;

    serial_port_object.BaudRate = 19200;

  3. 3

    Open communication to the serial port using the following code:

    fopen(serial_port_object);

  4. 4

    Flush the current buffer using the following code:

    data = fread(serial_port_object,serial_port_object.BytesAvailable);

    The BytesAvailable variable is set by the serial port hardware indicating the number of bytes in the buffer that are currently filled with data. The "fread" function will return binary data.

  5. 5

    Close the serial port using the following code:

    fclose(serial_port_object);

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.