[Matlab] Simulink 串口接收详解1



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

原文链接: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

  • 27
    点赞
  • 159
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
MATLAB Simulink模块库详解》是一本介绍MATLAB Simulink模块库的详细说明书。SimulinkMATLAB的一个扩展工具,用于建立和仿真系统动态行为的模型。该模块库为用户提供了丰富的模块及其功能,可以使用图形化界面搭建复杂的系统模型。 这本书首先介绍了Simulink的基本概念和使用方法,包括如何创建模型、添模块和参数设置等。然后详细介绍了Simulink模块库各个模块的功能及其使用方法,包括信号处理、控制系统、通信系统等领域的模块。 在信号处理领域,该模块库提供了各种常用的滤波器、变换器和调制器等模块,用户可以通过简单地拖拽和连接这些模块来搭建信号处理系统,并进行仿真和分析。 在控制系统领域,该模块库提供了各种常见的控制器、传感器和执行器等模块,用户可以使用这些模块来设计和仿真各种控制系统,包括PID控制器、模糊控制器和自适应控制器等。 在通信系统领域,该模块库提供了各种常用的调制器、解调器和信道模型等模块,用户可以使用这些模块来模拟各种通信系统,并进行性能评估和优化。 此外,《MATLAB Simulink模块库详解》还介绍了如何自定义和扩展Simulink模块库,以满足用户的特定需求。该书使用简洁明了的语言,配有大量的示例和实践操作,帮助读者快速掌握Simulink模块库的使用方法。 总而言之,《MATLAB Simulink模块库详解》是一本全面介绍MATLAB Simulink模块库的参考书,对于想要深入学习和应用Simulink的工程师和研究人员来说是一本十分有价值的读物。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值