Does Matlab handle USB communication?

10,465

Solution 1

You are likely to want the Instrument Control Toolbox.

Solution 2

Can you have your device present a USB virtual COM port? Then the normal MATLAB com port calls (overview) work well.

s1 = serial(port,'BaudRate',57600,'Parity','none','Stopbits',1, ...
       'Terminator','CR/LF'); % there are more properties to play with
fopen(s1);
fprintf(s1, 'text'); % appends terminator
resp = fscanf(s1);  % waits for terminator
fwrite(s1, [1 2 3 4 5], 'uint8'); % writes binary chars, no terminator
resp = fread(s1, s1.BytesAvailable, 'char'); % reads all available bytes as chars
fclose(s1);

These calls don't need the Data Acquisition toolbox, which is nice, and in my experience work ok with both ASCII and binary data.

On some computers, though, we found that the FOPEN call took forever. This had something to do with Bluetooth virtual COM ports on some laptops for some reason. So we ended up writing a really simple .NET DLL that wrapped the Microsoft .NET serial port class, then imported it into Matlab as an ActiveX server. But hopefully the above will let you get started.

Solution 3

Either you have virtual serial port and use example above, or you have some API in some language preferably C/C++ (if you want fast data transfers) to the device driver that is installed shown when you plug in the device.

Share:
10,465
Jader Dias
Author by

Jader Dias

Perl, Javascript, C#, Go, Matlab and Python Developer

Updated on June 28, 2022

Comments

  • Jader Dias
    Jader Dias almost 2 years

    I would like to receive the input for my MATLAB program from an USB source. It is possible? How? I am also the developer of the hardware that sends an audio stream through the USB. There is any way to send this kind of data it making easier to receive it?