matlab serial communication with arduino is very slow

4 次查看(过去 30 天)
I want to send commands with matlab, to arduino uno which is connected to a 4channel relay. but after about 8 seconds of communicating, it takes so long to send commands. so arduino recieves my command late and relay will change state after about 20 seconds delay!
here is my matlab code:
sr = serial('COM4','BaudRate',9600);
fopen(sr); % open Arduino USB port
while 1
command = input('say the command:');
if (command == 0)
break;
end
fprintf(sr,char(command),'async');
end
fclose(sr);
a = instrfind();
fclose(a);
and here is my aruino code(void loop code). and help will be appreciated.
if(Serial.available()>0) // if there is data to read
{
matdata = Serial.read(); // read the data
if (matdata==1)
{digitalWrite(relaypin1,HIGH);
}
else if(matdata==2)
{digitalWrite(relaypin1,LOW);
}
if(matdata==3)
{digitalWrite(relaypin2,HIGH);
}
else if(matdata==4)
{digitalWrite(relaypin2,LOW);
}
if(matdata==5)
{digitalWrite(relaypin3,HIGH);
}
else if(matdata==6)
{digitalWrite(relaypin3,LOW);
}
if(matdata==7)
{digitalWrite(relaypin4,HIGH);
}
else if(matdata==8)
{digitalWrite(relaypin4,LOW);
}
}
else
{Serial.println('None');}

回答(0 个)

类别

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