Using "serial" to connect to an Arduino

5 ビュー (過去 30 日間)
Thomas Ibbotson
Thomas Ibbotson 2012 年 1 月 4 日
回答済み: Bagus Tris Atmaja 2015 年 2 月 7 日
I got an Arduino for xmas, and I'm trying to talk to it from MATLAB (R2012a prerelease) via the serial interface. I'm not having any luck at the moment, I can talk to it from python no problem. Here's what I've got:
>> s = serial('/dev/ttyACM0', 'BaudRate', 9600);
>> fopen(s)
Error using serial/fopen (line 72)
Open failed: Port: /dev/ttyACM0 is not available. Available ports: /dev/ttyS0.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
But in ipython:
In [1]: import serial
In [2]: s = serial.Serial('/dev/ttyACM0', 9600)
In [3]: s.readline()
Out[3]: '408\r\n'
I've tried '/dev/ttyS0' and made sure I closed old connections, but I still get this error. I've run out of ideas, anyone have suggestions?
  1 件のコメント
Thomas Ibbotson
Thomas Ibbotson 2012 年 1 月 4 日
Forgot to point out that I'm on 64-bit linux (Ubuntu 11.10)

サインインしてコメントする。

採用された回答

Thomas Ibbotson
Thomas Ibbotson 2012 年 1 月 5 日
I found the following solution on the newsgroup:
Hi Patrick
You can use /dev/ttyACM0 but you need to let the library know that you will be using it. To specify the ports on your system, create a java.opts file in the directory you start MATLAB from with the following line in it:
-Dgnu.io.rxtx.SerialPorts=/dev/ttyS0:/dev/ttyS1:/dev/USB0:/dev/ttyACM0
After you restart MATLAB, the port should be found:
>> instrhwinfo('serial')
ans =
AvailableSerialPorts: {2x1 cell}
JarFileVersion: 'Version 2.7.0'
ObjectConstructorName: {2x1 cell}
SerialPorts: {2x1 cell}
>> ans.AvailableSerialPorts
ans =
'/dev/ttyS0'
'/dev/ttyACM0'
Another option is to create a symbolic link from /dev/ttyS101 to
/dev/ttyACM0. The port should be enumerated as ttyS101 then.

その他の回答 (4 件)

Walter Roberson
Walter Roberson 2012 年 1 月 4 日
Is ttyACM0 a virtual USB port? If so then the device needs to be connected when you start MATLAB, as MATLAB only scans the device table once.
  1 件のコメント
Thomas Ibbotson
Thomas Ibbotson 2012 年 1 月 4 日
Yes, the device was connected when MATLAB started.

サインインしてコメントする。


Jan
Jan 2012 年 4 月 19 日
i got the same problem. using ubuntu 11.10 and matlab 2010b with arduino ide 1.0
in which folder do i have to put the java.opts file?
/usr/local/MATLAB/R2010b/ ?
  2 件のコメント
Thomas Ibbotson
Thomas Ibbotson 2012 年 4 月 19 日
You could add the java.opts file to /usr/local/MATLAB/R2010b/matlab/bin/glnxa64/ if you have write permissions there.
Alternatively you can put it in the MATLAB startup directory i.e. the directory MATLAB returns from 'pwd' before any other code is run (which is the directory you were in when you called the command from if you launch MATLAB from the prompt, or if you've set up a desktop launcher for MATLAB, it is usually your home directory).
Thomas Ibbotson
Thomas Ibbotson 2012 年 4 月 19 日
I assumed you're on 64-bit linux, if it's 32-bit then the directory will be glnx86. In any case you can find out from MATLAB which architecture you are using from the command:
computer('arch')

サインインしてコメントする。


Jan
Jan 2012 年 4 月 19 日
ok, thank you! it s working :)

Bagus Tris Atmaja
Bagus Tris Atmaja 2015 年 2 月 7 日
I have same problem with Matlab 2013 in Ubuntu 14.04. When I try fnd info from serial port, it is live,
>> instrhwinfo('serial')
ans =
AvailableSerialPorts: {'/dev/ttyS101'}
JarFileVersion: 'Version 3.3'
ObjectConstructorName: {'serial('/dev/ttyS101');'}
SerialPorts: {'/dev/ttyS101'}
but, when I connect it with arduino (using ArduinoIO package),
>> a=arduino('/dev/ttyS101');
Attempting connection .............
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
Error using arduino (line 104)
Connection unsuccessful, please make sure that the board is powered on, running a
sketch provided with the package, and connected to the indicated serial port. You
might also try to unplug and re-plug the USB cable before attempting a
reconnection.
Any idea? I guess the error from ArduioIO package

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by