3

Description of goal:

I'm trying to get matlab to determine if the computer is running windows, or mac. My main goal for this is to write a robust script to determine what serial ports are available, and USB ports (for the same reason) to identify which is an Arduino.

Current work:

I have a script that queries the registry on Windows and successfully identifies this information. However, I'm trying to make this script robust and identify for both mac and windows.

Request: If there is a better way to do this in matlab? And how do I do it robustly, hopefully independent of OS if possible, or if not, how do I do this on a Mac! Note, (instrfindall) only identifies objects to ports, which are therefore already open. So it is sadly not a solution.

EDIT: I can determine if it's MAC or WINDOWS via ismac, and ispc calls. However, the main guts of this question still remains!

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Leon92
  • 505
  • 1
  • 9
  • 15
  • http://www.mathworks.com/matlabcentral/fileexchange/33273-scanports-function-scans-for-available-serial-ports Is this script universal to Windows and Mac? – Yvon Jul 24 '14 at 06:28
  • Sadly, no :( I just tried it. But if you read it, it's searching for 'COM' ports. That's how they're addressed in windows. They're labelled and referenced completely different in mac -_- but thank you for your advice! – Leon92 Jul 24 '14 at 12:04
  • I've never been a serious Mac user so forgive me no knowledge at all. But in my view you can simply change the script a little bit to include Mac devices? http://www.mathworks.com/help/matlab/ref/serial.html Here the references are listed. – Yvon Jul 24 '14 at 15:59
  • If you want to make your script "independent of OS", then avoid registry queries since Windows may change the commands/API's across versions. Instead, try using Matlab commands as the answers suggest. It seems impossible to name serial ports in Windows and Mac in the same way, so you may still have to include both versions of code. Try `switch` :) – Yvon Jul 24 '14 at 16:04
  • That is the plan. But still yet to find a way to determine the serial ports. – Leon92 Jul 25 '14 at 04:32

5 Answers5

5

Even easier. MATLAB has a method to flag each OS without going to the level of CPU architecture.

if ismac
    % Code to run on Mac platform
elseif isunix
    % Code to run on Linux platform
elseif ispc
    % Code to run on Windows platform
else
    disp('Platform not supported')
end

Note: I am a MathWorks employee.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
yuvalz
  • 165
  • 2
  • 7
  • Nice to have a MathWorks employee here! A question: If MATLAB cannot run on unsupported platforms, what is the point of the last `else`? – Cris Luengo Nov 15 '19 at 20:18
  • Thanks! This is meant for illustration though in some cases due to lack of testing, hardware or underlying MATLAB support there will be situations where a platform will not be supported. – yuvalz Nov 18 '19 at 12:02
2

The MATLAB command you need to identify OS is computer

For example, on a Mac:

>> computer

ans =

MACI64
WalkingRandomly
  • 4,537
  • 5
  • 34
  • 34
  • Thanks for your help! This is also an option, however, I edited it because I learned of the ispc ismac command. So if you have any idea of the bulk of my question that'd be awesome! – Leon92 Jul 24 '14 at 01:13
2

This is simple.

You need to use the function ispc. The reference is http://www.mathworks.com/help/matlab/ref/ispc.html.

Returns 0 if Mac and 1 if Windows.

Serial port:http://www.mathworks.com/help/matlab/serial-port-devices.html. Read up on this.

lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • Thanks for your help! This is also an option, however, I edited it because I learned of the ispc ismac command. So if you have any idea of the bulk of my question that'd be awesome! – Leon92 Jul 24 '14 at 01:18
2

The command from inside MATLAB is computer. The following returns the value MACI which is for Mac OS X (Intel):

>> c = computer;
>> 

c =

MACI

>> 

Type help computer for more.

Madhurjya
  • 497
  • 5
  • 17
  • Thanks for your help! This is also an option, however, I edited it because I learned of the ispc ismac command. So if you have any idea of the bulk of my question that'd be awesome! – Leon92 Jul 24 '14 at 01:13
1

If you have instrument control toolbox you may check the instrhwinfo function to get available serial ports.

If not I would suggest to go to system command. For example, on windows you may parse output of the mode command:

[status,cmdout] = system('mode');

Variable cmdout will contain something like this:

Status for device CON:
----------------------
    Lines:          2048
    Columns:        200
    Keyboard rate:  31
    Keyboard delay: 1
    Code page:      866

for each device like serial ports and LPT ports. I'm not MAC user, but I suspect you may find something similar there too.

UPD1

I do not think that this way you can distinguish between real serial ports and USB-to-serial adapters, but it should give you list of available serial ports at the moment you call the mode function.

anandr
  • 1,622
  • 1
  • 12
  • 10
  • I'll basically send a message to all and see if I get the designated response(if any) to determine if it's the correct one (unless), like on windows, I can identify the device name and decide if it's the correct device that way. I will give this a look into! And let you know what I find, thank you :) – Leon92 Jul 24 '14 at 12:07