How to retrieve 'bus reported device description' for Universal Serial Bus Controllers in MATLAB? - matlab

I am trying to extract 'Bus reported device description' and 'Bus Relations' informations for Universal Serial Bus Controllers in Matlab.
I can't find it in the registry and I don't know exactly how to use setupapi.dll function in Matlab to get the informations.
I want to do this because I have a plurality of Arduino Nano devices and all of them have different COMs.
I also use other USB Serial Devices under Matlab and for all of them I must create different serial objects, with different COM port names.
I want to create a Matlab function which will return what devices are connected on USB ports and what COM port they use .
I hope someone can help me with some ideas or code examples.
Thanks in advance!

This is a very late answer, but I also run into this problem and I have found a solution to find the "Bus reported device description" after searching on the Internet. I do not need "Bus Relations" in my case but I guess it may be retrieved in a similar way. I am using Windows 10 21H1 and MATLAB R2021a.
In short, my solution has 4 steps:
Find all the active COM port devices.
Find the friendly names of all devices.
Based on 1 and 2, get the friendly names of all active COM port devices.
Based on 3, get the "Bus reported device description" of all active COM port devices.
Code:
% To improve speed (x10) add jsystem to the path from here:
% https://github.com/avivrosenberg/matlab-jsystem/blob/master/src/jsystem.m
if exist('jsystem','file')
fsystem = #jsystem;
else
fsystem = #system;
end
% Find all the active COM ports
com = 'REG QUERY HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM';
[err,str] = fsystem(com);
if err
error('Error when executing the system command "%s"',com);
end
% Find the friendly names of all devices
ports = regexp(str,'\\Device\\(?<type>[^ ]*) *REG_SZ *(?<port>COM\d+)','names','dotexceptnewline');
cmd = 'REG QUERY HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ /s /f "FriendlyName" /t "REG_SZ"';
[~,str] = fsystem(cmd); % 'noshell'
% Get the friendly names of all active COM port devices
names = regexp(str,'FriendlyName *REG_SZ *(?<name>.*?) \((?<port>COM\d+)\)','names','dotexceptnewline');
[i,j] = ismember({ports.port},{names.port});
[ports(i).name] = names(j(i)).name;
% Get the "Bus reported device description" of all active COM port devices
for i = 1:length(ports)
cmd = sprintf('powershell -command "(Get-WMIObject Win32_PnPEntity | where {$_.name -match ''(%s)''}).GetDeviceProperties(''DEVPKEY_Device_BusReportedDeviceDesc'').DeviceProperties.Data"',ports(i).port);
[~,ports(i).USBDescriptorName] = fsystem(cmd);
end
This solution works but may not be clean. I am not a Windows expert anyway. Suggestions are highly appreciated.

Related

Connecting serial port via MATLAB App Designer

I want to connect my Arduino to App Designer by using the "drop down" list. This is what my app looks like
First, I am looking for if there is any serial com. system. And I am writing them to Drop Down.
p = instrhwinfo('serial');
app.SerialPortsDropDown.Items = p.AvailableSerialPorts;
After this I have planned to read the serial port that is shown in the Drop Down and write it to serialport()
app.a = serialport(app.SerialPortsDropDown.value,9600);
Unfortunately these lines did not work. The error message I got:
Error using serialport (line 116)
Unable to connect to the serialport device at port 'COM9'. Verify that
a device is connected to the port, the port is not in use, and all
serialport input arguments and parameter values are supported by the
device.
So, the first two lines of code work. I am able to see COM9 (the com my arduino connected) in the drop-down list. This shows there is a serial port at COM9. But when it comes to reading it with app.a = serialport(app.SerialPortsDropDown.value,9600); it gives error.
How can I connect a serial port via the MATLAB App-designer?
app.a = serialport(app.SerialPortsDropDown.value,9600);
This is a wrong way of connecting Arduino to MATLAB. This declaration does not let us use Arduino functions such as 'writeDigitalPin, writePWMDutyCycle'.,
As I mentioned in the comments, it is still important to clear the port first and connect the serial port.
Lastly, the true way to declare Arduino to be able to use its functions as in the following:
app.a = arduino(app.SerialPortsDropDown.value, 'Tag of your arduino card');

Programmatically find COM and LPT address in Matlab

I am using Matlab to program an experiment.
In particular, I am using these lines of code to read and send triggers to an external device:
ioObj = io64;
status = io64(ioObj);
io64(ioObj,portWriteAddress,0);
I have found the value for 'portWriteAddress by navigating to:
Device manager -> Ports -> LPT1 -> Resources. Then, in Resources there is an entry called I/O Range/ Settings with a code like 02S7 - 02SS (something like that).
Then I have converted it from Hex to Dec and put it in the line above.
THE PROBLEM IS: I am running this experiment on several different computers. Is there a way to programmatically find this range (or address) information fromMatlab?
Thank you all for your time.
Gluce
P.S.
The OS that I am using is Windows 7 (it should be soon updated to windows 10).
The computers are running either Matlab 2015b or 2016b.

serial monitoring method to test communication via com ports without a serial communication device

I have a Verilog code simulated and synthesized on ISE design toolkit. I've got an FPGA spartan 6 device which is to be used for the implementation. But there is a problem with the device (probably a power issue) which makes the device unavailable in any of the COM ports when I connected it to my PC. So I want to check whether my Matlab code which I made for serial communication through the device does the desired job. So I need a method to test serial communication via any of the COM ports without connecting a serial com device to the PC. Is there any such method that I can Tx Rx serial data from Matlab to COM ports? Any software or any other method would be highly appreciated :)
I found a way to test Matlab serial communication using virtual serial ports.
Download "Freeware Virtual COM Ports Emulator" from: http://freevirtualserialports.com/
I installed it in Windows 10, and it's working (as trial).
Add a pair of two serial ports:
Execute the following Matlab code sample to verify it's working:
s3 = serial('COM3','BaudRate',115200);
s4 = serial('COM4','BaudRate',115200);
fopen(s3);
fopen(s4);
fwrite(s3, uint8([1, 2, 3, 4, 5]));
%fprintf(s3, '12345');
pause(0.1);
RxBuf = fread(s4, 5)
fclose(s3);
delete(s3);
clear s3
fclose(s4);
delete(s4);
clear s4
The output is:
RxBuf =
1
2
3
4
5
Bypassing the problem "it only stays for a single test session".
There is a problem when creating a pair of virtual ports using the software, it only stays for a single test session.
I guess it's a problem with the COM port emulation software.
The following solution, is not a good practice (and not a true solution).
Declare the serial object as global, keeping the object persistent.
Create the serial object only if it's not created.
Don't delete and don't clear the serial object.
See the following code sample:
global s3 s4
if isempty(s3)
s3 = serial('COM3','BaudRate',115200);
end
if isempty(s4)
s4 = serial('COM4','BaudRate',115200);
end
fopen(s3);
fopen(s4);
fwrite(s3, uint8([1, 2, 3, 4, 5]));
pause(0.1);
RxBuf = fread(s4, 5)
fclose(s3);
%delete(s3);
%clear s3
fclose(s4);
%delete(s4);
%clear s4
You can also look for a better virtual COM port software.
As Rotem suggested, if you need to communicate via serial line between 2 program of your PC you need a virtual COM port emulator.
It seems you are running on Windows OS so I would recommend a completely free emulator (not a trial one). For Windows I use com0com Null-modem emulator (from SourceForge).
In the example below I will show how to communicate with "another" device so Matlab will not handle both side of the communication. The other device will be simulated by a simple terminal. For windows I use RealTerm: Serial/TCP Terminal (also from SourceForge).
Setup:
Execute the setup of both program with all default options. by default com0com will create a virtual pair COM3/COM4 but if these port already exist on your system the program may assign other numbers. Check the numbers before you run the example. (it will also create a CNCA0/CNCB0 pair but you can ignore this one for now).
For RealTerm, once installed (don't forget to activate the server registration at the end of the setup, it should be ticked by default though), it will look like below. Keep all default options, just set the port number and the baud rate if they need to be changed.
Test MATLAB -> Terminal
You are ready to send Ascii characters or binary values from MATLAB to your device. The animation below shows you an example of both option:
you can click on the picture to see it full size. It is running in loop so you may want to wait until it restart from the beginning.
Test Terminal -> MATLAB
Below animation shows you how to test the communication in the other way:
Don't forget to tick [CR] [LF] on RealTerm when you send Ascii characters and want to use the '%s' format specifier on MATLAB, as it needs these characters to detect the end of the string.
Note:
If you have another terminal program that you are more used too, it
will work the same.
If the RealTerm option does not suit you, or if you want to handle
both sides of communication from Matlab, then you can use the code
provided by Rotem in his first answer. Just install com0com but
ignore all the RealTerm part.

PC to PC Serial port communication through Matlab

I have two laptops are connected via a USB cable, the USB on one end is attached to a USB to serial adapter. I need to send information from one laptop (Mac) to the other laptop (PC) through Matlab.
I know how to use fopen to use serial ports in Matlab to send these "markers" to the second laptop. However, I am unsure on how to read them at the receiving end.
I currently have the following code where the serial connection is opened:
% connect to serial port to send markers
portID = '/dev/tty.USA28X145P2.2';
global markerID
[markerID, err] = fopen(portID, 'w');
if ~isempty(err)
error('An error was returned whilst connecting to the serial port
to send markers. The error was:\n\t"%s"', err);
end
My Matlab script then calls a function that uses fwrite to write the information that I need to the object that was opened using fopen.
Now I am unsure how to "receive" these in Matlab on the other laptop. Do I need to use fscanf?
I am quite new to this so I'm not sure how to proceed. Any suggestions would be appreciated!
Thanks

CNCopyCurrentNetworkInfo() is returning bad data

Apple introduced the CNCopyCurrentNetworkInfo() function in OS 4.1.
https://developer.apple.com/library/ios/#documentation/SystemConfiguration/Reference/CaptiveNetworkRef/Reference/reference.html#//apple_ref/doc/c_ref/kCNNetworkInfoKeySSIDData
According to the documentation it should:
Return the current network info for a given network interface.
However, when used it seems to return the correct SSID (readable network name) but a garbage BSSID(the MAC address of the Wireless Access Point). I have tried this connected to multiple different networks with two different iPads and the results are the same.
On my home network, the function returns:
{
BSSID = "0:19:db:8:5c:cc";
SSID = "Das Boot";
SSIDDATA = <44617320 426f6f74>;
}
In reality, the BSSID of my router is 0:4:ed:66:81:xx where the xx definitely is not cc
Does anyone have experience using this function and have I missed something obvious (more likely) or is this an Apple bug (much less likely) ?
Any input is greatly appreciated,
Nicke.