Stymied on this - fopen('ser') hangs Matlab!
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2012-7-8
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Functions in Microsoft Excel 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!