Serial Communication / Simulink

Hi, I want to send some binary data through processing serial to Matlab Simulink Software(serial receive block);

I defined header and terminator in Simulink, but i dont know how to define this in processing?

my code is like this but it does not work:

import processing.serial.*;
Serial myPort2;
int totalAngle = 1000;
String h;
  void setup()
{
  myPort2 = new Serial(this, "COM1", 9600);

}
void draw(){
  myPort2.write("A");
  for(int i=0; i<200000; i++){
 i = i+1;
myPort2.write(binary(i,16)); 
delay(200);
}  
 myPort2.write('\n'); 
}
1 Like

Hi @MJAhmadi and welcome to this forum,

I’m not familiar with Matlab Simulink but I did notice that your loop will take a long time to run due to the 200ms delay you have used. As you are looping 200000 times with a delay of 0.2 seconds it will take approximately 200000 x 0.2 = 40000 seconds or over 11 hours to complete one loop.

Also your loop is in the draw() function so it will keep repeating as long as the code is running. Is this what you want?

If you are simply testing the serial link you could put your loop in setup() as that only runs once when the program starts.

For testing, do you really need to output 200000 values?

You should also check the maximum value that 16 bits of binary can represent: it’s less than 200000, so your binary(i,16) function may not give you the values you expect.

You could try using println(binary(i,16)); in place of the myPort2.write(..); instruction to view the output of this function in the Processing console to see what it is actually doing.

Regards,

Phil.

1 Like

@MJAhmadi I just noticed that you are incrementing the loop variable i inside the loop. Is this what you intended? The i++ in the for (...) statement already increments i each time around the loop.

If you print the value of i you will see the difference when you comment out the i = i + 1; statement.

1 Like