MATLAB LanguageUsing serial ports

Download MATLAB Language for free

Introduction

Serial ports are a common interface for communicating with external sensors or embedded systems such as Arduinos. Modern serial communications are often implemented over USB connections using USB-serial adapters. MATLAB provides built-in functions for serial communications, including RS-232 and RS-485 protocols. These functions can be used for hardware serial ports or "virtual" USB-serial connections. The examples here illustrate serial communications in MATLAB.

Parameters

Serial port parameterwhat it does
BaudRateSets the baudrate. The most common today is 57600, but 4800, 9600, and 115200 are frequently seen as well
InputBufferSizeThe number of bytes kept in memory. Matlab has a FIFO, which means that new bytes will be discarded. The default is 512 bytes, but it can easily be set to 20MB without issue. There are only a few edge cases where the user would want this to be small
BytesAvailableThe number of bytes waiting to be read
ValuesSentThe number of bytes sent since the port was opened
ValuesReceivedThe number of bytes read since the port was opened
BytesAvailableFcnSpecify the callback function to execute when a specified number of bytes is available in the input buffer, or a terminator is read
BytesAvailableFcnCountSpecify the number of bytes that must be available in the input buffer to generate a bytes-available event
BytesAvailableFcnModeSpecify if the bytes-available event is generated after a specified number of bytes is available in the input buffer, or after a terminator is read

Related Examples

Automatically processing data received from a serial port