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.
Serial port parameter | what it does |
---|---|
BaudRate | Sets the baudrate. The most common today is 57600, but 4800, 9600, and 115200 are frequently seen as well |
InputBufferSize | The 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 |
BytesAvailable | The number of bytes waiting to be read |
ValuesSent | The number of bytes sent since the port was opened |
ValuesReceived | The number of bytes read since the port was opened |
BytesAvailableFcn | Specify the callback function to execute when a specified number of bytes is available in the input buffer, or a terminator is read |
BytesAvailableFcnCount | Specify the number of bytes that must be available in the input buffer to generate a bytes-available event |
BytesAvailableFcnMode | Specify 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 |