How to read data sent from matlab in arduino?

24 次查看(过去 30 天)
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',5,'Terminator', 'CR');
fopen(a);
sendData=7;
% fwrite(a,'7','uint8','async');
fprintf(a,'%i',sendData);
the matlab is able to send the data and the arduino is recieving it, but the output is not as required. This is my arduino code:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop()
{
if(Serial.available()>0)
{
int command = Serial.read();
// char ch = Serial.read();
// // show the byte on serial monitor
// delay(500);
// Serial.print(ch, HEX);
if(command == 7)
{
delay(500);
Serial.println("i");
}
}
else
{
delay(500);
Serial.println("NON");
}
}
It is always printing "NON". What I was expecting is to print i.
I know the arduino reads data in bytes. So, I even tried 55 (ASCII value of 7), but still no change. I also tried to use atoi function, but there is no change. Instead of using atoi() function we can also use command == no. of bytes(7) as I had done above, but I dont understand how many bytes does ardiuno recognise. I used the command Serial.readBytes, but there is always error. Does anyone know how to read data in arduino sent from MATLAB?
I also used
byte i;
void setup() {
Serial.begin(115200);
}
void loop()
{
if(Serial.available()>0)
{
delay(500);
i = Serial.read();
Serial.print(i);
}
else
{
delay(500);
Serial.println("NON");
}
}
it is printing NON not 'i', even though i have sent the data.

回答(1 个)

Dhanashree Mohite
As per my understanding, serial data is not available to read in both cases.
Check status of serial object after open and ValuesSent property after fprintf to make sure connection and value is sent.
You can check below link for your doubts:
Also, you can try using MATLAB support package for Arduino. Refer below link for the same:
  4 个评论
Nagarjun Vinukonda
Nagarjun Vinukonda 2019-4-11
I got my problem solved. According to the two arduino codes i have posted above, the arduino software(IDE) is able to print the data, but unable to display in the serial monitor. Because, when I sent '7' from matlab into buffer the arduino receives it and prints it in serial monitor, but we cant see it because our serial monitor will be closed at that time(As we cant open the port and open the serial monitor at same time). So, when we close the port through matlab (fclose), the value sent to buffer will be already read and stored in a variable('command' or 'i'), leaving no value in the buffer. As we are in the loop, when we open the serial monitor after closing the port, the value available in the buffer(currently nothing) will be detected and hence prints 'NON' all the time.
The below arduino code is able to read data sent to it from matlab code i have provided above with it.
byte x;
int j = 0;
void setup() {
Serial.begin(115200);
delay(500);
}
void loop()
{
if(Serial.available()>0)
{
x = Serial.read();
j++;
}
if(j>0)
{
if((char(x)=='7'))
{
Serial.println("hi");
}
else
{
Serial.println("NON");
}
}
}
Now I am able to print 'hi' all the time.
Arturo Medina
Arturo Medina 2021-5-23
Hello, are you saying that we do not have to use the fclose command to update the arduino terminal? I am trying to run your example on my Arduino Uno board and am getting COM# is busy after running your provided Matlab code followed by the Arduino one. Thank you!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 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