How to read data sent from matlab in arduino?
21 次查看(过去 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.
0 个评论
回答(1 个)
Dhanashree Mohite
2019-4-9
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 个评论
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 Center 和 File 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!