receive multiple serail data in simulink frm arduino without delay
    7 次查看(过去 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 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!