Reputation: 21
How can I send some data from MATLAB Simulink (serial send block) and receive that value in processing programming? Totally I need a float or an integer. I am using virtual serial port, for example COM1 for SIMULINK serial configuration and COM2 for processing.
Upvotes: 0
Views: 512
Reputation: 51837
You can use the Processing Serial Library to interface with serial ports.
Once quick and dirty option is too send the data as a string terminated with a new line character ('\n'
) from SIMULINK.
With a combination of bufferUntil('\n')
and serialEvent()
you can listen for a complete string, be it an int or float and simply parse it.
Here's a modified version of the example above to illustrate parsing:
// Example by Tom Igoe
import processing.serial.*;
Serial myPort; // The serial port
PFont myFont; // The display font
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed
void setup() {
size(400,200);
// You'll need to make this font with the Create Font Tool
myFont = loadFont("ArialMS-18.vlw");
textFont(myFont, 18);
// List all the available serial ports:
printArray(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
}
void draw() {
background(0);
text("received: " + inString, 10,50);
}
void serialEvent(Serial p) {
inString = p.readString();
// if we received a valid string from Simulink
if(inString != null && inString.length() > 0){
// trim white space (\n, etc.)
inString = inString.trim();
// parse value
int valueAsInt = int(inString);
float valueAsFloat = float(inString);
println("received: ", valueAsInt, valueAsFloat);
}
}
Note the above isn't tested (as I don't have Simulink), however it should illustrate the idea. (Remember to double check and update the serial port used before running, and of course match baud rates between Simulink and Processing).
This would be a simple way but not a very efficient way as you'd send mulitple bytes for a floating point value.
If you only need to send an int up to 255 (a byte) you can simply use readByte()
in Processing. If you need to send a larger integer (e.g. 16-bit or 32-bit integer) then you'd need something like readBytes()
to buffer individual bytes and then put them together into a larger higher precision integer. (Similar for the float).
Many years ago I remember working with a talented engineer on a robot and he was using Simulink, but instead of Serial we simply used sockets on the local machine to get the software to talk to each other. In that scenario, because we needed a constant fast stream of data we used UDP sockets (which in Processing can be handled by the oscP5 library). It might be worth checking if there's a Simulink addon/library for the OSC (Open Sound Control) protocol over UDP. It will make it much easier to pack named messages with ints/floats since you won't have to make up a communication protocol from scratch.
Upvotes: 0