Path: EDN Asia >> Design Centre >> IC/Board/Systems Design >> How to read serial data directly into Octave
IC/Board/Systems Design Share print

How to read serial data directly into Octave

02 Nov 2015  | Steve Hageman

Share this page with your friends

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


Reading and writing data
Now that we have opened and set up the COM Port, it's time to read and write data. The examples that follow use a serial port with a loopback connector attached [4].

A basic "Hello World!" can be sent and received as follows,

% Example using srl_write & srl_read

srl_write(s1, "Hello World!");

% Reads back 12B as an array of integers

read_back_int = srl_read(s1, 12);

% Convert the integers to a string

read_back_str = char(read_back_int);

The srl_write() command writes out everything in the buffer and it blocks further execution until the buffer is empty. The srl_read() command reads back the number of bytes specified as an array of integers. A conversion to a string is easily done as shown.

If there are fewer characters to read then you specify in the srl_read() call, the read will hang until the port times out. If there are more characters to read than you specify they will be left in the input buffer.

Naturally, this requirement to know the length of the input string is very restrictive and puts a real damper on the serial port usability, but it is exactly the lowest level command that we need in order to build upon.

Since most instruments communicate by some form of termination character, either a carriage return, line feed, or both, we can just read each character as it comes in and terminate the read when we get to the specified termination character.

This need led to my humble addition to the serial port operations, and the: ReadToTermination() function was born (Appendix A). Its usage is as follows,

return_string = ReadToTermination(serialport, {termination_char})

First, it returns a string, which can be parsed for numbers later or can be used as a basis for modified functions as you may need for your specific needs, like returning a double.

Second, it keeps reading the serial port a character at a time until the {termination_char} is read. If you leave the {termination_char} blank, the default character used is the Carriage Return ('\r'). If your device instead uses the New Line ('\n'), or some other termination character, then you can specify this as needed.

The {term_character} is specified as an unsigned 8bit int. The most common termination codes are listed below. You can look up other codes if needed on an ASCII chart.

Carriage Return = '\r' = 13dec (The Default)

New Line = '\n' = 10dec

 First Page Previous Page 1 • 2 • 3 • 4 Next Page Last Page

Want to more of this to be delivered to you for FREE?

Subscribe to EDN Asia alerts and receive the latest design ideas and product news in your inbox.

Got to make sure you're not a robot. Please enter the code displayed on the right.

Time to activate your subscription - it's easy!

We have sent an activate request to your registerd e-email. Simply click on the link to activate your subscription.

We're doing this to protect your privacy and ensure you successfully receive your e-mail alerts.

Add New Comment
Visitor (To avoid code verification, simply login or register with us. It is fast and free!)
*Verify code:
Tech Impact

Regional Roundup
Control this smart glass with the blink of an eye
K-Glass 2 detects users' eye movements to point the cursor to recognise computer icons or objects in the Internet, and uses winks for commands. The researchers call this interface the "i-Mouse."

GlobalFoundries extends grants to Singapore students
ARM, Tencent Games team up to improve mobile gaming

News | Products | Design Features | Regional Roundup | Tech Impact