Newsyland Blog

tail -f /dev/brain >> blog.newsyland.com

17  Mar 2008

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.


RSS feed

6 Comments »

Comment by Ming at 2008-07-02 15:00:23

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!

Actually, no. What we did instead is chose to use ZTerm. Using it, you can go “File”→”Start capture”, and spool the serial port data to a file.

 
 
Comment by M A Hopcroft at 2008-08-19 18:41:00

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!

Hi there!

I’ve succesfully (but not easily) installed rxtx on a Intel iMac (2,4GHz Core 2 Duo, 10.4.11). I managed to use it with Matlab R2007a.

Charlie C.

 
 
Comment by sushmita at 2009-03-27 09:19:51

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.

Are you using a Mac? If you are using a PC, you should be able to use something like: s = serial(‘COM1′,’BaudRate’,4800);

Did you follow the directions above?

Are you using the correct baud rate, parity, stop bits, etc.? This should be documented in your controller manual.

Where are you having problems? What kind of errors are you getting?

You may want to check out the Serial-IO.doc document here:
http://www.mathworks.com/matlabcentral/files/8311/Serial%20IO.zip

 
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.
NOTE!:
  • Your comment may need approval before it posts, please be patient.
  • I'm using page caching, so your comment may take a minute to show up.