フィルターのクリア

Matlab and Arduino serial data

5 ビュー (過去 30 日間)
Shaunak Chandwadkar
Shaunak Chandwadkar 2017 年 4 月 13 日
回答済み: Nick 2017 年 4 月 14 日
I am sending a string data to Arduino from Matlab using the following code:
clear s;
s=serial('COM4','BAUD', 9600); % Baud rate and COM port
fopen(s);
value1=('11,222;333<444>555?666');
display('data sent');
fprintf(s,'%s\n',value1);
fclose(s);
I am receiving and processing the string in Arduino using the following code:
String command;
int i;
float data_rec;
void setup() {
// put your setup code here, to run once:
command="11,222;333<444>555?666";
Serial.begin(9600);
}
void loop() {
if(Serial.available() >0){
String command=Serial.readString();
parseCommand(command);
data_rec=Serial.parseInt();
Serial.println("data_rec");
Serial.println(data_rec);
}
}
void parseCommand(String com){
String F_d;
String E_m;
String L_p;
String S_v;
String H_d;
String L_t;
F_d=com.substring(0,com.indexOf(','));
E_m=com.substring(com.indexOf(',')+1,com.indexOf(';'));
L_p=com.substring(com.indexOf(';')+1,com.indexOf('<'));
S_v=com.substring(com.indexOf('<')+1,com.indexOf('>'));
H_d=com.substring(com.indexOf('>')+1,com.indexOf('?'));
L_t=com.substring(com.indexOf('?')+1,com.length());
Serial.println(com);
Serial.println("F_d:");
Serial.println(F_d);
Serial.println("E_m:");
Serial.println(E_m);
Serial.println("L_p:");
Serial.println(L_p);
Serial.println("S_v:");
Serial.println(S_v);
Serial.println("H_d:");
Serial.println(H_d);
Serial.println("L_t:");
Serial.println(L_t);
}
This does not work with Matlab. However, when I send the same string from the Serial Monitor, it seems to work.
Why aren't I getting any output for the Matlab code? I am checking the data sent and received by Arduino over serial by monitoring the serial port using Serial Monitoring Studio.

回答 (1 件)

Nick
Nick 2017 年 4 月 14 日
Hi try adding a pause of a few seconds after you initialize the serial port. When you initialize the serial port on the Arduino, it resets the Arduino so you might be sending information before the Arduino is ready for it.
I added pause(2) and started to receive data back pause(1) still resulted in a timeout. You can check if there is data on buffer by using
s.BytesAvailable
and one way to read data back from the Arduino in Matlab is
fscanf(s)

カテゴリ

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