'In Octave, how to close the serial port after opening with the new "serialport" command syntax

The latest documentation for serial communication in Octave explains to use the "serialport" command to open the port instead of the deprecated "serial" command.

There is no explanation of how to close the serial port. I used this to successfully open the port and do some writing

s1=serialport('com5','Baudrate',57600)
num=write(s1,'help')

But I can't figure out how to close the port. I used this:

fclose(s1)

And got this error response

error: file id must be a file object, std::string, or integer value

Does anyone know how to close the serial port?



Solution 1:[1]

The octave functions attempt to mimic where possible the Matlan functions, which also does not have a close function.

To close the serial port, set the variable to something else, and if it is not used elsewhere, it will close the port.

# create a serialport references by the s1 variable
s1=serialport('com5','Baudrate',57600)
num=write(s1,'help')

# set s1 to something else
s1 = [];
# s1 was only variable referencing the serial port
# so the serial port is now freed/closed

You could also clear the variable:

clear s1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1