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)
>> 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>...
Thanks for your help ;-)
"Laurens Willems van Beveren" <lwille...@hotmail.com> wrote in message <gd47q9$s2b$1...@fred.mathworks.com>...
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!
> 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.
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.
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>...
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.
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>...
"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?!!!
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>...
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?
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
> 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
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...
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
> 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
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?
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
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.
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);