Main Content

fopen (serial)

(To be removed) Connect serial port object to device

This serial object function will be removed in a future release. Use serialport object functions instead. For more information on updating your code, see Version History.

Syntax

fopen(obj)

Description

fopen(obj) connects the serial port object, obj to the device.

Examples

This example creates the serial port object s, connects s to the device using fopen, writes and reads text data, and then disconnects s from the device. This example works on a Windows® platform.

s = serial('COM1');
fopen(s)
fprintf(s,'*IDN?')
idn = fscanf(s);
fclose(s)

Tips

Before you can perform a read or write operation, obj must be connected to the device with the fopen function. When obj is connected to the device:

  • Data remaining in the input buffer or the output buffer is flushed.

  • The Status property is set to open.

  • The BytesAvailable, ValuesReceived, ValuesSent, and BytesToOutput properties are set to 0.

An error is returned if you attempt to perform a read or write operation while obj is not connected to the device. You can connect only one serial port object to a given device.

Some properties are read-only while the serial port object is open (connected), and must be configured before using fopen. Examples include InputBufferSize and OutputBufferSize. Refer to the property reference pages to determine which properties have this constraint.

The values for some properties are verified only after obj is connected to the device. If any of these properties are incorrectly configured, then an error is returned when fopen is issued and obj is not connected to the device. Properties of this type include BaudRate, and are associated with device settings.

Version History

Introduced before R2006a

collapse all

R2021a: serial object interface will be removed

Use of this function with a serial object will be removed. To access a serial port device, use a serialport object with its functions and properties instead.

The recommended functionality has additional capabilities and improved performance. See Transition Your Code to serialport Interface for more information about using the recommended functionality.

See Also

Functions