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

Now you should be a ready to use Octave with your Arduino or Rasberry PI and even with lab equipment like that great old HP34401 multi-meter on your workbench. Enjoy!


References
1. GNU Octave

2. MATLAB is a registered trademark of The Mathworks, Inc.

3. The latest instrument control package may be downloaded here. This article is written for version: 0.2.1 (2015-02-05). Future versions may change things significantly. Be sure to check the release notes. More information on the functions contained in the package is also located there.

4. A simple Web search will locate many examples of how to make a loopback plug for your serial port.


About the author
Steve Hageman is a confirmed analogue-a-holic since about the fifth grade when he built his first shortwave receiver. After acquiring his first Apple computer in 1982, he has continued using software to control analogue hardware and building useful measurement systems. Steve has had the pleasure of designing such diverse products as modular data acquisition systems, switching power supplies, RFIC test systems, software defined radios, and most recently, high frequency lock in amplifiers for biological sample investigation. He would be happy to discuss your custom project needs and can be reached via his website AnalogHome.


Appendix A
Code listing of the ReadToTermination() Function


function [char_array] = ReadToTermination (srl_handle, term_char)


% parameter term_char is optional, if not specified

% then CR = '\r' = 13dec is the default.

if(nargin == 1)

term_char = 13;

end

not_terminated = true;

i = 1;

int_array = uint8(1);


while not_terminated


val = srl_read(srl_handle, 1);


if(val == term_char)

not_terminated = false;

end


% Add char received to array

int_array(i) = val;

i = i + 1;


end


% Change int array to a char array and return a string array

char_array = char(int_array);


endfunction

You can place this function in a file named "ReadToTermination.m" and place it in your project's current working directory.


Appendix B
The complete demonstration program for use with a loopback serial connection [4].


% Octave—Serial Test—Using a loopback connection

clc; clear;


% Load the package

pkg load instrument-control


% Check if serial support exists

if (exist("serial") != 3)

disp("No Serial Support");

endif


% Instantiate the Serial Port

% Naturally, set the COM port # to match your device

% Use this crazy notation for any COM port number: 1—255

s1 = serial("\\\\.\\COM10");

pause(1); % Wait a second as it takes some ports a while to wake up


% Set the port parameters

set(s1, 'baudrate', 115200);

set(s1, 'bytesize', 8);

set(s1, 'parity', 'n');

set(s1, 'stopbits', 1);

set(s1, 'timeout', 123); % 12.3 Seconds as an example here


% Optional commands, these can be 'on' or 'off'

%set(s1, 'requesttosend', 'on'); % Sets the RTS line

%set(s1, 'dataterminalready', 'on'); % Sets the DTR line


% Optional—Flush input and output buffers

srl_flush(s1);


%——- Loopback Communication Test——-


% Looback using srl_write & srl_read

srl_write(s1, "Hello World!");


% reads back 12B as integers

read_back_int = srl_read(s1, 12);


% Convert the integers to a string

read_back_str = char(read_back_int);


% Use the ReadToTermination to read an entire line back

srl_write(s1, "Hello world!\r");

read_back = ReadToTermination(s1);


% Change the termination character to newline ('\n' = 10)

srl_write(s1, "Hello world!\n");

read_back2 = ReadToTermination(s1, 10);


% Read two lines back

srl_write(s1, "Hello world!\r Next Line\r");

line1 = ReadToTermination(s1);

line2 = ReadToTermination(s1);


% Finally, Close the port

fclose(s1);


 First Page Previous Page 1 • 2 • 3 • 4


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