receive multiple serail data in simulink frm arduino without delay
5 views (last 30 days)
Show older comments
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 Comment
Walter Roberson
on 31 Oct 2019
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.
Answers (3)
elham modaresi
on 31 Oct 2019
3 Comments
Arun Kumar
on 31 Oct 2019
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
on 31 Oct 2019
Edited: Arun Kumar
on 31 Oct 2019
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
on 31 Oct 2019
3 Comments
Arun Kumar
on 31 Oct 2019
You need to move only the assignment, not the declaration. Since you are using the same variable in loop function also.
See Also
Categories
Find more on Device Driver Blocks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!