Serial Port communications on Mac via Matlab

I have a rev B (Core 2 Duo from Dec, 2006) Apple MacBook Pro laptop running Leopard OS X (version 10.5.2) and have Matlab version 7.5.0.338 (R2007b). I’ve just found that I can NOT use the built-in serial port object, instead, I get the following error:

 java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
??? Error using ==> serial.serial at 57
The serial port object is supported on the Solaris, 32-bit Windows and 32-bit Linux platforms only.

So, to get it successfully working, I downloaded these files: SerialComm.m, SerialComm.mexmac and SerialComm.mexmaci from http://svn.berlios.de/svnroot/repos/osxptb/stable/Psychtoolbox/PsychHardware/.

You can then use code like this:

 PORT = 1;
% close (if previously open)
SerialComm( 'close', PORT)
% open
SerialComm('open',PORT,'9600,n,8,1')
% flush the buffers
SerialComm( 'purge', PORT)
% status
SerialComm( 'status', PORT)
% read
STR = SerialComm( 'readl', PORT);
% close
SerialComm( 'close', PORT)

[UPDATE 02 Apr, 2008]: Note, this same code didn’t seem to work on a recently purchased Macbook. Good luck.

[UPDATE 27 Mar, 2009]: I see that in Matlab version 7.8.0 (R2009a) if you go to ‘serial’ in Help docs it now says for Mac OS X and Mac OS X 64 you can use serial(‘/dev/tty.KeySerial1’); I don’t have a way of testing that right now, but it’s possible that you don’t need to use this third party toolkit anymore. If that’s true, please leave a note here.

6 thoughts on “Serial Port communications on Mac via Matlab

  1. Hello, did you ever get it to work with a serial-to-usb convertor? I keep getting the error message ‘SERIALCOMM(‘OPEN’): could not open port’. If you know how to fix it, please let me know. Thanks!

  2. There is a similar toolbox called comm here:
    http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=4952

    BUT, there is another way. MATLAB uses the code from the rxtx project (rxtx.org) for its serial port interface for Windows and Linux. This same code can also be compiled for OS X, although it has only recently become stable enough to use. I suspect it will eventually be included in MATLAB. In the meantime, you can do it yourself, if you are not afraid of the terminal:

    1) Install the latest version (2.1-8) from rxtx.org. This requires getting the code through cvs.
    2) Install your preferred USB-to-serial kernel extension (I am using the FTDI driver for a Prologix USB controller).
    3) Open the included m-file “serial.m” and comment out the lines that check to see if you are using a Mac (lines 56-58).

    Restart the computer, plug in USB device, start MATLAB. You can now use the “tmtool” and “serial” commands like a normal MATLAB user.
    For example:

    >> tmtool
    >> io=serial(‘/dev/tty.usbserial-PXQDOOHN’)

    Serial Port Object : Serial-/dev/tty.usbserial-PXQDOOHN

    Communication Settings
    Port: /dev/tty.usbserial-PXQDOOHN
    BaudRate: 9600
    Terminator: ‘LF’

    Communication State
    Status: closed
    RecordStatus: off

    Read/Write State
    TransferStatus: idle
    BytesAvailable: 0
    ValuesReceived: 0
    ValuesSent: 0

    >>

    Pretty cool… I’m using MATLAB 2007b on Intel Mac, 10.5.4. I don’t pretend it was easy, and I have no idea if it will work on any other system/versions. Good luck!

  3. I am doing my project using Matlab2008a. I am using simulink blocks for designing my model. I have flashed the program i got from my model in PIC18F4550 since it supports USB. i am giving my inputs using controller to the PC. so iam interfacing it using USB. Can u please help me in giving the M file for the matlab to accept the data from controller.

Leave a Reply to Ming Cancel reply

Your email address will not be published. Required fields are marked *