receive multiple serail data in simulink frm arduino without delay
6 ビュー (過去 30 日間)
古いコメントを表示
Hi Every one
I am using Matlab 2018 to receive multiple data from arduino. The Serial Receive block in Matlab 2018 has the pssibility of defining number of received data. So if I want to receive multiple data of size 2 (for examp.), I will set the data lenght to 2. However, If I try to send an array using the following code in arduino:
int a=10;
int b=20;
char c[2] = {a,b};
void setup() {
Serial1.begin(9600);
}
void loop() {
Serial1.write(c);
delay(100);
}
and use the fllowing simple simulink code to receive the array in simulink:
I receive the folloing error:
Build process completed successfully
Error occurred while executing External Mode MEX-file 'ext_comm':
ExtTargetPktPending() call failed while checking for target pkt
If I try to send the array elements seperately in arduino like as:
int a=10;
int b=20;
void setup() {
Serial1.begin(9600);
}
void loop() {
Serial1.write(a);
Serial1.write(b);
delay(100);
}
again I receive the same error. However, if I use a delay between every two data like as:
int a=10;
int b=20;
void setup() {
Serial1.begin(9600);
}
void loop() {
Serial1.write(a);
delay(100);
Serial1.write(b);
delay(100);
}
then the receive block in simulink works correctly an I can get the data. But it makes no sense for me to send multiple data with delays between them. I need to send all of may data simultaneously. I just can have one delay (equal to my time step )at the end of sending all data synchronously.
Also, I should mention that if I set the delays equal to small vaclues(like 10ms), again I receive the same error.
I would appreciate if some one helps me on this.
1 件のコメント
Walter Roberson
2019 年 10 月 31 日
char c[2] = {a,b};
That is not a byte.
That is not a string: it is not null terminated.
Serial1.write(c);
You can write() a byte or you can write a string.
回答 (3 件)
elham modaresi
2019 年 10 月 31 日
3 件のコメント
Arun Kumar
2019 年 10 月 31 日
Move the assignment inside setup or loop, it should work fine.
byte c[2];
void setup() {
c[0] =10;
c[1]=20;
Serial1.begin(9600);
}
void loop() {
Serial1.write(c,2);
//delay(10);
//Serial1.write(b);
delay(100);
}
Arun Kumar
2019 年 10 月 31 日
編集済み: Arun Kumar
2019 年 10 月 31 日
Hi,
This code should work fine. I've checked with Arduino Uno and Mega
int a=10;
int b=20;
void setup() {
Serial1.begin(9600);
}
void loop() {
Serial1.write(a);
Serial1.write(b);
delay(100);
}
Delay is not required in between serial writes.
elham modaresi
2019 年 10 月 31 日
3 件のコメント
Arun Kumar
2019 年 10 月 31 日
You need to move only the assignment, not the declaration. Since you are using the same variable in loop function also.
参考
カテゴリ
Help Center および File Exchange で Device Driver Blocks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!