Send message to Arduino device from MATLAB


[dataOut,payloadSize] = sendCommand(obj,libName,commandID,dataIn)
[dataOut,payloadSize] = sendCommand(obj,libName,commandID,dataIn,timeout)



[dataOut,payloadSize] = sendCommand(obj,libName,commandID,dataIn) sends a command to the Command Handler implemented by your add-on library.

[dataOut,payloadSize] = sendCommand(obj,libName,commandID,dataIn,timeout) sends a command to the Command Handler implemented by your add-on library with a user-specified acknowledgement timeout.


collapse all

Create a command, and use sendCommand to send it to the Arduino® hardware.

Get the parent Arduino object.

arduinoObj = obj.Parent;

Define the library name.

libName = 'ExampleAddon/HelloWorld';

Define the command ID, and create data to send.

commandID = obj.READ_COMMAND;
dataIn = [];

Send the message.

[dataOut, payloadSize] = sendCommand(obj, libName, commandID, dataIn);

Show the result in the MATLAB® command line.

Hello World!

Input Arguments

collapse all

Handle to the Arduino object stored in the private Parent property of the custom class.

Name of library defined in the custom class, specified as a character vector.

Unique command identifier, specified as a number. This number must match the ID in the command handler defined in your custom library header file.

An array of integers, specified as a vector. The size of dataIn is different for different boards. The below table specifies the size of dataIn for different boards :

BoardsdataIn size
Leonardo, Micro150 bytes
MKR1000, MKR1010, Mega2560,MegaADK, Due720 bytes
Uno. Nano, Pro Mini, Digital Sandbox57 bytes

Each byte is limited to an unsigned integer between 0 and 255. Any values greater than 255 must be converted into two unsigned integers between 0 and 255, and will be sent as two separate bytes.

Timeout duration, specified as a numeric value that is the number of seconds to wait for an acknowledgement from the server code. Specifying a timeout duration blocks the MATLAB command line until the timeout duration is met. The default is 5 seconds if timeout is not specified.

Output Arguments

collapse all

Data returned, as an array of doubles

Data Types: uint8

Length of the output returned by sendCommand, as a number.

Introduced in R2015b