MATLAB Answers

Stymied on this - fopen('ser') hangs Matlab!

15 views (last 30 days)
I am helpless with this one, I have a digital Pulse oximeter which acts as a COM device - connected through USB port. When it connects, I see on the device manager COM4 port activated with it.
But when I write
ser=serial('COM4');
fopen(ser);
pause(1);
data=fread(ser,ser.BytesAvailable);
disp(done);
fclose(ser);
The Matlab application goes into an endless busy mode. The only two ways to get out is cntrl-alt-del, or On the device, is an option to switch the USB on or off manually. By default the USB mode is on, but if I switch it to off manually, I can now access Matlab.
To rerun the above I have to give
delete(instrfind);
Surprisingly, the following works:
1. replace ser.BytesAvailable by 512
and
2. Put USB mode 'off; then run the Matlab program; while it is waiting put the USB mode to 'On' manually on the device. Then I get 512 bytes of data out.
Whenever I replace 512 with BytesAvailable I get error "SIZE has to be greater than 0". Also wheever I run the code with USB 'On', fopen hangs Matlab. On debugging I found that the place where the code hangs is at fopen(getobject1) in fopen.m
Any ideas suggestions. Please help. Thanks. rajn

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Jul 2012
Traditionally, working with serial ports (not MATLAB specific)
If the opening end does not assert DTR then the device might not respond. This is, though, likely being asserted automatically on your behalf.
If the device does not assert DCD then the open might wait for it to be received. This is one of the normal operation modes for serial ports, to have the process go to sleep until the device is ready (such as when a call comes in to a modem) and then to wake up because the open() succeeds then. On Unix systems it is possible to bypass this mode, and I would think it likely that MS WIndows provides mechanisms for this as well, but I do not know how it is done on MS Windows.
When a serial port is being emulated over USB, these signals do not physically exist on the USB cable, but updated information about the state of the signals is send with every USB packet of emulated serial port data. When a program opens a USB-emulated serial port, then if it opens it in such a way that it is expecting to look for DCD and the remote end is not supplying DCD then the open would wait for DCD to appear in a packet.
Unfortunately I do not know how to activate or disable DCD recognition under MS Windows or inside MATLAB on MS Windows; my serial port work was on Unix systems.

  1 Comment

gujax
gujax on 9 Jul 2012
Thank you Walter, At least now I know what to look for. I have seen on the Web only three sites which have codes for communicating with this device. Two use Linux OS and run pyserial. And as you mention, that code is difficult to decipher especially when I am trying to look for emulating functions in Matlab. The third one does use Matlab though on a similar but a different model of the instrument. I do not see in his code anything that he is doing different. For them fopen() just works.
Appreciate your clarifications. Rajn

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by