Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using Serial Port with Mac OS X

622 views
Skip to first unread message

M. A. Hopcroft

unread,
Aug 25, 2008, 6:35:03 PM8/25/08
to
I recently discovered that it is possible to access serial
port devices (through the USB ports) using MATLAB 2007b on
Mac OS X, even though it is not officially supported.
Following a hint from Peter Meilstrup, I discovered that the
code that MATLAB uses for serial port access is created by
the RXTX project (rxtx.org). The rxtx code for Windows and
Linux is stable enough that MATLAB includes it in their
product, but the Mac OS X code has only recently become good
enough to use, and its still a little edgy (but it is
working fine for me). The following steps will enable serial
port access in Mac OS X:

1) Install the rxtx code, version 2.1-8 or higher (the
gnu.io branch). The 2.1-7 version is available for download;
get it and then use cvs to get the latest version. The
instructions are on the rxtx wiki.

2) Install the kernel driver for whatever USB-serial device
you are using. I am using a Prologix USB-GPIB controller
which uses an FTDI USB chip. The PL2303 is another popular
chip used in many USB-serial converters.

3) Start MATLAB, and edit the file serial.m. Comment out the
lines that cause an error if you are using OS X (lines 56-58).

Voila! You can now use the normal serial port commands, like
serial, fprintf, etc:

>> io=serial('/dev/tty.usbserial-PXQDOOHN');
>> io.Terminator = 'CR/LF'

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

Communication Settings
Port: /dev/tty.usbserial-PXQDOOHN
BaudRate: 9600
Terminator: 'CR/LF'

Communication State
Status: closed
RecordStatus: off

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

>> fopen(io)
>> fprintf(io,'++ver')
>> r=fscanf(io)

r =

Prologix GPIB-USB Controller version 5.1

>> fclose(io)
>> delete(io)

Laurens Willems van Beveren

unread,
Oct 15, 2008, 3:58:01 AM10/15/08
to
I tried the same thing, but in Matlab get the following error:

>> delete(instrfind)
>> instrfind

ans =

[]

>> io=serial('/dev/tty.usbserial-PXQB9AXB');
io.Terminator = 'CR/LF'

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

Communication Settings
Port: /dev/tty.usbserial-PXQB9AXB
BaudRate: 9600
Terminator: 'CR/LF'

Communication State
Status: closed
RecordStatus: off

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

ValuesSent: 0



>> fopen(io)
fprintf(io,'++ver')
r=fscanf(io)

??? Error using ==> serial.fopen at 71
Cannot connect to the /dev/tty.usbserial-PXQB9AXB port. Possible reasons are
another
application is connected to the port or the port does not exist.

Do you know what is wrong?
Regards,
Laurens

"M. A. Hopcroft" <mho...@mindspring.com.nospam> wrote in message <g8vc2n$4go$1...@fred.mathworks.com>...

Laurens Willems van Beveren

unread,
Oct 15, 2008, 9:19:01 PM10/15/08
to
But with the latest 2.1.8 rxtx drivers I got it to work just as you described. Great, let the fun begin.

Thanks for your help ;-)

"Laurens Willems van Beveren" <lwille...@hotmail.com> wrote in message <gd47q9$s2b$1...@fred.mathworks.com>...

A-J Campagne

unread,
Oct 30, 2008, 6:01:02 AM10/30/08
to
"M. A. Hopcroft" <mho...@mindspring.com.nospam> wrote in message <g8vc2n$4go$1...@fred.mathworks.com>...

Laurens Willems van Beveren

unread,
Oct 30, 2008, 7:00:04 PM10/30/08
to
i got the prologix usb to gpib controller working in matlab r2007 a or b using the serial/COM port using the rxtx code 2.1.8. i can now acquire data from a Keithley 2602A source measure unit. thanks again for your tips. cheers, laurens

Nico Christie

unread,
Nov 8, 2008, 9:26:02 PM11/8/08
to
"Laurens" wrote:
< i got the prologix usb to gpib controller working in matlab r2007 a or b using the serial/COM port using the rxtx code 2.1.8... >

Hi M.A., hi Laurens, I'm trying to get this to work, but got stuck with the cvs update! How can I check which version is intalled? I still get that error Laurens mentions, despite having followed the intructions on this site:

http://rxtx.qbang.org/wiki/index.php/Retrieving_Source_Code

using, as suggested,

cvs checkout -r commapi-0-0-1 rxtx-devel

and cvs update rxtx-devel ...

Where to next...?

For those not very familiar with Terminal and shell scripting this getting stuck story shows up quite often, there's a lot of "as you'd allways do..." written in the instruction texts! Not that I'm complaining about all you great guys working to get around all these problems, but a more "idiot-proof" version would be welcomed arms wide open! :)

Finally, a perhaps even dumber question, how do I know which 'Port' is the one I'm after? When trying to open serial('IWantThisToCrashPort') it obviously won't find it and trow a list of available ports. Intuition points to "/dev/tty.Dj-SerialPort-1" but since I still have that other problem...

Thanks! Saludos!

M. A. Hopcroft

unread,
Nov 17, 2008, 12:45:03 AM11/17/08
to
Hi Nico,
See point #1 of my post- I think that the easiest thing to do is to download the code for version 2.1-7, which is available as a single download tar file (*not* from cvs). This download file has the cvs info already set up; so if you cvs update this directory, then you will have the 2.1-8 version (this is mentioned somewhere else on the rxtx wiki, which is how I learned it).

> Finally, a perhaps even dumber question, how do I know which 'Port' is the one I'm after? When trying to open serial('IWantThisToCrashPort') it obviously won't find it and trow a list of available ports. Intuition points to "/dev/tty.Dj-SerialPort-1" but since I still have that other problem...

Yes, the port name is the complete entry including "/dev". You can type "ls /dev/tty.*" to get a list of connected serial devices. Speaking of typing:

> For those not very familiar with Terminal and shell scripting this getting stuck story shows up quite often, there's a lot of "as you'd always do..." written in the instruction texts! Not that I'm complaining about all you great guys working to get around all these problems, but a more "idiot-proof" version would be welcomed arms wide open! :)

Yeah, this is actually really hard, because there are a lot of creative idiots out there, including myself. For example, while writing this post, I can't remember the syntax for doing a cvs update. If I was going to do it right now, I would read the help file, then google something, then ask somebody, etc., until I got it. Then I would forget it again in a few minutes :) So, instead, I write "do the cvs update". Good technical writing takes a lot of work, thats why its a paying job...
If you are really motivated, take careful notes while you work on this problem, and then write down *exactly* what you did. We salute your effort!

Good luck, post again if it still doesn't work.

Maryam Vaziri Pashkam

unread,
Jan 8, 2009, 11:27:01 PM1/8/09
to
"M. A. Hopcroft" <mho...@mindspring.com.nospam> wrote in message <g8vc2n$4go$1...@fred.mathworks.com>...


Hi

I am trying to communicate with a hand monitoring device (Polhemus fastrak) using a serial port-USB (Keyspan). I am using a Macbook with OSX 10.5.5 and MATLAB 7.6.0 (R2008a). This is my first experience with serial ports and I am having a hard time with it so any help will be most appreciated.

I have followed your instructions and downloaded rxtx 2.1-7 and updated it by typing 'cvs update' in terminal. I have no idea how to check weather the update was done properly and what version of rxtx do I have right now. The problem is I get the exact same error as Lauren explains in the thread:

??? Error using ==> serial.fopen at 71
Cannot connect to the /dev/tty.usbserial-PXQB9AXB port. Possible reasons are
another
application is connected to the port or the port does not exist.

Any idea where the problem is and how I can fix this?

Bests,
Maryam.


Laurens Willems van Beveren

unread,
Jan 9, 2009, 4:58:03 AM1/9/09
to
dear maryam,
did you reboot already? and secondly, i got it to work with matlab r2007a. this could be why it is not working? also, you must be sure to have the last cvs version of rxtx.
ciao,
laurens

Maryam Vaziri Pashkam

unread,
Feb 26, 2009, 6:55:03 PM2/26/09
to
Dear Lauren,

I installed the latest version of rxtx, and could open a serial port successfully on my MAC without an error. I could also change the baud rate and other configurations. but there is another big problem. Now when I try to write on the serial port using fprintf, Matlab hangs and even force quite does not work. I have to restart the computer to bring up MATLAB.
The device that I am using needs the command fprintf('C\n') to start sending data. The strange thing is this signal is being sent. After I restart my computer and open the serial port again in MATLAB, I can read data from the serial port using fscanf. So fscanf works, fprintf sends the data, but MATLAB hangs after sending the data. Any idea what I should do and where the problem might be?

Thanks,
Maryam.


"Laurens Willems van Beveren" <lwille...@hotmail.com> wrote in message <gk773b$p0l$1...@fred.mathworks.com>...

M. A. Hopcroft

unread,
Mar 2, 2009, 12:19:02 PM3/2/09
to
You might try looking at the serial communications settings. In my experience, many devices & software react badly to incorrect settings, such as:

termination character
timeout
Input and Output buffer size
baud rate
etc.

These are all parameters of the 'serial' object. Make sure they agree with the settings that the serial device expects. Also, try sending data when only the USB-serial adapter is connected (without the actual serial device). If you can send data to "nowhere", then it is more likely that these settings are the problem.

Good luck,
-Matt

"Maryam Vaziri Pashkam" <mvaz...@gmail.com> wrote in message <go7a4n$i18$1...@fred.mathworks.com>...
> Dear Lauren,
>
> I installed the latest version of rxtx, and could open a serial port successfully on my Mac without an error. I could also change the baud rate and other configurations. but there is another big problem. Now when I try to write on the serial port using fprintf, Matlab hangs and even force quite does not work. I have to restart the computer to bring up MATLAB.

Maryam Vaziri Pashkam

unread,
Mar 6, 2009, 7:02:01 PM3/6/09
to
Hi Mat,

Thanks for the tip! Actually there was no problem with the setting. I had the same problem communicating with serial port using Terminal and that made me think there is a Harvdware problem and not a software one.

I found out were the problem is. I was using an Old Keyspan USB-Serial Adaptor. Seems like there is an incompatibility with the old ones and new MACs/new driver. I changed it with a brand new KeySpan and everything started working! Now I can read and write on the serial port with Serial command. I have no idea however why the old KeySpan works on PC and not in MAC?!!!

Thanks again for all your help,
Maryam.


"M. A. Hopcroft" <mho...@mindspring.com.nospam> wrote in message <goh4e6$t0g$1...@fred.mathworks.com>...

M. A. Hopcroft

unread,
Mar 8, 2009, 3:46:02 PM3/8/09
to
Maryam,
Glad to hear things worked out. Do you mind sharing the exact model name/number of the Keyspan serial adapter? I am looking to buy something like this. Thanks!
-Matt

"Maryam Vaziri Pashkam" <mvaz...@gmail.com> wrote in message <gosdhp$6o4$1...@fred.mathworks.com>...


> Hi Mat,
>
> Thanks for the tip! Actually there was no problem with the setting. I had the same problem communicating with serial port using Terminal and that made me think there is a Harvdware problem and not a software one.
>

> I found out were the problem is. I was using an Old Keyspan USB-Serial Adaptor. Seems like there is an incompatibility with the old ones and new Mac/new driver. I changed it with a brand new KeySpan and everything started working! Now I can read and write on the serial port with Serial command. I have no idea however why the old KeySpan works on PC and not in Mac?!!!

Maryam Vaziri Pashkam

unread,
Mar 9, 2009, 4:13:02 PM3/9/09
to
Hi Matt,

The new USB serial connector that I have is :
" KeySpan USB Serial adaptor P/N: USA-19HS "
you can install the driver online from the KeySpan website.
The old on is USA-19W that is not working.

Cheers,
Maryam.



"M. A. Hopcroft" <mho...@mindspring.com.nospam> wrote in message <gp179q$rmv$1...@fred.mathworks.com>...

Paul

unread,
Apr 13, 2009, 3:18:01 PM4/13/09
to
Hi
This seems to be the right place to get help... as I see some of you are trying the same challenge.
My challenge is sending/receiving data to/from a Festo FC-20 PLC (used for a time lapse photographic project - http://www.gondwanastudio.com/Content_1050100000_time+lapse.htm) from a MacBook Pro via a Sunix USB-serial adapter.

I have a trial version of MatLab R2009a and here is the code I am using in order to send 10000 values to the PLC registers :
s = serial('/dev/tty.usbserial');
set(s,'BaudRate',9600,'Parity','none')
fopen(s);
// loop 10000x
fprintf(s,'mmw0'); // prepare register 0 for sending value
fprintf(s,'4444'); // value to store in register 0
fprintf(s,'dmw1'); // interrogate register 0
p = fscanf(s); // read buffer to get value
Warning: A timeout occurred before the Terminator was reached.
p = ''
// this is the 'error' I get - the objective here is to test is the value has been correctly stored
// end of loop
fclose(s)

My questions...
1/ Do I need MatLab to do this part (I use MatLab to calculate the 10000 values)?
2/ Can I do this simply in the Terminal? I tried with PHP but fopen() hangs...
3/ If I use MatLab, is my code correct? How can I read the serial buffer with fscanf()?
4/ Do I need to set-up a line terminator? Which one and how?

Paul

unread,
Apr 13, 2009, 3:27:01 PM4/13/09
to
Me again...
5/ Do I need RXTX for matLab R2009a? If yes where do I get it from?
Thanks, Paul

Ankit Desai

unread,
Apr 13, 2009, 3:46:02 PM4/13/09
to
"Paul " <pa...@kidsofnature.org> wrote in message <gs03m5$l0g$1...@fred.mathworks.com>...

> Me again...
> 5/ Do I need RXTX for matLab R2009a? If yes where do I get it from?
> Thanks, Pau

Hi Paul,

I have not used Sunix USB-Serial adaptor, but assuming that it works correctly I'll try to answer your questions.

1. You can use MATLAB to communicate via a serial port.
3. Regarding the code being correct:

You might want to figure out some basic serial properties for your instruments.
For example, the baud rate, parity, terminator etc.
The property setting on the instruments must match that of your serial object.

fscanf is used to read string values returned by the instrument/device. The functions looks/waits for the terminator before it stops looking for more data - as is evident from the warning - "A timeout occurred before the Terminator was reached"

help fscanf should provide you with more information

If you just want to read binary data, you might want to use fread instead.

While if your purpose is to just query, just use the query command. It will send the command and read the returned values.

4. Please check the device for the terminator that you need to use and make sure you set the serial object to the same terminator.

You can find more information at: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f95464.html

Hope this helps

-Ankit

Paul

unread,
Apr 13, 2009, 4:25:07 PM4/13/09
to
Thank you Ankit,

> I have not used Sunix USB-Serial adaptor, but assuming that it works correctly I'll try to answer your questions.
Yes it works fine.

> You might want to figure out some basic serial properties for your instruments.
> For example, the baud rate, parity, terminator etc.
> The property setting on the instruments must match that of your serial object.

set(s,'BaudRate',9600,'Parity','none') these settings are correct for the PLC
I was not sure if the terminator was PLC or Mac specific (LF is supposed to be the terminator for the PLC

> fscanf is used to read string values returned by the instrument/device. The functions looks/waits for the terminator before it stops looking for more data - as is evident from the warning - "A timeout occurred before the Terminator was reached"
> help fscanf should provide you with more information
> If you just want to read binary data, you might want to use fread instead.

Can you be a bit more specific, please?

> While if your purpose is to just query, just use the query command. It will send the command and read the returned values.

My objective is to change the value of each 10000 register one by one and make sure that at each step of the loop, the value stored in register is the same as the one I have just sent (at least if this operation is necessary.
Via QuickTerm, the typical sequence for one step would be :
>.mmw0<CR> // to prepare register 0
=0:. // reply
>.1234<CR> // new value for register 0
>.dmw0<CR> // get value from register 0
=1234 // reply
Obviously I do want to do the same in any kind of program (Terminal, MatLab, PHP ...) that will allow me to send and check the 10000 values.

> 4. Please check the device for the terminator that you need to use and make sure you set the serial object to the same terminator.

It is supposed to be LF based on Festo PLC spec sheet.

I really need to find a way to send these values to the PLC. Any other idea?
Paul

M. A. Hopcroft

unread,
Apr 13, 2009, 4:35:17 PM4/13/09
to
"Paul " <pa...@kidsofnature.org> wrote in message <gs0359$dmp$1...@fred.mathworks.com>...

>
> My questions...
> 1/ Do I need MatLab to do this part (I use MatLab to calculate the 10000 values)?
> 2/ Can I do this simply in the Terminal? I tried with PHP but fopen() hangs...
> 3/ If I use MatLab, is my code correct? How can I read the serial buffer with fscanf()?
> 4/ Do I need to set-up a line terminator? Which one and how?

Hello Paul,
1) Well, no one *needs* MATLAB for anything :) You should only use MATLAB if it makes your programming task easier.
2) So yes, you can write your program in some other programming environment if you want to. php, perl, C, whatever.
3) Your MATLAB code seems fine. Of course I don't know how your serial device is supposed to work... However, you did not set the 'Terminator' property for the serial port object,
4) and since you got an error relating to the Terminator setting, it seems like a good thing to investigate... The Terminator character is added to the end of every string of data that is sent to/from the serial port (it 'terminates' the data string), and fscanf() is waiting to see that character to know when the string is complete (your php code is probably similiar). The Terminator character is specific to your serial port device, so you will have to check its documentation.
5) As of MATLAB version 2009a (Instrument Control toolbox 2.8), serial port support for all platforms including Mac OS X is included, so you do not have to install RXTX yourself. See release notes:
http://www.mathworks.com/access/helpdesk/help/toolbox/instrument/index.html?/access/helpdesk/help/toolbox/instrument/rn/brqzen9-1.html&http://www.mathworks.com/access/helpdesk/help/base/relnotes/f0-107996.html

And the documentation for serial, with Mac OS X included:
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/serial.html

So actually, it seems like the purpose for this thread is nearly over...

Ankit Desai

unread,
Apr 13, 2009, 4:53:01 PM4/13/09
to
"M. A. Hopcroft" <mho...@mindspring.com.nospam> wrote in message <gs07m5$3it$1...@fred.mathworks.com>...

You might want to try setting your object's terminator property to match that of the PLC.

set(s,'Terminator','LF');

fscanf read data from instrument, and formats it as text while fread reads binary data from instrument.

Hope this helps

-Ankit

Paul

unread,
Apr 13, 2009, 4:56:01 PM4/13/09
to
Thank you again for the pointers (I am very new to MatLab and even more to serial comm), so your help is very much appreciated.

> 3) Your MATLAB code seems fine. Of course I don't know how your serial device is supposed to work... However, you did not set the 'Terminator' property for the serial port object,

I did just after your first message but I still get the same warning error when I set up the terminator.
s.Terminator = 'LF'
I also tried 'LF/CR' but still the same error...

> So actually, it seems like the purpose for this thread is nearly over...

Unfortunately not yet...
Paul

Paul

unread,
Apr 13, 2009, 5:42:01 PM4/13/09
to
No success!

s = serial('/dev/tty.usbserial');
set(s,'BaudRate',9600,'Parity','none','Terminator','LF');
fopen(s);
fprintf(s,'mmw0')
p = fread(s)
fprintf(s,'3333')
p = fread(s)
fprintf(s,'dmw0')
p = fread(s)
fclose(s)
Warning: The specified amount of data was not returned within the Timeout period.

p =

Empty matrix: 1-by-0

Warning: The specified amount of data was not returned within the Timeout period.

p =

Empty matrix: 1-by-0

Warning: The specified amount of data was not returned within the Timeout period.

p =

Empty matrix: 1-by-0

What can be wrong?

Paul

unread,
Apr 13, 2009, 6:08:01 PM4/13/09
to
I have just tried another PLC and without changing anything in the code from the last post, now I get some returned values but they do not make a lot of sense...

But why do I still get the timeOut period warning?

Warning: The specified amount of data was not returned within the Timeout period.

p =

109
108
119
48
7
13
10
65
67
67
69
83
83
32
69
82
82
79
82
13
10
62
17

Warning: The specified amount of data was not returned within the Timeout period.

p =

51
50
51
51
7
13
10
65
67
67
69
83
83
32
69
82
82
79
82
13
10
62
17

Warning: The specified amount of data was not returned within the Timeout period.

p =

100
107
119
48
7
13
10
65
67
67
69
83
83
32
69
82
82
79
82
13
10
62
17

M. A. Hopcroft

unread,
Apr 13, 2009, 6:19:01 PM4/13/09
to
> What can be wrong?

Well, it looks like your serial device is not sending any data and/or is not working the way you think it should... It does not appear to be a question of the MATLAB serial interface (or php, same result). I don't know how your device is supposed to work... Maybe you need a forum devoted to your USB device?

In general, I would recommend using fscanf() instead of fread() until you are certain of the format of the data you expect to get.

Ankit Desai

unread,
Apr 13, 2009, 6:29:01 PM4/13/09
to
"Paul " <pa...@kidsofnature.org> wrote in message <gs0d41$dma$1...@fred.mathworks.com>...

Hi Paul,

fread takes the numbeof bytes to read as second input.
You might want to call fread as following.

data = fread(s,s.BytesAvailable);
transposedData = data';

Since you are not providing fread with the number of bytes to read, it is waiting till the timeout occurs.

Like I mentioned for getting result in text format use:

data = fscanf(s);

0 new messages