How to Flush the Buffer of Serials in MATLAB

Save

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.

Things You'll Need

  • Computer with a serial port
  • MATLAB software
  • 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');

  • 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;

  • Open communication to the serial port using the following code:

    fopen(serial_port_object);

  • 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.

  • Close the serial port using the following code:

    fclose(serial_port_object);

References

  • Photo Credit Thinkstock Images/Comstock/Getty Images
Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!