前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Matlab] Simulink 串口接收详解1

[Matlab] Simulink 串口接收详解1

作者头像
祥知道
发布2020-03-10 15:45:32
2K0
发布2020-03-10 15:45:32
举报
文章被收录于专栏:祥的专栏祥的专栏

原创文章,欢迎转载。转载请注明:转载自 祥的博客

原文链接:https://cloud.tencent.com/developer/article/1596351


    1. 接收uint8数据
    • 1.1. 接收端设置
    1. 接收double数据
    • 2.1. 接收端设置
    • 2.2. 发送端设置
  • 3.模块讲解
    • 3.1. 模块 Serial Receive
    • 3.2. 模块 Serial Configuration
    1. 接收更加复杂的情况
  • 4.程序下载

1. 接收uint8数据

串口调试助手Matlab 发送两个字节aA,用ASCII码展示就是9765

1.1. 接收端设置

因为发送方只发送了2个uint8类型的数据,所以设置如下:

2. 接收double数据

我用Qt写了一个串口发送程序(上图右),这个程序的单值测试发送程序是一次发送2double类型的数据,用的是union进行发送,实际上就是一次发送16uint8 数据( 1个double占用8个字节)。

2.1. 接收端设置

主要是对于Data type进行了修改,现在我们传的的是uint8 buf[16] ,但是其意义是两个double类型的数值(double num[2])。

对于Serial Receive模块的设置,其实是让这个模块完成了两件事情:

  1. 接收数据。数据只有2个;
  2. 解析数据。这两个数据是double类型的,也就是接收了16个uint8类型的数据,然后解析为2个double类型的数据。

2.2. 发送端设置

代码语言:javascript
复制
//--------------------
//   发送类型数据定义
//--------------------
typedef union 
{
    char           buf[16];//用于发送和接收
    double         number[2];//用于解码
}Un_sendData;


//--------------------
//   发送部分代码展示
//--------------------
qint64 ret, len;
Un_sendData sendData;
double num1, num2;

//获取两个数值
num1 = ui.linE_num1->text().toDouble();
num2 = ui.linE_num2->text().toDouble();

//用union的double数组进行赋值
sendData.number[0] = num1;
sendData.number[1] = num2;


len = sizeof(sendData);
//用union的char型数组发送
m_Com.write(sendData.buf, len);//发送数据

3.模块讲解

3.1. 模块 Serial Receive

  • Communication port 设置串口号 Specify the serial port that you will use to receive from. You have to select an available port from the list. By default, the Communication port field contains the text Please select a port… and you must change this to a valid port. If you have not configured a port, the block will prompt you to do so. You can select a port from the available ports and then configure the port using the Serial Configuration block. Each Serial Receive block must have a configured serial port. If you use multiple ports in your simulation, you must configure each port separately
  • Header设置包头,解析用,我们不需要,复杂的解析以后用S-Function自己写解析函数 Specify data that marks the beginning of your data block. The header indicates the beginning of a new data block and the simulation will disregard data that occurs before the header. The header data is not sent to the output port. Only data that occurs between the header and the terminator is sent to the output port. By default none or no header is specified.
  • Terminator同上 Specify data that marks the end of your data block. The terminator indicates the end of the data block and the simulation will account for any data that occurs after the terminator as a new data block. The terminator data is not sent to the output port. Only data that occurs between the header and the terminator is sent to the output port. By default or no terminator is specified. Other available terminator formats are:
代码语言:txt
复制
- CR (‘\r’) — Carriage return
- LF (‘\n’) — Line feed
- CR/LF (‘\r\n’)
- NULL (‘\0’)Data size接收一次分割接收数据的大小
Specify the output data size, or the number of values that should be read at every simulation time step. The default size is 1 1.
  • Data type 解析数据类型设置 Specify the output data type to receive from the block. You can select from the following values:
代码语言:txt
复制
- single
- double
- int8
- uint8 (default)
- int16
- uint16
- int32
- uint32Byte order 数据大小端设置
When you specify a data type other than int8 or uint8, you can specify the byte order of the device for the binary data. Your options are BigEndian or LittleEndian.
  • Enable blocking mode 是否打开阻塞模式,没有来数据,阻塞 Specify if you want to block the simulation while receiving data. This option is selected by default. Clear this check box if you do not want the read operation to block the simulation.

If you enable blocking mode, the model will block the simulation while it is waiting for the requested data to be available. When you do not enable blocking mode, the simulation runs continuously. The block has two output ports, Status and Data. The Data port contains the requested set of data at each time step. The Status port contains 0 or 1 based on whether it received new data at the given time step.

  • Action when data is unavailable 阻塞时,数据的输出情况 Specify the action the block should take when data is unavailable. Available options are:
代码语言:txt
复制
- Output last received value — Block will return the value it received at the preceding time step when it does not receive data at current time step. This value is selected by default.
- Output custom value — Block will return any user–defined value when it does not receive current data. You can define the custom value in the Custom value field.
- Error — Block will return an error when it does not receive current data. This option is unavailable if you do not select blocking mode.Custom value

Specify a custom value for the block to output when it does not receive current data. The default value is 0. The custom value can be scalar or value equal to the size of Data that it receives (specified by Data size field).

  • Block sample time

Specify the sample time of the block during the simulation. This is the rate at which the block is executed during simulation. The default value is 1 second.

3.2. 模块 Serial Configuration

对于串口的一些基本设置。

3. 接收更加复杂的情况

由本文1和2两个小节可以看到,该串口接收只能解析单一的数据(要么是uint8数据类型,要么是doule数据类型)。但是实际我们的发送包会很复杂,有可能是多种类型的数据集合。这个时候应该怎么处理,我将会在下一个博客中进行讲解。

比如下面这个数据类型的发送,就不能用这些模块直接简单解析,这时候自己写解析函数,用S-Fucntion的形式,进行数据解析。

进阶教程:[Matlab]Simulink串口接收详解2用S-Function解析数据包

代码语言:javascript
复制
typedef struct
{
    uint8  head;//1
    double x;//8
    double y;//8
    uint8  z;//1
    double u;//8
    uint8  v;//1
    uint8 chkSum;//1
}St_Data;


typedef union 
{
    char         buf[28];//用于接收
    St_Data      data;
}Un_sendData;

4.程序下载

程序下载的所有地址集中在,下一个博客Simulink串口接收详解2用S-Function解析数据包:https://blog.csdn.net/humanking7/article/details/80856505的最后。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 接收uint8数据
    • 1.1. 接收端设置
    • 2. 接收double数据
      • 2.1. 接收端设置
        • 2.2. 发送端设置
        • 3.模块讲解
          • 3.1. 模块 Serial Receive
            • 3.2. 模块 Serial Configuration
            • 3. 接收更加复杂的情况
            • 4.程序下载
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档