MATLAB Answers

How to make arduino receive data sent from MATLAB?

155 views (last 30 days)
nagarjun vinukonda
nagarjun vinukonda on 25 Mar 2019
Commented: Sravani Vanama on 19 Nov 2019
Hello,
I am trying to send data from MATLAB 2018Ra to Arduino software. I am using Chestnut PCB borad, belonging to Openbionics. I am using the follwing code in MATLAB:
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
a = serial('COM8','BaudRate',115200,'TimeOut',20,'Terminator', 'CR');
val=2;
fopen(a);
fprintf(a,'%i',val,'async');
the matlab is able to send the data:
>> a
Serial Port Object : Serial-COM8
Communication Settings
Port: COM8
BaudRate: 115200
Terminator: 'CR'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 1
But, the ardunio is not receiving:
This is my arduino code:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1000);
if(Serial.available()>0)
{
Serial.println("matLab");
}
else
{
Serial.println("NON");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
After running the code I am getting 'NON' as my answer.
I have tried, %d instead: no change.
I have tried, fwrite: Error is occuring, "PRECISION must be a character vector or string."
I also tried: fwrite(a,'2','async');--- no change.
if I dont use async, I am getting another error, that is the reason I am using async.
I have tried different ways, but the Serial moniter is unable to print the "matlab". How can I receive the data in Arduino? Can anyone please suggest me?

  0 Comments

Sign in to comment.

Answers (1)

Mark Sherstan
Mark Sherstan on 27 Mar 2019
I would consider putting your if statments in the void loop and you can simplify your connection and fprintf calls. Have a look at my code below. It is a slighty different use case but it should give you the tools you need. Let me know if you have specific questions.
MATLAB
% Connect to serial port
s = serial('/dev/cu.usbmodem14101', 'BaudRate', 115200);
fopen(s);
pause(3);
fprintf("Connection established\n")
% Start a counter and timer
count = 0;
tic
startTimer = toc;
% Get data for 5 seconds
while (toc < startTimer+5)
% Send character and receive data
fprintf(s, "a");
out = fscanf(s, '%d\n');
% Display data to user
fprintf("%d\n",out)
% Increment counter
count = count + 1;
end
% Display sample rate to user
endTimer = toc;
fprintf("Sample rate was: %0.2f Hz\n",count/(endTimer - startTimer))
% Remove serial port connection
fclose(s);
delete(s)
clear s
ARDUINO
// Declare variables
int rawSensorVal;
String incomingString;
void setup(){
// Initialize serial port
Serial.begin(115200);
Serial.setTimeout(3);
}
void loop(){
// Read analog pin
rawSensorVal = analogRead(A0);
// Check if MATLAB has sent a character
if (Serial.available() > 0) {
incomingString = Serial.readString();
// If character is correct send MATLAB the analog value
if (incomingString == "a\n") {
Serial.println(rawSensorVal);
}
}
}

  2 Comments

Nagarjun Vinukonda
Nagarjun Vinukonda on 2 Apr 2019
Thank you, it works when I put if statement in Loop. I understand it now, if the 'IF' statement is in the void loop it searches for value sent from MATLAB continousely, even though when it fails once. As when we put that statement in the setup, the arduino calls only once and fails....hence giving my output NON..
Sravani Vanama
Sravani Vanama on 19 Nov 2019
i have same problem....and tried it...but getting output as non in serial monitor infintely

Sign in to comment.

Sign in to answer this question.