User`s manual
Getting Started with Serial I/O
8-19
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.
You can also configure properties during object creation. In particular, you
may want to configure properties associated with serial port
communications such as the baud rate, the number of data bits, and so on.
2 Connect to the device – You connect the serial port object to the device
using the
fopen function.
After the object is connected, you can alter device settings by configuring
property values, read data, and write data.
3 Configure properties – To establish the desired serial port object behavior,
you assign values to properties using the
set function or dot notation.
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 may be
able to accept the default property values and skip this step.
4 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.
The serial port object behaves according to the previously configured or
default property values.
5 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” on page 8-18 to see a basic
example that uses the steps shown above.