Read serial data directly into Octave
The Octave: “Instrument Control Package”  was updated mid-year 2015 and fixed a number of serial port issues. Now that the serial port more or less works we can get some real work done.
Note: These instructions assume that the reader has some previous serial port knowledge and knows how to hook up and use their device. The information here will help you get the serial package going in Octave.
Install the instrument control package
The basic steps to install the instrument control package in Windows are:
1. Download the latest instrument control package  and place it in the Octave packages directory,
Note: The exact path will depend on the version of Octave you are running. The example above is for version: 4.0.0.
2. Start Octave.
3. Set the current directory to the directory where you placed the instrument control package in step 1.
4. In the Octave Command Window, at the “>>” prompt type,
Note: Be sure to use the correct file name for the instrument control package that you are installing. The above example is for version 0.2.1.
5. Press enter and wait. The install takes several minutes, even on a very fast computer.
Finally, the command prompt will come back and the package will be all compiled installed and ready to go.
Using the serial commands in Octave
A number of steps need to be done to load the instrument-control package and then set it up for use. You will need to load the instrument control package before you can use it in your Octave program. In your Octave program you will need to place this command near the start of your program.
if (exist("serial") != 3)
disp("No Serial Support");
The first line loads the package. The optional 'if' statement checks the serial port status. Three means that the serial functions are OK, anything else is an error and no serial support has been loaded. This check is useful if your program could ever run on a computer that does not have the Instrument Control Package installed.
To open the serial port and get a handle to it for future reads and writes use the 'Serial()' command as shown,
% Naturally, set the COM port # to match your device
% Use this crazy notation for any COM port number: 1 - 255
s1 = serial("\\\\.\\COM15") % Open the port
pause(1); % Optional wait for device to wake up
The returned value 's1' is the handle to the opened port that is used in subsequent port operations. Your device may take a while to wake up when opened, so a pause() may be needed after opening the port.
If you try to open a COM Port that does not exist, you will get this error on the command line,
error: serial: Error opening the interface: No such file or directory
The solution to this error is to: connect, power up, or specify the proper COM Port for your device.
The next step is to setup the serial port for the proper operating parameters. The get/set interface is the preferred way to access the serial port. And the basic parameters are,
% Set the port parameters
set(s1, 'baudrate', 115200); % See List Below
set(s1, 'bytesize', 8); % 5, 6, 7 or 8
set(s1, 'parity', 'n'); % 'n' or 'y'
set(s1, 'stopbits', 1); % 1 or 2
set(s1, 'timeout', 123); % 12.3 Seconds as an example here
The terms above should be familiar to most folks who have ever set up a serial port.
Some things to note:
- Handshaking is set to: None. At this time, there is no code support for any other settings.
- The Data Terminal Ready (DTR) and Ready to Send (RTS) control lines are enabled by default when the port is opened. If needed you can use the following commands to set the state to anything you need.
% Optional commands, these can be 'on' or 'off'
set(s1, 'requesttosend', 'on'); % Sets the RTS line to on
set(s1, 'dataterminalready', 'off'); % Sets the DTR line to off
- Baudrate can be: 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200 and 230400.
- Timeout is specified in 1/10 second units. -1 means no timeout and the port will wait forever for the specified operation to complete.
Depending on your device you may need to flush the serial input and output buffers, so I do something like this:
% Optional Flush input and output buffers