简体   繁体   English

MATLAB SIMULINK 处理编程(串行通信)?

[英]MATLAB SIMULINK to Processing Programming ( Serial Communication)?

How can I send some data from MATLAB Simulink (serial send block) and receive that value in processing programming?如何从 MATLAB Simulink(串行发送块)发送一些数据并在处理编程中接收该值? 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.我使用的是虚拟串口,例如 COM1 用于 SIMULINK 串行配置,COM2 用于处理。

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.一旦快速和肮脏的选项也将数据作为以换行符( '\\n' )结尾的字符串从 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.通过bufferUntil('\\n')serialEvent()的组合,您可以监听完整的字符串,无论是 int 还是 float 并简单地解析它。

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.请注意,上述内容未经测试(因为我没有 Simulink),但它应该说明了这个想法。 (Remember to double check and update the serial port used before running, and of course match baud rates between Simulink and Processing). (记得在运行前仔细检查并更新使用的串口,当然还要匹配 Simulink 和 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.如果您只需要发送最多 255(一个字节)的 int,您可以在 Processing 中简单地使用readByte() If you need to send a larger integer (eg 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.如果您需要发送一个更大的整数(例如 16 位或 32 位整数),那么您需要像readBytes()这样的东西来缓冲单个字节,然后将它们组合成一个更大的更高精度的整数。 (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.许多年前,我记得与一位才华横溢的机器人工程师一起工作,他使用的是 Simulink,但我们没有使用 Serial,而是使用本地机器上的套接字来让软件相互通信。 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 ).在那种情况下,因为我们需要一个恒定的快速数据流,所以我们使用了 UDP 套接字(在处理中可以由oscP5 库处理)。 It might be worth checking if there's a Simulink addon/library for the OSC (Open Sound Control) protocol over UDP.可能值得检查是否有用于基于 UDP 的 OSC(开放式声音控制)协议的 Simulink 插件/库。 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.由于您不必从头开始编写通信协议,因此使用整数/浮点数打包命名消息将变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM