How to make arduino receive data sent from MATLAB?

7 ビュー (過去 30 日間)
nagarjun vinukonda
nagarjun vinukonda 2019 年 3 月 25 日
コメント済み: Sravani Vanama 2019 年 11 月 19 日
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?

回答 (1 件)

Mark Sherstan
Mark Sherstan 2019 年 3 月 27 日
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 件のコメント
Nagarjun Vinukonda
Nagarjun Vinukonda 2019 年 4 月 2 日
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 2019 年 11 月 19 日
i have same problem....and tried it...but getting output as non in serial monitor infintely

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by