External Interfaces/API    

The Serial Port Session

The serial port session comprises all the steps you are likely to take when communicating with a device connected to a serial port. These steps are:

  1. Create a serial port object - You create a serial port object for a specific serial port using the serial creation function.
  1. You can also configure properties during object creation. In particular, you might want to configure properties associated with serial port communications such as the baud rate, the number of data bits, and so on.

  1. Connect to the device - You connect the serial port object to the device using the fopen function.
  1. After the object is connected, you can alter device settings by configuring property values, read data, and write data.

  1. Configure properties - To establish the desired serial port object behavior, you assign values to properties using the set function or dot notation.
  1. In practice, you can configure many of the properties at any time including during, or just after, object creation. Conversely, depending on your device settings and the requirements of your serial port application, you might be able to accept the default property values and skip this step.

  1. Write and read data - You can now write data to the device using the fprintf or fwrite function, and read data from the device using the fgetl, fgets, fread, fscanf, or readasync function.
  1. The serial port object behaves according to the previously configured or default property values.

  1. Disconnect and clean up - When you no longer need the serial port object, you should disconnect it from the device using the fclose function, remove it from memory using the delete function, and remove it from the MATLAB workspace using the clear command.

The serial port session is reinforced in many of the serial port documentation examples. Refer to Example: Getting Started to see a basic example that uses the steps shown above.


  Getting Started with Serial I/O Configuring and Returning Properties