[Matlab] Simulink 串口接收详解1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/humanking7/article/details/80851223


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

原文链接:https://blog.csdn.net/humanking7/article/details/80851223



1. 接收uint8数据

MG20180629023322

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

1.1. 接收端设置

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

IMG20180629025836

2. 接收double数据

IMFG20180629030312

我用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类型的数据。

IMG20180629033524

2.2. 发送端设置

//--------------------
//   发送类型数据定义
//--------------------
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

IMG20180629025836

  • 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:

    • 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:

    • single
    • double
    • int8
    • uint8 (default)
    • int16
    • uint16
    • int32
    • uint32
  • Byte 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:

    • 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

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

IMG20180629035924

3. 接收更加复杂的情况

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

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

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

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的最后。


赞赏码New

猜你喜欢

转载自blog.csdn.net/humanking7/article/details/80851223
今日推荐