-------------------------------------------------------------------------------------
??? Error using ==> serial.fopen at 72
Port: COM1 is not available. Available ports: COM3, COM4.
Use INSTRFIND to determine if other instrument objects are connected to the requested
device.
Error in ==> PulseOx_01 at 111
fopen(s1);
------------------------------------------------------------------------------------
Obviously MATLAB understands how to do this, and even returns the available ports in the error message. (For whatever reason, Windows 7 has assigned the pulse-ox to COM4. I discovered this when I opened Device Manager.)
For the time being, I "hard-coded" the pulse-ox to COM4. The problem is that if I want to share my routine or use it on another machine, I'll have to go back into Device Manager on that machine and figure out what serial port it's assigned to. If I could at least find the available COM ports, I could query them to find out which one was connected to the pulse-ox.
Either I'm doing something wrong or I've discovered a problem with "instrfind" and "instrfindall". I've looked through all the "Help" documentation and did a search on the TMW site but couldn't find the answer. To the best of my knowledge, I'm doing everything correctly, but obvously something is wrong somewhere.
Is there a way for me to convince MATLAB to discover — and write to a variable — that my pulse-ox is on COM4, rather than my having to do this manually? Am I doing something wrong with "instrfind" or "instrfindall", or am I missing something else? (This wasn't a problem under 32-bit Vista and 32-bit MATLAB, so I can't figure out what's wrong. With those I could assign my pulse-ox to any COMx port and it'd work.)
Thanks,
Frank
HTA
"Star Strider" <skystrider...@ieee.net> wrote in message <hdlavv$35u$1...@fred.mathworks.com>...
Currently, you can add the serial port and find it in MATLAB. The problem
is that you only get a fresh list of available ports on the first scan.
Your observations are correct in that MATLAB assumes the serial port is
soldered onto your motherboard and wont go anyplace. We are considering
adding the ability to scan for new and moved serial ports as USB virtual
ports are fairly reliable and common now.
"HeavierThanAir " <kyle.adr...@gmail.com> wrote in message
news:hfocd4$1ji$1...@fred.mathworks.com...
"Trent Jarvi" <tja...@mathworks.com> wrote in message <hfod9t$kb$1...@fred.mathworks.com>...
UPDATE
I'm still working on this problem. My device uses a Silicon Laboratories CP2102, so I e-mailed the Silabs support engineers to see if there's a way to get the port information from the Registry Entry. (According to Silabs AN197, this was possible in Vista. Silabs has updated its drivers for Windows 7 but I can't find Windows 7 documentation.)
I can get information from the Registry with MATLAB's routines. (I'm not going into detail here in case my approach is wrong.) I need to know where to look in the Registry Entry to get the COM Port assignment. When I figure out how to do this, I'll write a short MATLAB function and post it to the file exchange. I'll post the soluton here in CS-SM as well.
I found that MATLAB does strange things to the COM Ports, and that polling them to see where my hardware is invariably generates errors. To make my life even more interesting, MATLAB seems to unpredictably close open COM Ports so that subsequent attempts to open them are unsuccessful. To add complexity to this problem, each call to 'instrfind' seems to do strange things to the port status as well. (There *has* to be some magic combination of 'fopen', 'fclose', 'delete' and 'clear' that makes this work!) I still haven't figured that out, so I opted for what I hope is a more reliable approach. That led me to searching the Registry.
I decided to kludge an interim solution and finally found that my device always seems to be allocated COM4, by peeking at the Registry using the Registry Editor and searching through the tree to:
'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB' ... etc.
then searching through each USB entry until I found the 'Friendly Name' that matches the description for it in Device Manager. (This has been quite an adventure.) So for the time being I'm hard-coding the COM4 assignment in my routine.
Meanwhile, if any of you figure out a solution to this before I do, please post it to *this* thread. I have it set up to e-mail me, so I won't miss it.
Star Strider
I figured out a sort of solution for this! It's inelegant, kludgy, and could likely be improved, but it works! I listed it below. I'm not posting it to the File Excange because it's not sufficiently straightforward. I invite anyone with knowledge of the Windows 7 Registry and the MATLAB commands for it to improve it. It only has to run once in any routine that needs it.
What I would like (from TMW) is a routine that tells me what's attached to the serial ports associated with each USB port. That apparently requires writing a lot of C++ code, not something I want to do anytime soon.
If anyone has problems with it, I can provide limited help through this thread.
Good luck!
Star Strider
---------------------------------------------------------------------------------------------------------------------
function PortIn = getusbserialport()
% GETUSBSERIALPORT is my (Star Strider's) kludged routine to detect the
% serial port assigned to a particular USB device. It has extensive
% internal documentation, so if necessary it should be easy to adapt to
% variations in the registry entries for each device. It requries the
% device's VID and PID values (one-time manual lookup).
%
% The returned value "PortIn" is a string variable as 'COMx' and can be
% used as returned as an argument to "serial".
% "Star Strider"
% — MATLAB® 7.9.0
% — 19 December 2009
% This routine is specifically written to detect the Windows 7 driver
% registry entry for:
% Silicon Laboratories CP210x USB to UART Bridge
% Driver Version: 5.4.24.0
% Driver Date: 2009-07-07
% See: Silicon Laboratories AN197 for full documentation
% NOTE: To find the registry entry for your device, remove the ";" from the
% "dos" command (Line 34), then run "getusbserialport" from the command
% line. Search the output in the Command Window for your device. Registry
% entry formats are different for different devices, so it may be necessary
% to tweak "getusbserialport" if the registry entry for your device is
% sufficiently different from that used by Silicon Labs for the CP210x.
% GET RELEVANT REGISTRY CONTENTS —
% Get Registry contents with DOS call.
% (Do 'REG QUERY /?' in the Command Prompt or use the MATLAB "dos"
% function with that string for details of this DOS command.)
hklm_usb_str = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\'; % Keep this as is — likely won't change
[~, regreplyresult] = dos(['REG QUERY ' hklm_usb_str ' /t REG_SZ /s /z']);
% DEFINE TARGET STRINGS —
% It will be necessary to look though the registry to find the VID_ &
% PID_ hex values if they're not already available. (Work with them as
% strings here.) Start by looking through "regreplyresult" for the
% necessary key combination. Read the "regreplyresult" data as a
% strings. MATLAB won't care.
hklm_usb_substr = 'VID_10C4&PID_EA60\0001\Device Parameters'; % Unique to particular driver
hklm_usb_full_str = [hklm_usb_str hklm_usb_substr];
% GET STRING LOCATIONS WITHIN "regreplyresult" —
len_hklm_usb_str = length(hklm_usb_full_str); % Define offset for subsequent read
[hklm_usb_full_idx] = strfind(regreplyresult, hklm_usb_full_str); % Find specific string with VID_ & PID_
[hklm_usb_idx] = strfind(regreplyresult, hklm_usb_str); % Find all occurrences of HKLM USB entries
next_key_idx = hklm_usb_idx(find(hklm_usb_idx > hklm_usb_full_idx, 1, 'first')); % Define end of search
% GET CELL ARRAY OF REGISTRY DATA OVER DEFINED RANGE —
[vrbl, ~, ~, value] = strread(regreplyresult(hklm_usb_full_idx+len_hklm_usb_str : next_key_idx-1), '%s %s %s %s', 'delimiter',' ');
% DETERMINE WHERE "PortName" IS —
portnameidx = strmatch('PortName', vrbl);
getport = value(portnameidx); % Returns "getport" as cell array
PortIn = char(getport); % Convert from cell to string
return
% ======================== END: getusbserialport.m ========================
---------------------------------------------------------------------------------------------------------------------
Isn't it possible for you to use this file:
I want Matlab to find an Arduino microcontroller automatically, but no luck so far. I am using Windows XP, but I'm not quite sure how to edit your m-file so I can use it for an Arduino with XP. Could you give me some advice?