Thursday, March 5, 2015

Serial Port Communication with GNU Octave in Windows

This is not so much a finished post as it is a place to record progress. Use any information found on this page at your own risk.

Introduction

I have been using GNU Octave in place of MATLAB on my laptop for a while now. It is free and serves my purposes well. One place MATLAB does have it beat though is in its ability to communicate with outside hardware through a serial port. I recently needed this functionality for Octave. This is how I made it work. My configuration:
  • Windows 7 - 64 bit
  • GNU Octave 3.8.2-5 using MXE installer
  • Instrument Control Package 0.2.1

Walkthrough

Install Octave

If you found this post I will assume you are probably running Windows. There is a convenient unoffical installer for Windows HERE. At the time of this writing I am running 3.8.2-5. Anything greater than 3.8.0 has the nice MATLAB style GUI.

Install Instrument Control Package

The equivalent of MATLAB toolboxes are packages in Octave. You need the instrument-control package to access the serial ports. There are two ways to install it.

1) Install it from Octave forge. Assuming you have an internet connection, open Octave and type in the command window "pkg install -forge instrument-control-0.2.1.tar.gz" Replace the 0.2.1 with the newest version of the package.

2) Download it from HERE. Assuming you did a standard install, move it to the folder "C:/Octave/Octave-3.8.2/src". There you will find all the other packages that were included with the installer. Now open Octave and make that folder your directory. Type in the command window "pkg install instrument-control-0.2.1.tar.gz". Obviously you may need to change the name of the package if you download a newer version.

Both options will take a while. One of my first mistakes was thinking I had crashed my computer. I wasn't sure if it would work on Windows, so when it just sat there for a minute I thought it was hung. Just give it some time. Mine took a couple minutes. 

Load Instrument Control Package

You only have to install the package once, but you need to load it every time you open Octave (you can also set it to auto load. Google it.)

Type "pkg list" to see all your installed packages. If you don't see instrument-control then you need to go back to the last step. Any package with an * by it is loaded.

To load the package type "pkg load instrument-control". Now load the list of packages again to see if it worked.

Use the Package

Now the part you have been waiting for. It is important to note that at the time of this writing the instrument control package is not a drop in replacement for the serial capabilities of MATLAB. Here are some helpful links to illustrate this. It is fairly obvious that the function names are different or missing for Octave.
For my initial test I used an Arduino with a jumper between Rx and Tx. This essentially mirrored anything I sent back to me. To simplify things, go to the device manager and change the serial port number to COM1 through COM8. Over that and additional work is needed. Device Manager > your port > Port Settings > Advanced > COM Port Number.

My Additions
To better serve my needs I added a few files to make the package more MATLAB compatible. Just make sure they are in your path somewhere if you want to use them.

srl_fwrite: Download HERE. Similar to the MATLAB fwrite. The regular srl_write only accepts char and uint8s. I made this function to simplify sending other variable types. Accepts three inputs 
  • Serial Object
  • Data to be sent
  • Data Type - int8, uint8, int16, uint16, int32, uint32, int64, or uint64
srl_fread: Download HERE. Similar to MATLAB fread. Reads serial port and returns data type specified. Takes three inputs.
  • Serial Object
  • Number of values to be returned. (eg for 3 uint64s, enter 3 not 24)
  • Data Type - int8, uint8, int16, uint16, int32, uint32, int64, or uint64

Test Script
Test Script: This script was taken and modified from the wiki linked above. It opens a serial port, sends a couple values and then attempts to read them when the serial device mirrors them back. A "correct" output should look something like this.

Serial: Supported
s1 = 0x444
int8 = 200
intdata =

    0  142    1   44


That is all I have at the moment. I hope this tutorial was useful to someone out there. I plan to do another post on the way I am actually using this capability in the future as a more in depth example. 

-Matthew

No comments:

Post a Comment