Serial communications crashes when accessing an array

I'd been making a project involving serial communication between arduino and matlab 2016a.
So far, the communication was successful, but when i try to use the data received from arduino, the serial communication disconnected.
The arduino code is:
Serial.println(String(flexread1)+" "+String(flexread2)+" "+String(flexread3)+" "+String(flexread4));
The matlab code is:
disp('Initializing Serial...')
mcu=serial('COM6', 'BaudRate', 3000);
fopen(mcu);
try
while(true)
chk=str2num(fscanf(mcu));
getnum=chk(1);
end
catch
fclose(mcu);
disp('Serial closed..')
end
The command window displays
Initializing Serial...
Serial closed..
If I were to use getnum=chk; the array sent from arduino was able to be saved successfully into getnum, but the moment i used getnum=chk(1); the comunication got disconnected. Playing around with baudrate doesnt help.

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeArduino Hardware についてさらに検索

質問済み:

2018 年 3 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by