MATLAB SIMULINK对加工编程(串口通信)? [英] MATLAB SIMULINK to Processing Programming ( Serial Communication)?

查看:71
本文介绍了MATLAB SIMULINK对加工编程(串口通信)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从MATLAB SIMULINK(串行发送挡路)发送一些数据并在处理编程中接收该值?我完全需要一个浮点型或整型。 我使用的是虚拟串行端口,例如用于Simulink串行配置的COM1和用于处理的COM2。

推荐答案

您可以使用Processing Serial Library与串行端口连接。

一旦选择了"快速且脏"选项,则将数据作为字符串从Simulink发送,该字符串以换行符(' ')结尾。

使用bufferUntil(' ')serialEvent()的组合,您可以侦听完整的字符串(可以是整型字符串或浮点型字符串),只需对其进行分析即可。

以下是上面示例的修改版本,用于说明解析:

// 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 (
, etc.)
    inString = inString.trim();
    // parse value
    int valueAsInt = int(inString);
    float valueAsFloat = float(inString);
    println("received: ", valueAsInt, valueAsFloat);
  }
  
} 

注意上面的内容没有经过测试(因为我没有Simulink),但是它应该说明了这个想法。(记住在运行之前仔细检查并更新使用的串行端口,当然还要匹配Simulink和Processing之间的波特率)。

这将是一种简单的方法,但不是非常有效的方法,因为您将为浮点值发送多个字节。

如果您只需要发送一个最大255(一个字节)的int,则只需在处理中使用readByte()即可。如果您需要发送一个更大的整数(例如,16位或32位整数),那么您将需要类似readBytes()的内容来缓冲单个字节,然后将它们放在一起形成一个更大的更高精度的整数。(与彩车类似)。

很多年前,我记得我和一位才华横溢的工程师一起研究机器人,他使用的是Simulink,但我们没有使用Serial,而是简单地使用本地机器上的套接字来让软件相互通信。在那个场景中,因为我们需要恒定的快速数据流,所以我们使用了UDP套接字(在处理过程中可以由oscP5 library处理)。也许值得检查一下是否有用于UDP上的OSC(Open Sound Control,开放声音控制)协议的Simulink插件/库。这将使使用整型/浮点型打包命名消息变得更加容易,因为您不必从头开始构建通信协议。

这篇关于MATLAB SIMULINK对加工编程(串口通信)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆