MATLAB Answers

receive multiple serail data in simulink frm arduino without delay

7 views (last 30 days)
elham modaresi
elham modaresi on 31 Oct 2019
Commented: elham modaresi on 31 Oct 2019
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:
d1.png
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
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.

Sign in to comment.

Answers (3)

elham modaresi
elham modaresi on 31 Oct 2019
Hi Walter
Tanks for your comment.
I modify the code as:
byte c[2];
c[0] =10;
c[1]=20;
void setup() {
Serial1.begin(9600);
}
void loop() {
Serial1.write(c,2);
//delay(10);
//Serial1.write(b);
delay(100);
}
Arduino gets an erre: 'c' does not have a type.
Could u pls modify it?
  3 Comments
elham modaresi
elham modaresi on 31 Oct 2019
I modify the arduino code as:
byte c[2];
void setup()
{
c[0] = 10;
c[1] = 20;
Serial1.begin(9600);
}
void loop()
{
Serial1.write(c, 2);
delay(100);
}
Now I do not get an an arduino error. but still I cannot receive the data in receive block in simulink I get the same error:
Build process completed successfully
Error occurred while executing External Mode MEX-file 'ext_comm':
ExtTargetPktPending() call failed while checking for target pkt
also I tested the code like this:
byte a =10;
byte b= 20;
void setup()
{
Serial1.begin(9600);
}
void loop()
{
Serial1.write(a);
Serial1.write("\n");
Serial1.write(b);
delay(100);
}
again same error I got.
It only works when I set a delay between two data.
But this is not possible for me. I wil also attach my simulink code in case.
Thanks,

Sign in to comment.


Arun Kumar
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.
  1 Comment
elham modaresi
elham modaresi on 31 Oct 2019
This code does not worked for me. pls see the simulink code I attached before.

Sign in to comment.


elham modaresi
elham modaresi on 31 Oct 2019
but this does not work for me. I get the same error I mentioned before. I am using arduino Due. But I do not think this would make a problem.
I will attach my simulink code.
Could u pls have a look at it. Thanks in advance.
  3 Comments
elham modaresi
elham modaresi on 31 Oct 2019
I fixed the arduino code. It wont work without delays between data. How can I fix it please?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by